From 713a8947043e4e7668735fdf09e16e23b2390fb9 Mon Sep 17 00:00:00 2001 From: danut007ro Date: Tue, 9 Oct 2012 21:38:56 +0300 Subject: [PATCH] avconv output --- youtube_dl/PostProcessor.py | 88 ++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/youtube_dl/PostProcessor.py b/youtube_dl/PostProcessor.py index f2e2aa1fa..c57b9b609 100644 --- a/youtube_dl/PostProcessor.py +++ b/youtube_dl/PostProcessor.py @@ -71,14 +71,13 @@ class FFmpegExtractAudioPP(PostProcessor): @staticmethod def detect_executables(): - def executable(exe): - try: - subprocess.check_output([exe, '-version']) - except OSError: - return False - return exe - programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe'] - return dict((program, executable(program)) for program in programs) + available = {'avprobe' : False, 'avconv' : False, 'ffmpeg' : False, 'ffprobe' : False} + for path in os.environ["PATH"].split(os.pathsep): + for program in available.keys(): + exe_file = os.path.join(path, program) + if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): + available[program] = exe_file + return available def get_audio_codec(self, path): if not self._exes['ffprobe'] and not self._exes['avprobe']: return None @@ -90,15 +89,25 @@ class FFmpegExtractAudioPP(PostProcessor): return None except (IOError, OSError): return None - audio_codec = None + codec = None + duration = None for line in output.split('\n'): if line.startswith('codec_name='): - audio_codec = line.split('=')[1].strip() - elif line.strip() == 'codec_type=audio' and audio_codec is not None: - return audio_codec - return None + codec = line.split('=')[1].strip() + elif line.startswith('duration='): + duration = line.split('=')[1].strip() + try: + duration = float(duration) + except: + duration = None + elif line.strip() == '[/STREAM]' and codec is not None: + break + return { + 'codec': codec, + 'duration': duration + } - def run_ffmpeg(self, path, out_path, codec, more_opts): + def run_ffmpeg(self, path, out_path, codec, more_opts, duration): if not self._exes['ffmpeg'] and not self._exes['avconv']: raise AudioConversionError('ffmpeg or avconv not found. Please install one.') if codec is None: @@ -108,16 +117,55 @@ class FFmpegExtractAudioPP(PostProcessor): cmd = ([self._exes['avconv'] or self._exes['ffmpeg'], '-y', '-i', encodeFilename(path), '-vn'] + acodec_opts + more_opts + ['--', encodeFilename(out_path)]) - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout,stderr = p.communicate() + start = time.time() + # open process redirecting stderr to stdout + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) + import fcntl + import errno + import select + # entire captured output + p_output = '' + # size= 765kB time=243.67 bitrate= 25.7kbits/s + reo = re.compile("""size=\s*(?P\S+) # size + \stime=(?P