general refactor & commenting
This commit is contained in:
parent
504a1f774a
commit
84e9340b56
31
coub-dl.py
31
coub-dl.py
@ -31,6 +31,19 @@ def getDuration(ffprobe_output):
|
|||||||
raise ValueError('Cannot process ffprobe output!')
|
raise ValueError('Cannot process ffprobe output!')
|
||||||
return duration
|
return duration
|
||||||
|
|
||||||
|
def downloadStreams():
|
||||||
|
call(('/usr/bin/env', 'youtube-dl', '--extract-audio',
|
||||||
|
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
|
||||||
|
URL))
|
||||||
|
call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
|
||||||
|
URL))
|
||||||
|
|
||||||
|
def readExtensions():
|
||||||
|
for file in listdir():
|
||||||
|
for filename in FILES:
|
||||||
|
if match('^{}.*'.format(FILES[filename]), file):
|
||||||
|
FILES[filename] = file
|
||||||
|
|
||||||
|
|
||||||
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
|
||||||
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'}
|
File.LIST: 'list.txt', File.LOOP: 'loop', File.OUTPUT: 'output.mp4'}
|
||||||
@ -38,23 +51,17 @@ OUTPUT_KEYS = [File.OUTPUT]
|
|||||||
URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if ''
|
URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if ''
|
||||||
|
|
||||||
|
|
||||||
call(('/usr/bin/env', 'youtube-dl', '--extract-audio',
|
downloadStreams()
|
||||||
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
|
readExtensions()
|
||||||
URL))
|
|
||||||
call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
|
|
||||||
URL))
|
|
||||||
|
|
||||||
for file in listdir():
|
|
||||||
for filename in FILES:
|
|
||||||
if match('^{}.*'.format(FILES[filename]), file):
|
|
||||||
FILES[filename] = file
|
|
||||||
|
|
||||||
|
# get stream lengths via ffprobe
|
||||||
audioData= getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.AUDIO]))).split(':')
|
audioData= getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.AUDIO]))).split(':')
|
||||||
videoData = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.VIDEO]))).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]))
|
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]))
|
videoLen = timedelta(hours=float(videoData[0]), minutes=float(videoData[1]), seconds=float(videoData[2]))
|
||||||
|
|
||||||
|
# decide which stream needs some looping
|
||||||
longer = audioLen if audioLen > videoLen else videoLen
|
longer = audioLen if audioLen > videoLen else videoLen
|
||||||
shorter = audioLen if audioLen < videoLen else videoLen
|
shorter = audioLen if audioLen < videoLen else videoLen
|
||||||
shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO]
|
shorterFile = FILES[Stream.AUDIO] if audioLen < videoLen else FILES[Stream.VIDEO]
|
||||||
@ -62,19 +69,23 @@ FILES[File.LOOP] += splitext(shorterFile)[1]
|
|||||||
|
|
||||||
timesLoop = ceil(longer.seconds / shorter.seconds)
|
timesLoop = ceil(longer.seconds / shorter.seconds)
|
||||||
|
|
||||||
|
# write concat helper file for ffmpeg
|
||||||
with open(FILES[File.LIST], 'w') as f:
|
with open(FILES[File.LIST], 'w') as f:
|
||||||
for i in range(timesLoop):
|
for i in range(timesLoop):
|
||||||
print("file '{}'".format(shorterFile), file=f)
|
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'
|
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]))
|
call(('/usr/bin/env', 'ffmpeg', '-f', 'concat', '-i', FILES[File.LIST], '-c', 'copy', FILES[File.LOOP]))
|
||||||
call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP],
|
call(('/usr/bin/env', 'ffmpeg', '-i', FILES[File.LOOP],
|
||||||
'-i', FILES[Stream.AUDIO],
|
'-i', FILES[Stream.AUDIO],
|
||||||
'-map', '0', '-map', '1',
|
'-map', '0', '-map', '1',
|
||||||
'-c', 'copy', FILES[File.OUTPUT]))
|
'-c', 'copy', FILES[File.OUTPUT]))
|
||||||
|
|
||||||
|
# cleanup
|
||||||
for key in FILES:
|
for key in FILES:
|
||||||
if key not in OUTPUT_KEYS:
|
if key not in OUTPUT_KEYS:
|
||||||
remove(FILES[key])
|
remove(FILES[key])
|
||||||
|
Loading…
Reference in New Issue
Block a user