coub-dl/coub-dl.py

49 lines
1.5 KiB
Python
Raw Normal View History

2017-01-15 19:06:19 +00:00
from subprocess import call, Popen, PIPE
from os import listdir
from re import match
from sys import argv
2017-01-15 19:18:07 +00:00
from enum import Enum
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 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 19:18:07 +00:00
argv.append('https://coub.com/view/aeeuu') # TODO: remove debug line
2017-01-15 19:18:07 +00:00
FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video'}
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 19:14:01 +00:00
call(('/usr/bin/env', 'youtube-dl', '--extract-audio',
2017-01-15 19:18:07 +00:00
'--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
URL))
2017-01-15 19:18:07 +00:00
call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
URL))
2017-01-15 19:06:19 +00:00
for file in listdir():
2017-01-15 19:14:01 +00:00
for filename in FILES:
if match('^{}.*'.format(FILES[filename]), file):
FILES[filename] = file
2017-01-15 19:06:19 +00:00
2017-01-15 19:18:07 +00:00
audioLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.AUDIO])))
videoLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES[Stream.VIDEO])))
2017-01-15 19:06:19 +00:00
print(audioLen)
print(videoLen)