2017-02-09 11:16:38 +00:00
|
|
|
from subprocess import call, Popen, PIPE, check_output, DEVNULL, CalledProcessError
|
2017-01-15 20:29:09 +00:00
|
|
|
from os import listdir, remove
|
2017-02-06 11:25:48 +00:00
|
|
|
from os.path import splitext, exists, join
|
2017-01-15 17:26:07 +00:00
|
|
|
from re import match
|
2017-01-15 19:18:07 +00:00
|
|
|
from enum import Enum
|
2017-01-15 19:49:27 +00:00
|
|
|
from datetime import timedelta
|
2017-02-04 15:27:26 +00:00
|
|
|
from math import floor
|
2017-01-15 23:09:49 +00:00
|
|
|
from argparse import ArgumentParser
|
2017-02-05 21:53:08 +00:00
|
|
|
from functools import wraps
|
2017-02-06 14:43:04 +00:00
|
|
|
from tempfile import mkdtemp
|
|
|
|
from shutil import rmtree
|
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
|
2017-02-04 15:27:26 +00:00
|
|
|
FRACTION = 3
|
|
|
|
OUTPUT = 4
|
2017-01-15 20:53:36 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-04 17:38:08 +00:00
|
|
|
def call_verbose(before_message='', after_message='Done!'):
|
|
|
|
def tag(f):
|
2017-02-04 17:00:20 +00:00
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
2017-02-04 17:38:08 +00:00
|
|
|
print_opt(before_message, end='', flush=True)
|
2017-02-04 17:00:20 +00:00
|
|
|
f(*args, **kwargs)
|
2017-02-04 17:38:08 +00:00
|
|
|
print_opt(after_message)
|
2017-02-04 17:00:20 +00:00
|
|
|
return wrapper
|
2017-02-04 17:38:08 +00:00
|
|
|
return tag
|
2017-02-04 17:00:20 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-01-15 22:53:48 +00:00
|
|
|
def print_opt(*args, **kwargs):
|
|
|
|
if VERBOSE:
|
|
|
|
print(*args, **kwargs)
|
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
def run(url, files_dict, directory):
|
|
|
|
# download streams and update FILE dict with extensions
|
|
|
|
download_audio_stream(url, files_dict)
|
|
|
|
download_video_stream(url, files_dict)
|
|
|
|
read_extensions(files_dict, directory)
|
2017-01-15 19:06:19 +00:00
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
# get stream lengths via ffprobe
|
|
|
|
audioLen = get_length(files_dict[Stream.AUDIO])
|
|
|
|
videoLen = get_length(files_dict[Stream.VIDEO])
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
# decide which stream needs some looping
|
|
|
|
longer = audioLen if audioLen > videoLen else videoLen
|
|
|
|
shorter = audioLen if audioLen < videoLen else videoLen
|
|
|
|
shorterFile = files_dict[Stream.AUDIO] if audioLen < videoLen else files_dict[Stream.VIDEO]
|
|
|
|
files_dict[File.LOOP] += splitext(shorterFile)[1]
|
|
|
|
files_dict[File.FRACTION] += splitext(shorterFile)[1]
|
|
|
|
|
|
|
|
# calculate how many times to loop
|
|
|
|
times = longer.total_seconds() / shorter.total_seconds()
|
|
|
|
timesLoop_base = floor(times)
|
|
|
|
timesLoop_fraction = times % 1
|
|
|
|
|
|
|
|
# write concat helper file for ffmpeg
|
|
|
|
with open(files_dict[File.LIST], 'w') as f:
|
|
|
|
for i in range(timesLoop_base):
|
|
|
|
print("file '{}'".format(shorterFile), file=f)
|
|
|
|
print("file '{}'".format(files_dict[File.FRACTION]), file=f)
|
|
|
|
|
|
|
|
# loop & mux streams
|
|
|
|
loop_shorter_stream(files_dict, shorter, shorterFile, timesLoop_fraction)
|
|
|
|
mux_streams(files_dict)
|
2017-01-15 19:06:19 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-04 17:38:08 +00:00
|
|
|
@call_verbose(before_message='Downloading audio stream... ')
|
2017-02-05 16:00:08 +00:00
|
|
|
def download_audio_stream(url, file_dict):
|
2017-02-03 21:54:09 +00:00
|
|
|
call(('youtube-dl', '--ignore-config',
|
|
|
|
'--extract-audio',
|
2017-02-05 16:00:08 +00:00
|
|
|
'--output', '{}.%(ext)s'.format(file_dict[Stream.AUDIO]),
|
|
|
|
url),
|
2017-01-15 22:43:21 +00:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
2017-02-04 17:00:20 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-04 17:38:08 +00:00
|
|
|
@call_verbose(before_message='Downloading video stream... ')
|
2017-02-05 16:00:08 +00:00
|
|
|
def download_video_stream(url, file_dict):
|
2017-02-03 21:54:09 +00:00
|
|
|
call(('youtube-dl', '--ignore-config',
|
2017-02-05 16:00:08 +00:00
|
|
|
'--output', '{}.%(ext)s'.format(file_dict[Stream.VIDEO]),
|
|
|
|
url),
|
2017-01-15 22:43:21 +00:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
2017-01-15 21:28:37 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-05 21:53:08 +00:00
|
|
|
def read_extensions(file_dict, directory):
|
|
|
|
for file in listdir(directory):
|
2017-02-05 16:00:08 +00:00
|
|
|
for filename in file_dict:
|
2017-02-06 11:25:48 +00:00
|
|
|
fullname = join(directory, file)
|
2017-02-05 21:53:08 +00:00
|
|
|
if match('^{}.*'.format(file_dict[filename]), fullname):
|
|
|
|
file_dict[filename] = fullname
|
2017-01-15 21:28:37 +00:00
|
|
|
|
2017-02-04 21:16:55 +00:00
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
def get_length(file):
|
|
|
|
data = get_duration(get_command_stderr(('ffprobe', file))).split(':')
|
|
|
|
return timedelta(hours=float(data[0]), minutes=float(data[1]), seconds=float(data[2]))
|
|
|
|
|
|
|
|
|
|
|
|
def get_command_stderr(command):
|
|
|
|
process = Popen(command, stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = process.communicate()
|
|
|
|
return err
|
|
|
|
|
|
|
|
|
|
|
|
def get_duration(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-02-04 21:16:55 +00:00
|
|
|
@call_verbose(before_message='Looping shorter stream... ')
|
2017-02-05 16:00:08 +00:00
|
|
|
def loop_shorter_stream(file_dict, shorter, shorter_file, loop_fraction):
|
|
|
|
# prepare last fractional loop
|
|
|
|
call(('ffmpeg', '-i', shorter_file, '-t', str(loop_fraction * shorter.total_seconds()), file_dict[File.FRACTION]),
|
2017-02-04 21:16:55 +00:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
|
2017-02-05 16:00:08 +00:00
|
|
|
# concat them
|
2017-02-05 21:53:08 +00:00
|
|
|
call(('ffmpeg', '-f', 'concat', '-safe', '0', '-i', file_dict[File.LIST],
|
2017-02-05 16:00:08 +00:00
|
|
|
'-c', 'copy', file_dict[File.LOOP]),
|
2017-02-04 21:16:55 +00:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
|
|
|
|
|
|
|
|
@call_verbose(before_message='Muxing streams... ')
|
2017-02-05 16:00:08 +00:00
|
|
|
def mux_streams(file_dict):
|
|
|
|
call(('ffmpeg', '-i', file_dict[File.LOOP],
|
|
|
|
'-i', file_dict[Stream.AUDIO],
|
2017-02-04 21:16:55 +00:00
|
|
|
'-map', '0:v:0', '-map', '1:a:0',
|
2017-02-05 16:00:08 +00:00
|
|
|
'-c', 'copy', file_dict[File.OUTPUT]),
|
2017-02-04 21:16:55 +00:00
|
|
|
stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
|
|
|
|
|
2017-02-09 11:16:38 +00:00
|
|
|
@call_verbose(before_message='Checking your system for youtube-dl and ffmpeg... ', after_message='Found both!')
|
|
|
|
def check_for_ytdl_and_ffmpeg():
|
|
|
|
error_str = '\nNo {} found in PATH! coub-dl requires youtube-dl & ffmpeg.'
|
|
|
|
ytdl_found = False
|
|
|
|
ffmpeg_found = False
|
|
|
|
try:
|
|
|
|
check_output(('youtube-dl', '--version'))
|
|
|
|
ytdl_found = True
|
|
|
|
except (CalledProcessError, FileNotFoundError):
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
check_output(('ffmpeg', '-version'))
|
|
|
|
ffmpeg_found = True
|
|
|
|
except (CalledProcessError, FileNotFoundError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not ytdl_found and not ffmpeg_found:
|
|
|
|
print_opt(error_str.format('youtube-dl nor ffmpeg'))
|
|
|
|
elif not ytdl_found:
|
|
|
|
print_opt(error_str.format('youtube-dl'))
|
|
|
|
elif not ffmpeg_found:
|
|
|
|
print_opt(error_str.format('ffmpeg'))
|
2017-02-10 19:11:08 +00:00
|
|
|
|
|
|
|
if not ytdl_found or not ffmpeg_found: exit()
|
2017-02-09 11:16:38 +00:00
|
|
|
|
|
|
|
|
2017-02-06 11:25:48 +00:00
|
|
|
def determine_output_filename(url, user_supplied, extension, files_dict):
|
|
|
|
if user_supplied is None:
|
|
|
|
files_dict[File.OUTPUT] = check_output(('youtube-dl', '--get-title', url)).decode('utf-8').strip()
|
|
|
|
else:
|
|
|
|
files_dict[File.OUTPUT] = user_supplied
|
|
|
|
files_dict[File.OUTPUT] += extension
|
|
|
|
|
|
|
|
|
2017-02-10 19:11:08 +00:00
|
|
|
def build_default_files_dict():
|
|
|
|
return {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
|
|
|
File.LIST: 'list.txt', File.LOOP: 'loop', File.FRACTION: 'fraction',
|
|
|
|
File.OUTPUT: ''}, [File.OUTPUT]
|
|
|
|
|
|
|
|
|
2017-02-06 11:25:48 +00:00
|
|
|
def parse_cmd_arguments():
|
|
|
|
parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.')
|
2017-02-07 00:17:24 +00:00
|
|
|
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')
|
2017-02-06 11:25:48 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.extension = '.' + args.extension
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
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-02-06 14:43:04 +00:00
|
|
|
# tempfile.TemporaryDirectory replacement to provide backwards compatibility
|
|
|
|
class temporary_directory:
|
|
|
|
def __enter__(self):
|
|
|
|
self.name = mkdtemp()
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
rmtree(self.name)
|
|
|
|
|
|
|
|
|
2017-01-15 19:14:01 +00:00
|
|
|
|
2017-02-05 21:53:08 +00:00
|
|
|
if __name__ == '__main__':
|
2017-02-06 11:25:48 +00:00
|
|
|
args = parse_cmd_arguments()
|
2017-02-05 21:53:08 +00:00
|
|
|
VERBOSE = False if args.nonverbose else True
|
2017-02-05 16:00:08 +00:00
|
|
|
|
2017-02-09 11:16:38 +00:00
|
|
|
check_for_ytdl_and_ffmpeg()
|
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
# create dict that contains files used
|
2017-02-10 19:11:08 +00:00
|
|
|
FILES, OUTPUT_KEYS = build_default_files_dict()
|
2017-02-05 21:53:08 +00:00
|
|
|
URL = args.url
|
2017-01-15 22:43:21 +00:00
|
|
|
|
2017-02-06 11:25:48 +00:00
|
|
|
determine_output_filename(URL, args.output, args.extension, FILES)
|
2017-02-05 21:53:08 +00:00
|
|
|
|
|
|
|
# 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_opt('Exiting!')
|
|
|
|
exit()
|
|
|
|
else:
|
|
|
|
remove(FILES[File.OUTPUT])
|
|
|
|
|
2017-02-06 12:10:47 +00:00
|
|
|
# create temporary directory to work in
|
2017-02-06 14:43:04 +00:00
|
|
|
with temporary_directory() as dir:
|
2017-02-06 11:25:48 +00:00
|
|
|
# update temporary file locations in FILES dict
|
2017-02-06 12:10:47 +00:00
|
|
|
for key in {key: FILES[key] for key in FILES if key not in OUTPUT_KEYS}:
|
|
|
|
FILES[key] = join(dir, FILES[key])
|
2017-02-05 21:53:08 +00:00
|
|
|
|
|
|
|
run(URL, FILES, dir)
|