started working on OO refactor
This commit is contained in:
parent
5f411e09e2
commit
720b9af4d8
118
coub-dl.py
118
coub-dl.py
@ -52,22 +52,29 @@ def get_output(*args, **kwargs):
|
||||
return check_output(*args, **kwargs).decode().rstrip('\n')
|
||||
|
||||
|
||||
def run(url, files_dict, directory):
|
||||
class coub_dl:
|
||||
def __init__(self, url, files_dict, directory):
|
||||
self._url = url
|
||||
self._files_dict = files_dict
|
||||
self._directory = directory
|
||||
|
||||
|
||||
def __call__(self):
|
||||
# download streams and update FILE dict with extensions
|
||||
download_audio_stream(url, files_dict)
|
||||
download_video_stream(url, files_dict)
|
||||
read_extensions(files_dict, directory)
|
||||
self.download_audio_stream()
|
||||
self.download_video_stream()
|
||||
self.read_extensions()
|
||||
|
||||
# get stream lengths via ffprobe
|
||||
audioLen = get_length(files_dict[Stream.AUDIO])
|
||||
videoLen = get_length(files_dict[Stream.VIDEO])
|
||||
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 = 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]
|
||||
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()
|
||||
@ -75,29 +82,29 @@ def run(url, files_dict, directory):
|
||||
timesLoop_fraction = times % 1
|
||||
|
||||
# write concat helper file for ffmpeg
|
||||
with open(files_dict[File.LIST], 'w') as f:
|
||||
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(files_dict[File.FRACTION]), file=f)
|
||||
print("file '{}'".format(self._files_dict[File.FRACTION]), file=f)
|
||||
|
||||
# loop & mux streams
|
||||
loop_shorter_stream(files_dict, shorter, shorterFile, timesLoop_fraction)
|
||||
mux_streams(files_dict)
|
||||
self.loop_shorter_stream(shorter, shorterFile, timesLoop_fraction)
|
||||
self.mux_streams()
|
||||
|
||||
|
||||
@call_verbose(before_message='Downloading audio stream... ')
|
||||
def download_audio_stream(url, file_dict):
|
||||
def download_audio_stream(self):
|
||||
call(('youtube-dl', '--ignore-config',
|
||||
'--extract-audio',
|
||||
'--output', '{}.%(ext)s'.format(file_dict[Stream.AUDIO]),
|
||||
url),
|
||||
'--output', '{}.%(ext)s'.format(self._files_dict[Stream.AUDIO]),
|
||||
self._url),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
@call_verbose(before_message='Downloading video stream... ')
|
||||
def download_video_stream(url, file_dict):
|
||||
def download_video_stream(self):
|
||||
url = get_output(('youtube-dl',
|
||||
'--get-url', url))
|
||||
'--get-url', self._url))
|
||||
|
||||
curl = Popen(('curl', '--silent',
|
||||
'--write-out',
|
||||
@ -112,17 +119,47 @@ def download_video_stream(url, file_dict):
|
||||
stdin=grep.stdout).strip('"')
|
||||
|
||||
call(('youtube-dl', '--ignore-config',
|
||||
'--output', '{}.%(ext)s'.format(file_dict[Stream.VIDEO]),
|
||||
'--output', '{}.%(ext)s'.format(self._files_dict[Stream.VIDEO]),
|
||||
url),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
def read_extensions(file_dict, directory):
|
||||
for file in listdir(directory):
|
||||
for filename in file_dict:
|
||||
fullname = join(directory, file)
|
||||
if match('^{}.*'.format(file_dict[filename]), fullname):
|
||||
file_dict[filename] = fullname
|
||||
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):
|
||||
@ -145,27 +182,6 @@ def get_duration(ffprobe_output):
|
||||
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!')
|
||||
def check_for_dependencies():
|
||||
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)))
|
||||
|
||||
|
||||
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():
|
||||
return {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
||||
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}:
|
||||
FILES[key] = join(dir, FILES[key])
|
||||
|
||||
run(URL, FILES, dir)
|
||||
coub_dl(URL, FILES, dir)()
|
||||
|
Loading…
Reference in New Issue
Block a user