1
0
Fork 0
mirror of synced 2024-07-03 05:21:02 +12:00
bulk-downloader-for-reddit/bulkredditdownloader/downloaders/vreddit.py

59 lines
2.2 KiB
Python
Raw Normal View History

import os
2021-02-07 01:29:13 +13:00
import pathlib
import subprocess
2021-02-07 14:09:31 +13:00
from bulkredditdownloader.downloaders.downloader_utils import getFile
2021-02-07 14:05:18 +13:00
from bulkredditdownloader.utils import GLOBAL
from bulkredditdownloader.utils import printToFile as print
class VReddit:
2021-02-07 01:29:13 +13:00
def __init__(self, directory: pathlib.Path, post: dict):
extension = ".mp4"
if not os.path.exists(directory):
os.makedirs(directory)
filename = GLOBAL.config['filename'].format(**post) + extension
short_filename = post['POSTID'] + extension
try:
fnull = open(os.devnull, 'w')
subprocess.call("ffmpeg", stdout=fnull, stderr=subprocess.STDOUT)
except Exception:
getFile(filename, short_filename, directory, post['CONTENTURL'])
print("FFMPEG library not found, skipping merging video and audio")
else:
video_name = post['POSTID'] + "_video"
video_url = post['CONTENTURL']
audio_name = post['POSTID'] + "_audio"
audio_url = video_url[:video_url.rfind('/')] + '/DASH_audio.mp4'
print(directory, filename, sep="\n")
getFile(video_name, video_name, directory, video_url, silent=True)
getFile(audio_name, audio_name, directory, audio_url, silent=True)
try:
self._mergeAudio(video_name, audio_name, filename, short_filename, directory)
except KeyboardInterrupt:
os.remove(directory / filename)
os.remove(directory / audio_name)
os.rename(directory / video_name, directory / filename)
@staticmethod
2021-02-07 01:29:13 +13:00
def _mergeAudio(
video: pathlib.Path,
audio: pathlib.Path,
filename: pathlib.Path,
short_filename,
directory: pathlib.Path):
input_video = str(directory / video)
input_audio = str(directory / audio)
fnull = open(os.devnull, 'w')
cmd = "ffmpeg -i {} -i {} -c:v copy -c:a aac -strict experimental {}".format(
input_audio, input_video, str(directory / filename))
subprocess.call(cmd.split(), stdout=fnull, stderr=subprocess.STDOUT)
os.remove(directory / video)
os.remove(directory / audio)