refactored some parts of code
This commit is contained in:
parent
6c5169573d
commit
0543e295ff
72
coub-dl.py
72
coub-dl.py
@ -10,18 +10,6 @@ from functools import wraps
|
||||
|
||||
|
||||
|
||||
# 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.')
|
||||
parser.add_argument('-o', '--output', default=None, help='Specify name of the output file (use -e for extension).')
|
||||
parser.add_argument('-e', '--extension', default='mp4', help='Set the container to use for the output.')
|
||||
parser.add_argument('url', type=str, help='The URL of the site containing the video to download.')
|
||||
|
||||
args = parser.parse_args()
|
||||
args.extension = '.' + args.extension
|
||||
VERBOSE = False if args.nonverbose else True
|
||||
|
||||
|
||||
class Stream(Enum):
|
||||
AUDIO = 1
|
||||
VIDEO = 2
|
||||
@ -32,6 +20,7 @@ class File(Enum):
|
||||
FRACTION = 3
|
||||
OUTPUT = 4
|
||||
|
||||
|
||||
def call_verbose(before_message='', after_message='Done!'):
|
||||
def tag(f):
|
||||
@wraps(f)
|
||||
@ -42,15 +31,18 @@ def call_verbose(before_message='', after_message='Done!'):
|
||||
return wrapper
|
||||
return tag
|
||||
|
||||
|
||||
def print_opt(*args, **kwargs):
|
||||
if VERBOSE:
|
||||
print(*args, **kwargs)
|
||||
|
||||
|
||||
def getCmdStdErr(command):
|
||||
process = Popen(command, stderr=PIPE, stdout=PIPE)
|
||||
out, err = process.communicate()
|
||||
return err
|
||||
|
||||
|
||||
def getDuration(ffprobe_output):
|
||||
durationPattern = r'.*Duration:\s(.+),\sstart.*'
|
||||
regex = match(durationPattern, str(ffprobe_output))
|
||||
@ -59,6 +51,7 @@ def getDuration(ffprobe_output):
|
||||
raise ValueError('Cannot process ffprobe output!')
|
||||
return duration
|
||||
|
||||
|
||||
@call_verbose(before_message='Downloading audio stream... ')
|
||||
def download_audio_stream():
|
||||
call(('youtube-dl', '--ignore-config',
|
||||
@ -67,6 +60,7 @@ def download_audio_stream():
|
||||
URL),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
@call_verbose(before_message='Downloading video stream... ')
|
||||
def download_video_stream():
|
||||
call(('youtube-dl', '--ignore-config',
|
||||
@ -74,12 +68,33 @@ def download_video_stream():
|
||||
URL),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
def readExtensions():
|
||||
for file in listdir():
|
||||
for filename in FILES:
|
||||
if match('^{}.*'.format(FILES[filename]), file):
|
||||
FILES[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]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
call(('ffmpeg', '-i', shorterFile, '-t', str(timesLoop_fraction * shorter.total_seconds()), FILES[File.FRACTION]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
@call_verbose(before_message='Muxing streams... ')
|
||||
def mux_streams():
|
||||
call(('ffmpeg', '-i', FILES[File.LOOP],
|
||||
'-i', FILES[Stream.AUDIO],
|
||||
'-map', '0:v:0', '-map', '1:a:0',
|
||||
'-c', 'copy', FILES[File.OUTPUT]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
|
||||
def yes_no_question(question, default):
|
||||
valid = {"yes": True, "y": True, "ye": True,
|
||||
"no": False, "n": False}
|
||||
@ -103,6 +118,19 @@ def yes_no_question(question, default):
|
||||
print("Please respond with 'yes'(y) or 'no'(n)!")
|
||||
|
||||
|
||||
|
||||
# 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.')
|
||||
parser.add_argument('-o', '--output', default=None, help='Specify name of the output file (use -e for extension).')
|
||||
parser.add_argument('-e', '--extension', default='mp4', help='Set the container to use for the output.')
|
||||
parser.add_argument('url', type=str, help='The URL of the site containing the video to download.')
|
||||
|
||||
args = parser.parse_args()
|
||||
args.extension = '.' + args.extension
|
||||
VERBOSE = False if args.nonverbose else True
|
||||
|
||||
|
||||
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
||||
File.LIST: 'list.txt', File.LOOP: 'loop', File.FRACTION: 'fraction',
|
||||
File.OUTPUT: 'output'+args.extension}
|
||||
@ -155,24 +183,8 @@ with open(FILES[File.LIST], 'w') as f:
|
||||
print("file '{}'".format(shorterFile), file=f)
|
||||
print("file '{}'".format(FILES[File.FRACTION]), file=f)
|
||||
|
||||
# Cut last loop accurrate
|
||||
call(('ffmpeg', '-i', shorterFile, '-t', str(timesLoop_fraction*shorter.total_seconds()), FILES[File.FRACTION]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
|
||||
# loop shorter stream
|
||||
print_opt('Looping shorter stream... ', end='', flush=True)
|
||||
call(('ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print_opt('Done!')
|
||||
|
||||
# mux with audio
|
||||
print_opt('Muxing streams... ', end='', flush=True)
|
||||
call(('ffmpeg', '-i', FILES[File.LOOP],
|
||||
'-i', FILES[Stream.AUDIO],
|
||||
'-map', '0:v:0', '-map', '1:a:0',
|
||||
'-c', 'copy', FILES[File.OUTPUT]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print_opt('Done!')
|
||||
loop_shorter_stream()
|
||||
mux_streams()
|
||||
|
||||
# cleanup
|
||||
for key in FILES:
|
||||
|
Loading…
Reference in New Issue
Block a user