From 7f3a8f94b22369a5db81a02cab295499d9b183ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 15 Jan 2017 20:06:19 +0100 Subject: [PATCH] implemented getting stream lengths --- coub-dl.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/coub-dl.py b/coub-dl.py index 5ba74fd..b64fb9c 100644 --- a/coub-dl.py +++ b/coub-dl.py @@ -1,15 +1,44 @@ -from subprocess import call, check_output, STDOUT +from subprocess import call, Popen, PIPE +from os import listdir from re import match from sys import argv + + +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 + + argv.append('https://coub.com/view/aeeuu') AUDIO_FILE = 'audio' VIDEO_FILE = 'video' -URL = argv[1] if len(argv) > 0 else '' +URL = argv[1] if len(argv) > 0 else '' # youtube-dl error message will be shown if '' call(('/usr/bin/env', 'youtube-dl', '--extract-audio', '--output', '{}.%(ext)s'.format(AUDIO_FILE), URL)) call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(VIDEO_FILE), URL)) + +for file in listdir(): + if match('^{}.*'.format(AUDIO_FILE), file): + AUDIO_FILE = file + if match('^{}.*'.format(VIDEO_FILE), file): + VIDEO_FILE = file + +audioLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', AUDIO_FILE))) +videoLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', VIDEO_FILE))) + +print(audioLen) +print(videoLen)