started working on OO refactor
This commit is contained in:
parent
5f411e09e2
commit
720b9af4d8
198
coub-dl.py
198
coub-dl.py
@ -52,77 +52,114 @@ def get_output(*args, **kwargs):
|
|||||||
return check_output(*args, **kwargs).decode().rstrip('\n')
|
return check_output(*args, **kwargs).decode().rstrip('\n')
|
||||||
|
|
||||||
|
|
||||||
def run(url, files_dict, directory):
|
class coub_dl:
|
||||||
# download streams and update FILE dict with extensions
|
def __init__(self, url, files_dict, directory):
|
||||||
download_audio_stream(url, files_dict)
|
self._url = url
|
||||||
download_video_stream(url, files_dict)
|
self._files_dict = files_dict
|
||||||
read_extensions(files_dict, directory)
|
self._directory = directory
|
||||||
|
|
||||||
# get stream lengths via ffprobe
|
|
||||||
audioLen = get_length(files_dict[Stream.AUDIO])
|
|
||||||
videoLen = get_length(files_dict[Stream.VIDEO])
|
|
||||||
|
|
||||||
# decide which stream needs some looping
|
|
||||||
longer = audioLen if audioLen > videoLen else videoLen
|
|
||||||
shorter = audioLen if audioLen < videoLen else videoLen
|
|
||||||
shorterFile = files_dict[Stream.AUDIO] if audioLen < videoLen else files_dict[Stream.VIDEO]
|
|
||||||
files_dict[File.LOOP] += splitext(shorterFile)[1]
|
|
||||||
files_dict[File.FRACTION] += splitext(shorterFile)[1]
|
|
||||||
|
|
||||||
# calculate how many times to loop
|
|
||||||
times = longer.total_seconds() / shorter.total_seconds()
|
|
||||||
timesLoop_base = floor(times)
|
|
||||||
timesLoop_fraction = times % 1
|
|
||||||
|
|
||||||
# write concat helper file for ffmpeg
|
|
||||||
with open(files_dict[File.LIST], 'w') as f:
|
|
||||||
for i in range(timesLoop_base):
|
|
||||||
print("file '{}'".format(shorterFile), file=f)
|
|
||||||
print("file '{}'".format(files_dict[File.FRACTION]), file=f)
|
|
||||||
|
|
||||||
# loop & mux streams
|
|
||||||
loop_shorter_stream(files_dict, shorter, shorterFile, timesLoop_fraction)
|
|
||||||
mux_streams(files_dict)
|
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Downloading audio stream... ')
|
def __call__(self):
|
||||||
def download_audio_stream(url, file_dict):
|
# download streams and update FILE dict with extensions
|
||||||
call(('youtube-dl', '--ignore-config',
|
self.download_audio_stream()
|
||||||
'--extract-audio',
|
self.download_video_stream()
|
||||||
'--output', '{}.%(ext)s'.format(file_dict[Stream.AUDIO]),
|
self.read_extensions()
|
||||||
url),
|
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
# get stream lengths via ffprobe
|
||||||
|
audioLen = get_length(self._files_dict[Stream.AUDIO])
|
||||||
|
videoLen = get_length(self._files_dict[Stream.VIDEO])
|
||||||
|
|
||||||
|
# decide which stream needs some looping
|
||||||
|
longer = audioLen if audioLen > videoLen else videoLen
|
||||||
|
shorter = audioLen if audioLen < videoLen else videoLen
|
||||||
|
shorterFile = self._files_dict[Stream.AUDIO] if audioLen < videoLen else self._files_dict[Stream.VIDEO]
|
||||||
|
self._files_dict[File.LOOP] += splitext(shorterFile)[1]
|
||||||
|
self._files_dict[File.FRACTION] += splitext(shorterFile)[1]
|
||||||
|
|
||||||
|
# calculate how many times to loop
|
||||||
|
times = longer.total_seconds() / shorter.total_seconds()
|
||||||
|
timesLoop_base = floor(times)
|
||||||
|
timesLoop_fraction = times % 1
|
||||||
|
|
||||||
|
# write concat helper file for ffmpeg
|
||||||
|
with open(self._files_dict[File.LIST], 'w') as f:
|
||||||
|
for i in range(timesLoop_base):
|
||||||
|
print("file '{}'".format(shorterFile), file=f)
|
||||||
|
print("file '{}'".format(self._files_dict[File.FRACTION]), file=f)
|
||||||
|
|
||||||
|
# loop & mux streams
|
||||||
|
self.loop_shorter_stream(shorter, shorterFile, timesLoop_fraction)
|
||||||
|
self.mux_streams()
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Downloading video stream... ')
|
@call_verbose(before_message='Downloading audio stream... ')
|
||||||
def download_video_stream(url, file_dict):
|
def download_audio_stream(self):
|
||||||
url = get_output(('youtube-dl',
|
call(('youtube-dl', '--ignore-config',
|
||||||
'--get-url', url))
|
'--extract-audio',
|
||||||
|
'--output', '{}.%(ext)s'.format(self._files_dict[Stream.AUDIO]),
|
||||||
curl = Popen(('curl', '--silent',
|
self._url),
|
||||||
'--write-out',
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
'--location',
|
|
||||||
url),
|
|
||||||
stdout=PIPE)
|
|
||||||
|
|
||||||
grep = Popen(('grep', '--only-matching', '"http.*"'),
|
|
||||||
stdin=curl.stdout, stdout=PIPE)
|
|
||||||
|
|
||||||
url = get_output(('sed', 's/muted_//g'),
|
|
||||||
stdin=grep.stdout).strip('"')
|
|
||||||
|
|
||||||
call(('youtube-dl', '--ignore-config',
|
|
||||||
'--output', '{}.%(ext)s'.format(file_dict[Stream.VIDEO]),
|
|
||||||
url),
|
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
|
||||||
|
|
||||||
|
|
||||||
def read_extensions(file_dict, directory):
|
@call_verbose(before_message='Downloading video stream... ')
|
||||||
for file in listdir(directory):
|
def download_video_stream(self):
|
||||||
for filename in file_dict:
|
url = get_output(('youtube-dl',
|
||||||
fullname = join(directory, file)
|
'--get-url', self._url))
|
||||||
if match('^{}.*'.format(file_dict[filename]), fullname):
|
|
||||||
file_dict[filename] = fullname
|
curl = Popen(('curl', '--silent',
|
||||||
|
'--write-out',
|
||||||
|
'--location',
|
||||||
|
url),
|
||||||
|
stdout=PIPE)
|
||||||
|
|
||||||
|
grep = Popen(('grep', '--only-matching', '"http.*"'),
|
||||||
|
stdin=curl.stdout, stdout=PIPE)
|
||||||
|
|
||||||
|
url = get_output(('sed', 's/muted_//g'),
|
||||||
|
stdin=grep.stdout).strip('"')
|
||||||
|
|
||||||
|
call(('youtube-dl', '--ignore-config',
|
||||||
|
'--output', '{}.%(ext)s'.format(self._files_dict[Stream.VIDEO]),
|
||||||
|
url),
|
||||||
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
|
def read_extensions(self):
|
||||||
|
for file in listdir(self._directory):
|
||||||
|
for filename in self._files_dict:
|
||||||
|
fullname = join(self._directory, file)
|
||||||
|
if match('^{}.*'.format(self._files_dict[filename]), fullname):
|
||||||
|
self._files_dict[filename] = fullname
|
||||||
|
|
||||||
|
|
||||||
|
@call_verbose(before_message='Looping shorter stream... ')
|
||||||
|
def loop_shorter_stream(self, shorter, shorter_file, loop_fraction):
|
||||||
|
# prepare last fractional loop
|
||||||
|
call(('ffmpeg', '-i', shorter_file, '-t', str(loop_fraction * shorter.total_seconds()),
|
||||||
|
self._files_dict[File.FRACTION]),
|
||||||
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
# concat them
|
||||||
|
call(('ffmpeg', '-f', 'concat', '-safe', '0', '-i', self._files_dict[File.LIST],
|
||||||
|
'-c', 'copy', self._files_dict[File.LOOP]),
|
||||||
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
|
@call_verbose(before_message='Muxing streams... ')
|
||||||
|
def mux_streams(self):
|
||||||
|
call(('ffmpeg', '-i', self._files_dict[File.LOOP],
|
||||||
|
'-i', self._files_dict[Stream.AUDIO],
|
||||||
|
'-map', '0:v:0', '-map', '1:a:0',
|
||||||
|
'-c', 'copy', self._files_dict[File.OUTPUT]),
|
||||||
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
|
def determine_output_filename(url, user_supplied, extension, files_dict):
|
||||||
|
if user_supplied is None:
|
||||||
|
files_dict[File.OUTPUT] = get_output(('youtube-dl', '--get-title', url))
|
||||||
|
else:
|
||||||
|
files_dict[File.OUTPUT] = user_supplied
|
||||||
|
files_dict[File.OUTPUT] += extension
|
||||||
|
|
||||||
|
|
||||||
def get_length(file):
|
def get_length(file):
|
||||||
@ -145,27 +182,6 @@ def get_duration(ffprobe_output):
|
|||||||
return duration
|
return duration
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Looping shorter stream... ')
|
|
||||||
def loop_shorter_stream(file_dict, shorter, shorter_file, loop_fraction):
|
|
||||||
# prepare last fractional loop
|
|
||||||
call(('ffmpeg', '-i', shorter_file, '-t', str(loop_fraction * shorter.total_seconds()), file_dict[File.FRACTION]),
|
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
|
||||||
|
|
||||||
# concat them
|
|
||||||
call(('ffmpeg', '-f', 'concat', '-safe', '0', '-i', file_dict[File.LIST],
|
|
||||||
'-c', 'copy', file_dict[File.LOOP]),
|
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Muxing streams... ')
|
|
||||||
def mux_streams(file_dict):
|
|
||||||
call(('ffmpeg', '-i', file_dict[File.LOOP],
|
|
||||||
'-i', file_dict[Stream.AUDIO],
|
|
||||||
'-map', '0:v:0', '-map', '1:a:0',
|
|
||||||
'-c', 'copy', file_dict[File.OUTPUT]),
|
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Checking your system for dependencies... ', after_message='Found all!')
|
@call_verbose(before_message='Checking your system for dependencies... ', after_message='Found all!')
|
||||||
def check_for_dependencies():
|
def check_for_dependencies():
|
||||||
check_for = (('youtube-dl', '--version'), ('ffmpeg', '-version'), ('curl', '--version'))
|
check_for = (('youtube-dl', '--version'), ('ffmpeg', '-version'), ('curl', '--version'))
|
||||||
@ -179,14 +195,6 @@ def check_for_dependencies():
|
|||||||
if missing: exit(error_str.format(', '.join(missing)))
|
if missing: exit(error_str.format(', '.join(missing)))
|
||||||
|
|
||||||
|
|
||||||
def determine_output_filename(url, user_supplied, extension, files_dict):
|
|
||||||
if user_supplied is None:
|
|
||||||
files_dict[File.OUTPUT] = get_output(('youtube-dl', '--get-title', url))
|
|
||||||
else:
|
|
||||||
files_dict[File.OUTPUT] = user_supplied
|
|
||||||
files_dict[File.OUTPUT] += extension
|
|
||||||
|
|
||||||
|
|
||||||
def build_default_files_dict():
|
def build_default_files_dict():
|
||||||
return {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
return {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
||||||
File.LIST: 'list.txt', File.LOOP: 'loop', File.FRACTION: 'fraction',
|
File.LIST: 'list.txt', File.LOOP: 'loop', File.FRACTION: 'fraction',
|
||||||
@ -275,4 +283,4 @@ if __name__ == '__main__':
|
|||||||
for key in {key: FILES[key] for key in FILES if key not in OUTPUT_KEYS}:
|
for key in {key: FILES[key] for key in FILES if key not in OUTPUT_KEYS}:
|
||||||
FILES[key] = join(dir, FILES[key])
|
FILES[key] = join(dir, FILES[key])
|
||||||
|
|
||||||
run(URL, FILES, dir)
|
coub_dl(URL, FILES, dir)()
|
||||||
|
Loading…
Reference in New Issue
Block a user