Merge remote-tracking branch 'Asido/master'
[youtube-dl] / youtube_dl / PostProcessor.py
index b4262f9e4f18f84466f51c8fe8108a60fa6b34b4..0501cc7f6075cc4b99264cae2f84b57691fb1d3a 100644 (file)
@@ -71,13 +71,14 @@ class FFmpegExtractAudioPP(PostProcessor):
 
        @staticmethod
        def detect_executables():
-               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 executable(exe):
+                       try:
+                               subprocess.Popen([exe, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
+                       except OSError:
+                               return False
+                       return exe
+               programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
+               return dict((program, executable(program)) for program in programs)
 
        def get_audio_codec(self, path):
                if not self._exes['ffprobe'] and not self._exes['avprobe']: return None
@@ -142,14 +143,20 @@ class FFmpegExtractAudioPP(PostProcessor):
                                extension = 'mp3'
                                more_opts = []
                                if self._preferredquality is not None:
-                                       more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality]
+                                       if int(self._preferredquality) < 10:
+                                               more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
+                                       else:
+                                               more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
                else:
                        # We convert the audio (lossy)
                        acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec]
                        extension = self._preferredcodec
                        more_opts = []
                        if self._preferredquality is not None:
-                               more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality]
+                               if int(self._preferredquality) < 10:
+                                       more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
+                               else:
+                                       more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
                        if self._preferredcodec == 'aac':
                                more_opts += ['-f', 'adts']
                        if self._preferredcodec == 'm4a':
@@ -162,7 +169,7 @@ class FFmpegExtractAudioPP(PostProcessor):
 
                prefix, sep, ext = path.rpartition(u'.') # not os.path.splitext, since the latter does not work on unicode in all setups
                new_path = prefix + sep + extension
-               self._downloader.to_screen(u'[' + self._exes['avconv'] and 'avconv' or 'ffmpeg' + '] Destination: ' + new_path)
+               self._downloader.to_screen(u'[' + (self._exes['avconv'] and 'avconv' or 'ffmpeg') + '] Destination: ' + new_path)
                try:
                        self.run_ffmpeg(path, new_path, acodec, more_opts)
                except:
@@ -170,7 +177,7 @@ class FFmpegExtractAudioPP(PostProcessor):
                        if isinstance(e, AudioConversionError):
                                self._downloader.to_stderr(u'ERROR: audio conversion failed: ' + e.message)
                        else:
-                               self._downloader.to_stderr(u'ERROR: error running ' + self._exes['avconv'] and 'avconv' or 'ffmpeg')
+                               self._downloader.to_stderr(u'ERROR: error running ' + (self._exes['avconv'] and 'avconv' or 'ffmpeg'))
                        return None
 
                # Try to update the date time for extracted audio file.