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') FILES = {'AUDIO': 'audio', 'VIDEO': 'video'} 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(FILES['AUDIO']), URL)) call(('/usr/bin/env', 'youtube-dl', '--output', '{}.%(ext)s'.format(FILES['VIDEO']), URL)) for file in listdir(): for filename in FILES: if match('^{}.*'.format(FILES[filename]), file): FILES[filename] = file audioLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES['AUDIO']))) videoLen = getDuration(getCmdStdErr(('/usr/bin/env', 'ffprobe', FILES['VIDEO']))) print(audioLen) print(videoLen)