refactor & fix for a bug which accidentally brought back issue #1.
This commit is contained in:
parent
aabc57cc9f
commit
bb680ad5b7
66
coub-dl.py
66
coub-dl.py
@ -6,7 +6,7 @@ from enum import Enum
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from math import floor
|
from math import floor
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from functools import wraps
|
from functools import wraps, partial
|
||||||
from atexit import register
|
from atexit import register
|
||||||
|
|
||||||
|
|
||||||
@ -54,53 +54,55 @@ def get_duration(ffprobe_output):
|
|||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Downloading audio stream... ')
|
@call_verbose(before_message='Downloading audio stream... ')
|
||||||
def download_audio_stream():
|
def download_audio_stream(url, file_dict):
|
||||||
call(('youtube-dl', '--ignore-config',
|
call(('youtube-dl', '--ignore-config',
|
||||||
'--extract-audio',
|
'--extract-audio',
|
||||||
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
|
'--output', '{}.%(ext)s'.format(file_dict[Stream.AUDIO]),
|
||||||
URL),
|
url),
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Downloading video stream... ')
|
@call_verbose(before_message='Downloading video stream... ')
|
||||||
def download_video_stream():
|
def download_video_stream(url, file_dict):
|
||||||
call(('youtube-dl', '--ignore-config',
|
call(('youtube-dl', '--ignore-config',
|
||||||
'--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
|
'--output', '{}.%(ext)s'.format(file_dict[Stream.VIDEO]),
|
||||||
URL),
|
url),
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def read_extensions():
|
def read_extensions(file_dict):
|
||||||
for file in listdir():
|
for file in listdir():
|
||||||
for filename in FILES:
|
for filename in file_dict:
|
||||||
if match('^{}.*'.format(FILES[filename]), file):
|
if match('^{}.*'.format(file_dict[filename]), file):
|
||||||
FILES[filename] = file
|
file_dict[filename] = file
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Looping shorter stream... ')
|
@call_verbose(before_message='Looping shorter stream... ')
|
||||||
def loop_shorter_stream():
|
def loop_shorter_stream(file_dict, shorter, shorter_file, loop_fraction):
|
||||||
call(('ffmpeg', '-f', 'concat', '-i', FILES[File.LIST],
|
# prepare last fractional loop
|
||||||
'-c', 'copy', FILES[File.LOOP]),
|
call(('ffmpeg', '-i', shorter_file, '-t', str(loop_fraction * shorter.total_seconds()), file_dict[File.FRACTION]),
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
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)
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Muxing streams... ')
|
@call_verbose(before_message='Muxing streams... ')
|
||||||
def mux_streams():
|
def mux_streams(file_dict):
|
||||||
call(('ffmpeg', '-i', FILES[File.LOOP],
|
call(('ffmpeg', '-i', file_dict[File.LOOP],
|
||||||
'-i', FILES[Stream.AUDIO],
|
'-i', file_dict[Stream.AUDIO],
|
||||||
'-map', '0:v:0', '-map', '1:a:0',
|
'-map', '0:v:0', '-map', '1:a:0',
|
||||||
'-c', 'copy', FILES[File.OUTPUT]),
|
'-c', 'copy', file_dict[File.OUTPUT]),
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def cleanup():
|
def cleanup(file_dict, outputs):
|
||||||
for key in FILES:
|
for key in file_dict:
|
||||||
if key not in OUTPUT_KEYS:
|
if key not in outputs:
|
||||||
try:
|
try:
|
||||||
remove(FILES[key])
|
remove(file_dict[key])
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -129,10 +131,6 @@ def yes_no_question(question, default):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# clean up on exit
|
|
||||||
register(cleanup)
|
|
||||||
|
|
||||||
|
|
||||||
# parse arguments
|
# parse arguments
|
||||||
parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.')
|
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.')
|
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
|
URL = args.url
|
||||||
|
|
||||||
|
|
||||||
|
# clean up on exit
|
||||||
|
register(partial(cleanup, FILES, OUTPUT_KEYS))
|
||||||
|
|
||||||
|
|
||||||
# fetch video title if no filename was specified
|
# fetch video title if no filename was specified
|
||||||
if args.output is None:
|
if args.output is None:
|
||||||
FILES[File.OUTPUT] = check_output(('youtube-dl', '--get-title', args.url)).decode('utf-8').strip()
|
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])
|
remove(FILES[File.OUTPUT])
|
||||||
|
|
||||||
# download streams and update FILE dict with extensions
|
# download streams and update FILE dict with extensions
|
||||||
download_audio_stream()
|
download_audio_stream(URL, FILES)
|
||||||
download_video_stream()
|
download_video_stream(URL, FILES)
|
||||||
read_extensions()
|
read_extensions(FILES)
|
||||||
|
|
||||||
# get stream lengths via ffprobe
|
# get stream lengths via ffprobe
|
||||||
audioData= get_duration(get_command_stderr(('ffprobe', FILES[Stream.AUDIO]))).split(':')
|
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(shorterFile), file=f)
|
||||||
print("file '{}'".format(FILES[File.FRACTION]), file=f)
|
print("file '{}'".format(FILES[File.FRACTION]), file=f)
|
||||||
|
|
||||||
loop_shorter_stream()
|
loop_shorter_stream(FILES, shorter, shorterFile, timesLoop_fraction)
|
||||||
mux_streams()
|
mux_streams(FILES)
|
||||||
|
Loading…
Reference in New Issue
Block a user