made interaction with user more friendly
This commit is contained in:
parent
84e9340b56
commit
72910c3367
63
coub-dl.py
63
coub-dl.py
@ -1,6 +1,6 @@
|
||||
from subprocess import call, Popen, PIPE, check_output
|
||||
from subprocess import call, Popen, PIPE, check_output, DEVNULL
|
||||
from os import listdir, remove
|
||||
from os.path import splitext
|
||||
from os.path import splitext, exists
|
||||
from re import match
|
||||
from sys import argv
|
||||
from enum import Enum
|
||||
@ -32,11 +32,17 @@ def getDuration(ffprobe_output):
|
||||
return duration
|
||||
|
||||
def downloadStreams():
|
||||
print('Downloading audio stream... ', end='', flush=True)
|
||||
call(('/usr/bin/env', 'youtube-dl', '--extract-audio',
|
||||
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
|
||||
URL))
|
||||
URL),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print('Done!')
|
||||
print('Downloading video stream... ', end='', flush=True)
|
||||
call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
|
||||
URL))
|
||||
URL),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print('Done!')
|
||||
|
||||
def readExtensions():
|
||||
for file in listdir():
|
||||
@ -44,6 +50,28 @@ def readExtensions():
|
||||
if match('^{}.*'.format(FILES[filename]), file):
|
||||
FILES[filename] = file
|
||||
|
||||
def yes_no_question(question, default):
|
||||
valid = {"yes": True, "y": True, "ye": True,
|
||||
"no": False, "n": False}
|
||||
if default is None:
|
||||
prompt = " [y/n] "
|
||||
elif default == "yes":
|
||||
prompt = " [Y/n] "
|
||||
elif default == "no":
|
||||
prompt = " [y/N] "
|
||||
else:
|
||||
raise ValueError("Invalid default answer: {}!".format(default))
|
||||
|
||||
while True:
|
||||
print(question + prompt)
|
||||
choice = input().lower()
|
||||
if default is not None and choice == '':
|
||||
return valid[default]
|
||||
elif choice in valid:
|
||||
return valid[choice]
|
||||
else:
|
||||
print("Please respond with 'yes'(y) or 'no'(n)!")
|
||||
|
||||
|
||||
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
||||
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'}
|
||||
@ -51,6 +79,19 @@ OUTPUT_KEYS = [File.OUTPUT]
|
||||
URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if ''
|
||||
|
||||
|
||||
# fetch video title
|
||||
FILES[File.OUTPUT] = check_output(('/usr/bin/env', 'youtube-dl', '--get-title', argv[1])).decode('utf-8').strip() + '.mp4'
|
||||
|
||||
# ask what to do if output exists
|
||||
if exists(FILES[File.OUTPUT]):
|
||||
answer = yes_no_question('A file named "{}" already exists! Overwrite?'.format(FILES[File.OUTPUT]), default='no')
|
||||
if not answer:
|
||||
print('Exiting!')
|
||||
exit()
|
||||
else:
|
||||
remove(FILES[File.OUTPUT])
|
||||
|
||||
# download streams and update FILE dict with extensions
|
||||
downloadStreams()
|
||||
readExtensions()
|
||||
|
||||
@ -74,16 +115,18 @@ with open(FILES[File.LIST], 'w') as f:
|
||||
for i in range(timesLoop):
|
||||
print("file '{}'".format(shorterFile), file=f)
|
||||
|
||||
|
||||
# fetch video title
|
||||
FILES[File.OUTPUT] = check_output(('/usr/bin/env', 'youtube-dl', '--get-title', argv[1])).decode('utf-8').strip() + '.mp4'
|
||||
|
||||
# loop & mux
|
||||
call(('/usr/bin/env', 'ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP]))
|
||||
print('Looping shorter stream... ', end='', flush=True)
|
||||
call(('/usr/bin/env', 'ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print('Done!')
|
||||
print('Muxing streams... ', end='', flush=True)
|
||||
call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP],
|
||||
'-i', FILES[Stream.AUDIO],
|
||||
'-map', '0', '-map', '1',
|
||||
'-c', 'copy', FILES[File.OUTPUT]))
|
||||
'-c', 'copy', FILES[File.OUTPUT]),
|
||||
stdout=DEVNULL, stderr=DEVNULL)
|
||||
print('Done!')
|
||||
|
||||
# cleanup
|
||||
for key in FILES:
|
||||
|
Loading…
Reference in New Issue
Block a user