diff --git a/coub-dl.py b/coub-dl.py index a1ac0e3..5321423 100644 --- a/coub-dl.py +++ b/coub-dl.py @@ -6,7 +6,7 @@ from enum import Enum from datetime import timedelta from math import floor from argparse import ArgumentParser -from functools import wraps +from functools import wraps, partial from atexit import register @@ -54,53 +54,55 @@ def get_duration(ffprobe_output): @call_verbose(before_message='Downloading audio stream... ') -def download_audio_stream(): +def download_audio_stream(url, file_dict): call(('youtube-dl', '--ignore-config', '--extract-audio', - '--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]), - URL), + '--output', '{}.%(ext)s'.format(file_dict[Stream.AUDIO]), + url), stdout=DEVNULL, stderr=DEVNULL) @call_verbose(before_message='Downloading video stream... ') -def download_video_stream(): +def download_video_stream(url, file_dict): call(('youtube-dl', '--ignore-config', - '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]), - URL), + '--output', '{}.%(ext)s'.format(file_dict[Stream.VIDEO]), + url), stdout=DEVNULL, stderr=DEVNULL) -def read_extensions(): +def read_extensions(file_dict): for file in listdir(): - for filename in FILES: - if match('^{}.*'.format(FILES[filename]), file): - FILES[filename] = file + for filename in file_dict: + if match('^{}.*'.format(file_dict[filename]), file): + file_dict[filename] = file @call_verbose(before_message='Looping shorter stream... ') -def loop_shorter_stream(): - call(('ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], - '-c', 'copy', FILES[File.LOOP]), +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) - call(('ffmpeg', '-i', shorterFile, '-t', str(timesLoop_fraction * shorter.total_seconds()), FILES[File.FRACTION]), + # concat them + call(('ffmpeg', '-f', 'concat', '-i', file_dict[File.LIST], + '-c', 'copy', file_dict[File.LOOP]), stdout=DEVNULL, stderr=DEVNULL) @call_verbose(before_message='Muxing streams... ') -def mux_streams(): - call(('ffmpeg', '-i', FILES[File.LOOP], - '-i', FILES[Stream.AUDIO], +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', FILES[File.OUTPUT]), + '-c', 'copy', file_dict[File.OUTPUT]), stdout=DEVNULL, stderr=DEVNULL) -def cleanup(): - for key in FILES: - if key not in OUTPUT_KEYS: +def cleanup(file_dict, outputs): + for key in file_dict: + if key not in outputs: try: - remove(FILES[key]) + remove(file_dict[key]) except FileNotFoundError: pass @@ -129,10 +131,6 @@ def yes_no_question(question, default): -# clean up on exit -register(cleanup) - - # parse arguments parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.') parser.add_argument('-nv', '--nonverbose', action='store_true', help='Turn off non-critical messages to user.') @@ -152,6 +150,10 @@ OUTPUT_KEYS = [File.OUTPUT] URL = args.url +# clean up on exit +register(partial(cleanup, FILES, OUTPUT_KEYS)) + + # fetch video title if no filename was specified if args.output is None: FILES[File.OUTPUT] = check_output(('youtube-dl', '--get-title', args.url)).decode('utf-8').strip() @@ -169,9 +171,9 @@ if exists(FILES[File.OUTPUT]): remove(FILES[File.OUTPUT]) # download streams and update FILE dict with extensions -download_audio_stream() -download_video_stream() -read_extensions() +download_audio_stream(URL, FILES) +download_video_stream(URL, FILES) +read_extensions(FILES) # get stream lengths via ffprobe audioData= get_duration(get_command_stderr(('ffprobe', FILES[Stream.AUDIO]))).split(':') @@ -197,5 +199,5 @@ with open(FILES[File.LIST], 'w') as f: print("file '{}'".format(shorterFile), file=f) print("file '{}'".format(FILES[File.FRACTION]), file=f) -loop_shorter_stream() -mux_streams() +loop_shorter_stream(FILES, shorter, shorterFile, timesLoop_fraction) +mux_streams(FILES)