2017-01-15 22:43:21 +00:00
|
|
|
from subprocess import call, Popen, PIPE, check_output, DEVNULL
|
2017-01-15 20:29:09 +00:00
|
|
|
from os import listdir, remove
|
2017-01-15 22:43:21 +00:00
|
|
|
from os.path import splitext, exists
|
2017-01-15 17:26:07 +00:00
|
|
|
from re import match
|
|
|
|
from sys import argv
|
2017-01-15 19:18:07 +00:00
|
|
|
from enum import Enum
|
2017-01-15 19:49:27 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from math import ceil
|
2017-01-15 17:26:07 +00:00
|
|
|
|
2017-01-15 19:06:19 +00:00
|
|
|
|
|
|
|
|
2017-01-15 19:18:07 +00:00
|
|
|
class Stream(Enum):
|
|
|
|
AUDIO = 1
|
|
|
|
VIDEO = 2
|
|
|
|
|
2017-01-15 20:53:36 +00:00
|
|
|
class File(Enum):
|
|
|
|
LIST = 1
|
|
|
|
LOOP = 2
|
|
|
|
OUTPUT = 3
|
|
|
|
|
2017-01-15 19:06:19 +00:00
|
|
|
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))
|
|
|
|
duration = regex.groups()[0] if regex else None
|
|
|
|
if not duration:
|
|
|
|
raise ValueError('Cannot process ffprobe output!')
|
|
|
|
return duration
|
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
def downloadStreams():
|
2017-01-15 22:43:21 +00:00
|
|
|
print('Downloading audio stream... ', end='', flush=True)
|
2017-01-15 21:28:37 +00:00
|
|
|
call(('/usr/bin/env', 'youtube-dl', '--extract-audio',
|
|
|
|
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
|
2017-01-15 22:43:21 +00:00
|
|
|
URL),
|
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
print('Done!')
|
|
|
|
print('Downloading video stream... ', end='', flush=True)
|
2017-01-15 21:28:37 +00:00
|
|
|
call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
|
2017-01-15 22:43:21 +00:00
|
|
|
URL),
|
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
print('Done!')
|
2017-01-15 21:28:37 +00:00
|
|
|
|
|
|
|
def readExtensions():
|
|
|
|
for file in listdir():
|
|
|
|
for filename in FILES:
|
|
|
|
if match('^{}.*'.format(FILES[filename]), file):
|
|
|
|
FILES[filename] = file
|
|
|
|
|
2017-01-15 22:43:21 +00:00
|
|
|
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)!")
|
|
|
|
|
2017-01-15 19:06:19 +00:00
|
|
|
|
2017-01-15 20:53:36 +00:00
|
|
|
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
|
|
|
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'}
|
|
|
|
OUTPUT_KEYS = [File.OUTPUT]
|
2017-01-15 19:06:19 +00:00
|
|
|
URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if ''
|
2017-01-15 17:26:07 +00:00
|
|
|
|
2017-01-15 19:14:01 +00:00
|
|
|
|
2017-01-15 22:43:21 +00:00
|
|
|
# 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
|
2017-01-15 21:28:37 +00:00
|
|
|
downloadStreams()
|
|
|
|
readExtensions()
|
2017-01-15 19:06:19 +00:00
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
# get stream lengths via ffprobe
|
2017-01-15 19:49:27 +00:00
|
|
|
audioData= getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.AUDIO]))).split(':')
|
|
|
|
videoData = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.VIDEO]))).split(':')
|
|
|
|
|
|
|
|
audioLen = timedelta(hours=float(audioData[0]), minutes=float(audioData[1]), seconds=float(audioData[2]))
|
|
|
|
videoLen = timedelta(hours=float(videoData[0]), minutes=float(videoData[1]), seconds=float(videoData[2]))
|
2017-01-15 19:06:19 +00:00
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
# decide which stream needs some looping
|
2017-01-15 19:49:27 +00:00
|
|
|
longer = audioLen if audioLen > videoLen else videoLen
|
|
|
|
shorter = audioLen if audioLen < videoLen else videoLen
|
2017-01-15 19:59:53 +00:00
|
|
|
shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO]
|
2017-01-15 20:53:36 +00:00
|
|
|
FILES[File.LOOP] += splitext(shorterFile)[1]
|
2017-01-15 19:49:27 +00:00
|
|
|
|
|
|
|
timesLoop = ceil(longer.seconds / shorter.seconds)
|
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
# write concat helper file for ffmpeg
|
2017-01-15 20:53:36 +00:00
|
|
|
with open(FILES[File.LIST], 'w') as f:
|
2017-01-15 19:59:53 +00:00
|
|
|
for i in range(timesLoop):
|
2017-01-15 20:18:10 +00:00
|
|
|
print("file '{}'".format(shorterFile), file=f)
|
2017-01-15 19:49:27 +00:00
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
# loop & mux
|
2017-01-15 22:43:21 +00:00
|
|
|
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)
|
2017-01-15 20:53:36 +00:00
|
|
|
call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP],
|
2017-01-15 20:23:36 +00:00
|
|
|
'-i', FILES[Stream.AUDIO],
|
|
|
|
'-map', '0', '-map', '1',
|
2017-01-15 22:43:21 +00:00
|
|
|
'-c', 'copy', FILES[File.OUTPUT]),
|
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
print('Done!')
|
2017-01-15 20:29:09 +00:00
|
|
|
|
2017-01-15 21:28:37 +00:00
|
|
|
# cleanup
|
2017-01-15 20:53:36 +00:00
|
|
|
for key in FILES:
|
|
|
|
if key not in OUTPUT_KEYS:
|
|
|
|
remove(FILES[key])
|