2 # -*- coding: utf-8 -*-
4 from __future__ import absolute_import
14 class PostProcessor(object):
15 """Post Processor class.
17 PostProcessor objects can be added to downloaders with their
18 add_post_processor() method. When the downloader has finished a
19 successful download, it will take its internal chain of PostProcessors
20 and start calling the run() method on each one of them, first with
21 an initial argument and then with the returned value of the previous
24 The chain will be stopped if one of them ever returns None or the end
25 of the chain is reached.
27 PostProcessor objects follow a "mutual registration" process similar
28 to InfoExtractor objects.
33 def __init__(self, downloader=None):
34 self._downloader = downloader
36 def set_downloader(self, downloader):
37 """Sets the downloader for this PP."""
38 self._downloader = downloader
40 def run(self, information):
41 """Run the PostProcessor.
43 The "information" argument is a dictionary like the ones
44 composed by InfoExtractors. The only difference is that this
45 one has an extra field called "filepath" that points to the
48 When this method returns None, the postprocessing chain is
49 stopped. However, this method may return an information
50 dictionary that will be passed to the next postprocessing
51 object in the chain. It can be the one it received after
54 In addition, this method may raise a PostProcessingError
55 exception that will be taken into account by the downloader
58 return information # by default, do nothing
60 class AudioConversionError(BaseException):
61 def __init__(self, message):
62 self.message = message
64 class FFmpegExtractAudioPP(PostProcessor):
65 def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, keepvideo=False, nopostoverwrites=False):
66 PostProcessor.__init__(self, downloader)
67 if preferredcodec is None:
68 preferredcodec = 'best'
69 self._preferredcodec = preferredcodec
70 self._preferredquality = preferredquality
71 self._keepvideo = keepvideo
72 self._nopostoverwrites = nopostoverwrites
73 self._exes = self.detect_executables()
76 def detect_executables():
79 subprocess.Popen([exe, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
83 programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
84 return dict((program, executable(program)) for program in programs)
86 def get_audio_codec(self, path):
87 if not self._exes['ffprobe'] and not self._exes['avprobe']: return None
89 cmd = [self._exes['avprobe'] or self._exes['ffprobe'], '-show_streams', '--', encodeFilename(path)]
90 handle = subprocess.Popen(cmd, stderr=compat_subprocess_get_DEVNULL(), stdout=subprocess.PIPE)
91 output = handle.communicate()[0]
92 if handle.wait() != 0:
94 except (IOError, OSError):
97 for line in output.decode('ascii', 'ignore').split('\n'):
98 if line.startswith('codec_name='):
99 audio_codec = line.split('=')[1].strip()
100 elif line.strip() == 'codec_type=audio' and audio_codec is not None:
104 def run_ffmpeg(self, path, out_path, codec, more_opts):
105 if not self._exes['ffmpeg'] and not self._exes['avconv']:
106 raise AudioConversionError('ffmpeg or avconv not found. Please install one.')
110 acodec_opts = ['-acodec', codec]
111 cmd = ([self._exes['avconv'] or self._exes['ffmpeg'], '-y', '-i', encodeFilename(path), '-vn']
112 + acodec_opts + more_opts +
113 ['--', encodeFilename(out_path)])
114 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
115 stdout,stderr = p.communicate()
116 if p.returncode != 0:
117 msg = stderr.strip().split('\n')[-1]
118 raise AudioConversionError(msg)
120 def run(self, information):
121 path = information['filepath']
123 filecodec = self.get_audio_codec(path)
124 if filecodec is None:
125 self._downloader.to_stderr(u'WARNING: unable to obtain file audio codec with ffprobe')
129 if self._preferredcodec == 'best' or self._preferredcodec == filecodec or (self._preferredcodec == 'm4a' and filecodec == 'aac'):
130 if self._preferredcodec == 'm4a' and filecodec == 'aac':
131 # Lossless, but in another container
133 extension = self._preferredcodec
134 more_opts = [self._exes['avconv'] and '-bsf:a' or '-absf', 'aac_adtstoasc']
135 elif filecodec in ['aac', 'mp3', 'vorbis']:
136 # Lossless if possible
138 extension = filecodec
139 if filecodec == 'aac':
140 more_opts = ['-f', 'adts']
141 if filecodec == 'vorbis':
145 acodec = 'libmp3lame'
148 if self._preferredquality is not None:
149 if int(self._preferredquality) < 10:
150 more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
152 more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
154 # We convert the audio (lossy)
155 acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec]
156 extension = self._preferredcodec
158 if self._preferredquality is not None:
159 if int(self._preferredquality) < 10:
160 more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
162 more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
163 if self._preferredcodec == 'aac':
164 more_opts += ['-f', 'adts']
165 if self._preferredcodec == 'm4a':
166 more_opts += [self._exes['avconv'] and '-bsf:a' or '-absf', 'aac_adtstoasc']
167 if self._preferredcodec == 'vorbis':
169 if self._preferredcodec == 'wav':
171 more_opts += ['-f', 'wav']
173 prefix, sep, ext = path.rpartition(u'.') # not os.path.splitext, since the latter does not work on unicode in all setups
174 new_path = prefix + sep + extension
176 if self._nopostoverwrites and os.path.exists(encodeFilename(new_path)):
177 self._downloader.to_screen(u'[youtube] Post-process file %s exists, skipping' % new_path)
179 self._downloader.to_screen(u'[' + (self._exes['avconv'] and 'avconv' or 'ffmpeg') + '] Destination: ' + new_path)
180 self.run_ffmpeg(path, new_path, acodec, more_opts)
182 etype,e,tb = sys.exc_info()
183 if isinstance(e, AudioConversionError):
184 self._downloader.to_stderr(u'ERROR: audio conversion failed: ' + e.message)
186 self._downloader.to_stderr(u'ERROR: error running ' + (self._exes['avconv'] and 'avconv' or 'ffmpeg'))
189 # Try to update the date time for extracted audio file.
190 if information.get('filetime') is not None:
192 os.utime(encodeFilename(new_path), (time.time(), information['filetime']))
194 self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file')
196 if not self._keepvideo:
198 os.remove(encodeFilename(path))
199 except (IOError, OSError):
200 self._downloader.to_stderr(u'WARNING: Unable to remove downloaded video file')
203 information['filepath'] = new_path