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):
|
class Stream(Enum):
|
||||||
AUDIO = 1
|
AUDIO = 1
|
||||||
VIDEO = 2
|
VIDEO = 2
|
||||||
@ -32,6 +20,7 @@ class File(Enum):
|
|||||||
FRACTION = 3
|
FRACTION = 3
|
||||||
OUTPUT = 4
|
OUTPUT = 4
|
||||||
|
|
||||||
|
|
||||||
def call_verbose(before_message='', after_message='Done!'):
|
def call_verbose(before_message='', after_message='Done!'):
|
||||||
def tag(f):
|
def tag(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
@ -42,15 +31,18 @@ def call_verbose(before_message='', after_message='Done!'):
|
|||||||
return wrapper
|
return wrapper
|
||||||
return tag
|
return tag
|
||||||
|
|
||||||
|
|
||||||
def print_opt(*args, **kwargs):
|
def print_opt(*args, **kwargs):
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print(*args, **kwargs)
|
print(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def getCmdStdErr(command):
|
def getCmdStdErr(command):
|
||||||
process = Popen(command, stderr=PIPE, stdout=PIPE)
|
process = Popen(command, stderr=PIPE, stdout=PIPE)
|
||||||
out, err = process.communicate()
|
out, err = process.communicate()
|
||||||
return err
|
return err
|
||||||
|
|
||||||
|
|
||||||
def getDuration(ffprobe_output):
|
def getDuration(ffprobe_output):
|
||||||
durationPattern = r'.*Duration:\s(.+),\sstart.*'
|
durationPattern = r'.*Duration:\s(.+),\sstart.*'
|
||||||
regex = match(durationPattern, str(ffprobe_output))
|
regex = match(durationPattern, str(ffprobe_output))
|
||||||
@ -59,6 +51,7 @@ def getDuration(ffprobe_output):
|
|||||||
raise ValueError('Cannot process ffprobe output!')
|
raise ValueError('Cannot process ffprobe output!')
|
||||||
return duration
|
return duration
|
||||||
|
|
||||||
|
|
||||||
@call_verbose(before_message='Downloading audio stream... ')
|
@call_verbose(before_message='Downloading audio stream... ')
|
||||||
def download_audio_stream():
|
def download_audio_stream():
|
||||||
call(('youtube-dl', '--ignore-config',
|
call(('youtube-dl', '--ignore-config',
|
||||||
@ -67,6 +60,7 @@ def download_audio_stream():
|
|||||||
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():
|
||||||
call(('youtube-dl', '--ignore-config',
|
call(('youtube-dl', '--ignore-config',
|
||||||
@ -74,12 +68,33 @@ def download_video_stream():
|
|||||||
URL),
|
URL),
|
||||||
stdout=DEVNULL, stderr=DEVNULL)
|
stdout=DEVNULL, stderr=DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def readExtensions():
|
def readExtensions():
|
||||||
for file in listdir():
|
for file in listdir():
|
||||||
for filename in FILES:
|
for filename in FILES:
|
||||||
if match('^{}.*'.format(FILES[filename]), file):
|
if match('^{}.*'.format(FILES[filename]), file):
|
||||||
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):
|
def yes_no_question(question, default):
|
||||||
valid = {"yes": True, "y": True, "ye": True,
|
valid = {"yes": True, "y": True, "ye": True,
|
||||||
"no": False, "n": False}
|
"no": False, "n": False}
|
||||||
@ -103,6 +118,19 @@ def yes_no_question(question, default):
|
|||||||
print("Please respond with 'yes'(y) or 'no'(n)!")
|
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',
|
FILES = {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',
|
||||||
File.OUTPUT: 'output'+args.extension}
|
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(shorterFile), file=f)
|
||||||
print("file '{}'".format(FILES[File.FRACTION]), file=f)
|
print("file '{}'".format(FILES[File.FRACTION]), file=f)
|
||||||
|
|
||||||
# Cut last loop accurrate
|
loop_shorter_stream()
|
||||||
call(('ffmpeg', '-i', shorterFile, '-t', str(timesLoop_fraction*shorter.total_seconds()), FILES[File.FRACTION]),
|
mux_streams()
|
||||||
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!')
|
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
for key in FILES:
|
for key in FILES:
|
||||||
|
Loading…
Reference in New Issue
Block a user