]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/ffmpeg.py
8c5f7c43b75b17466a91a2b54c928ec8ca2298f3
[youtubedl] / youtube_dl / postprocessor / ffmpeg.py
1 import os
2 import subprocess
3 import sys
4 import time
5
6
7 from .common import AudioConversionError, PostProcessor
8
9 from ..utils import (
10 check_executable,
11 compat_subprocess_get_DEVNULL,
12 encodeArgument,
13 encodeFilename,
14 PostProcessingError,
15 prepend_extension,
16 shell_quote,
17 subtitles_filename,
18 )
19
20
21 class FFmpegPostProcessorError(PostProcessingError):
22 pass
23
24
25 class FFmpegPostProcessor(PostProcessor):
26 def __init__(self, downloader=None, deletetempfiles=False):
27 PostProcessor.__init__(self, downloader)
28 self._exes = self.detect_executables()
29 self._deletetempfiles = deletetempfiles
30
31 @staticmethod
32 def detect_executables():
33 programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
34 return dict((program, check_executable(program, ['-version'])) for program in programs)
35
36 def _get_executable(self):
37 if self._downloader.params.get('prefer_ffmpeg', False):
38 return self._exes['ffmpeg'] or self._exes['avconv']
39 else:
40 return self._exes['avconv'] or self._exes['ffmpeg']
41
42 def _uses_avconv(self):
43 return self._get_executable() == self._exes['avconv']
44
45 def run_ffmpeg_multiple_files(self, input_paths, out_path, opts):
46 if not self._get_executable():
47 raise FFmpegPostProcessorError(u'ffmpeg or avconv not found. Please install one.')
48
49 files_cmd = []
50 for path in input_paths:
51 files_cmd.extend(['-i', encodeFilename(path, True)])
52 cmd = ([self._get_executable(), '-y'] + files_cmd
53 + [encodeArgument(o) for o in opts] +
54 [encodeFilename(self._ffmpeg_filename_argument(out_path), True)])
55
56 if self._downloader.params.get('verbose', False):
57 self._downloader.to_screen(u'[debug] ffmpeg command line: %s' % shell_quote(cmd))
58 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
59 stdout, stderr = p.communicate()
60 if p.returncode != 0:
61 stderr = stderr.decode('utf-8', 'replace')
62 msg = stderr.strip().split('\n')[-1]
63 raise FFmpegPostProcessorError(msg)
64 if self._deletetempfiles:
65 for ipath in input_paths:
66 os.remove(ipath)
67
68 def run_ffmpeg(self, path, out_path, opts):
69 self.run_ffmpeg_multiple_files([path], out_path, opts)
70
71 def _ffmpeg_filename_argument(self, fn):
72 # ffmpeg broke --, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details
73 if fn.startswith(u'-'):
74 return u'./' + fn
75 return fn
76
77
78 class FFmpegExtractAudioPP(FFmpegPostProcessor):
79 def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
80 FFmpegPostProcessor.__init__(self, downloader)
81 if preferredcodec is None:
82 preferredcodec = 'best'
83 self._preferredcodec = preferredcodec
84 self._preferredquality = preferredquality
85 self._nopostoverwrites = nopostoverwrites
86
87 def get_audio_codec(self, path):
88 if not self._exes['ffprobe'] and not self._exes['avprobe']:
89 raise PostProcessingError(u'ffprobe or avprobe not found. Please install one.')
90 try:
91 cmd = [
92 self._exes['avprobe'] or self._exes['ffprobe'],
93 '-show_streams',
94 encodeFilename(self._ffmpeg_filename_argument(path), True)]
95 handle = subprocess.Popen(cmd, stderr=compat_subprocess_get_DEVNULL(), stdout=subprocess.PIPE)
96 output = handle.communicate()[0]
97 if handle.wait() != 0:
98 return None
99 except (IOError, OSError):
100 return None
101 audio_codec = None
102 for line in output.decode('ascii', 'ignore').split('\n'):
103 if line.startswith('codec_name='):
104 audio_codec = line.split('=')[1].strip()
105 elif line.strip() == 'codec_type=audio' and audio_codec is not None:
106 return audio_codec
107 return None
108
109 def run_ffmpeg(self, path, out_path, codec, more_opts):
110 if codec is None:
111 acodec_opts = []
112 else:
113 acodec_opts = ['-acodec', codec]
114 opts = ['-vn'] + acodec_opts + more_opts
115 try:
116 FFmpegPostProcessor.run_ffmpeg(self, path, out_path, opts)
117 except FFmpegPostProcessorError as err:
118 raise AudioConversionError(err.msg)
119
120 def run(self, information):
121 path = information['filepath']
122
123 filecodec = self.get_audio_codec(path)
124 if filecodec is None:
125 raise PostProcessingError(u'WARNING: unable to obtain file audio codec with ffprobe')
126
127 uses_avconv = self._uses_avconv()
128 more_opts = []
129 if self._preferredcodec == 'best' or self._preferredcodec == filecodec or (self._preferredcodec == 'm4a' and filecodec == 'aac'):
130 if filecodec == 'aac' and self._preferredcodec in ['m4a', 'best']:
131 # Lossless, but in another container
132 acodec = 'copy'
133 extension = 'm4a'
134 more_opts = ['-bsf:a' if uses_avconv else '-absf', 'aac_adtstoasc']
135 elif filecodec in ['aac', 'mp3', 'vorbis', 'opus']:
136 # Lossless if possible
137 acodec = 'copy'
138 extension = filecodec
139 if filecodec == 'aac':
140 more_opts = ['-f', 'adts']
141 if filecodec == 'vorbis':
142 extension = 'ogg'
143 else:
144 # MP3 otherwise.
145 acodec = 'libmp3lame'
146 extension = 'mp3'
147 more_opts = []
148 if self._preferredquality is not None:
149 if int(self._preferredquality) < 10:
150 more_opts += ['-q:a' if uses_avconv else '-aq', self._preferredquality]
151 else:
152 more_opts += ['-b:a' if uses_avconv else '-ab', self._preferredquality + 'k']
153 else:
154 # We convert the audio (lossy)
155 acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'opus': 'opus', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec]
156 extension = self._preferredcodec
157 more_opts = []
158 if self._preferredquality is not None:
159 # The opus codec doesn't support the -aq option
160 if int(self._preferredquality) < 10 and extension != 'opus':
161 more_opts += ['-q:a' if uses_avconv else '-aq', self._preferredquality]
162 else:
163 more_opts += ['-b:a' if uses_avconv else '-ab', self._preferredquality + 'k']
164 if self._preferredcodec == 'aac':
165 more_opts += ['-f', 'adts']
166 if self._preferredcodec == 'm4a':
167 more_opts += ['-bsf:a' if uses_avconv else '-absf', 'aac_adtstoasc']
168 if self._preferredcodec == 'vorbis':
169 extension = 'ogg'
170 if self._preferredcodec == 'wav':
171 extension = 'wav'
172 more_opts += ['-f', 'wav']
173
174 prefix, sep, ext = path.rpartition(u'.') # not os.path.splitext, since the latter does not work on unicode in all setups
175 new_path = prefix + sep + extension
176
177 # If we download foo.mp3 and convert it to... foo.mp3, then don't delete foo.mp3, silly.
178 if new_path == path:
179 self._nopostoverwrites = True
180
181 try:
182 if self._nopostoverwrites and os.path.exists(encodeFilename(new_path)):
183 self._downloader.to_screen(u'[youtube] Post-process file %s exists, skipping' % new_path)
184 else:
185 self._downloader.to_screen(u'[' + self._get_executable() + '] Destination: ' + new_path)
186 self.run_ffmpeg(path, new_path, acodec, more_opts)
187 except:
188 etype,e,tb = sys.exc_info()
189 if isinstance(e, AudioConversionError):
190 msg = u'audio conversion failed: ' + e.msg
191 else:
192 msg = u'error running ' + self._get_executable()
193 raise PostProcessingError(msg)
194
195 # Try to update the date time for extracted audio file.
196 if information.get('filetime') is not None:
197 try:
198 os.utime(encodeFilename(new_path), (time.time(), information['filetime']))
199 except:
200 self._downloader.report_warning(u'Cannot update utime of audio file')
201
202 information['filepath'] = new_path
203 return self._nopostoverwrites,information
204
205
206 class FFmpegVideoConvertor(FFmpegPostProcessor):
207 def __init__(self, downloader=None,preferedformat=None):
208 super(FFmpegVideoConvertor, self).__init__(downloader)
209 self._preferedformat=preferedformat
210
211 def run(self, information):
212 path = information['filepath']
213 prefix, sep, ext = path.rpartition(u'.')
214 outpath = prefix + sep + self._preferedformat
215 if information['ext'] == self._preferedformat:
216 self._downloader.to_screen(u'[ffmpeg] Not converting video file %s - already is in target format %s' % (path, self._preferedformat))
217 return True,information
218 self._downloader.to_screen(u'['+'ffmpeg'+'] Converting video from %s to %s, Destination: ' % (information['ext'], self._preferedformat) +outpath)
219 self.run_ffmpeg(path, outpath, [])
220 information['filepath'] = outpath
221 information['format'] = self._preferedformat
222 information['ext'] = self._preferedformat
223 return False,information
224
225
226 class FFmpegEmbedSubtitlePP(FFmpegPostProcessor):
227 # See http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
228 _lang_map = {
229 'aa': 'aar',
230 'ab': 'abk',
231 'ae': 'ave',
232 'af': 'afr',
233 'ak': 'aka',
234 'am': 'amh',
235 'an': 'arg',
236 'ar': 'ara',
237 'as': 'asm',
238 'av': 'ava',
239 'ay': 'aym',
240 'az': 'aze',
241 'ba': 'bak',
242 'be': 'bel',
243 'bg': 'bul',
244 'bh': 'bih',
245 'bi': 'bis',
246 'bm': 'bam',
247 'bn': 'ben',
248 'bo': 'bod',
249 'br': 'bre',
250 'bs': 'bos',
251 'ca': 'cat',
252 'ce': 'che',
253 'ch': 'cha',
254 'co': 'cos',
255 'cr': 'cre',
256 'cs': 'ces',
257 'cu': 'chu',
258 'cv': 'chv',
259 'cy': 'cym',
260 'da': 'dan',
261 'de': 'deu',
262 'dv': 'div',
263 'dz': 'dzo',
264 'ee': 'ewe',
265 'el': 'ell',
266 'en': 'eng',
267 'eo': 'epo',
268 'es': 'spa',
269 'et': 'est',
270 'eu': 'eus',
271 'fa': 'fas',
272 'ff': 'ful',
273 'fi': 'fin',
274 'fj': 'fij',
275 'fo': 'fao',
276 'fr': 'fra',
277 'fy': 'fry',
278 'ga': 'gle',
279 'gd': 'gla',
280 'gl': 'glg',
281 'gn': 'grn',
282 'gu': 'guj',
283 'gv': 'glv',
284 'ha': 'hau',
285 'he': 'heb',
286 'hi': 'hin',
287 'ho': 'hmo',
288 'hr': 'hrv',
289 'ht': 'hat',
290 'hu': 'hun',
291 'hy': 'hye',
292 'hz': 'her',
293 'ia': 'ina',
294 'id': 'ind',
295 'ie': 'ile',
296 'ig': 'ibo',
297 'ii': 'iii',
298 'ik': 'ipk',
299 'io': 'ido',
300 'is': 'isl',
301 'it': 'ita',
302 'iu': 'iku',
303 'ja': 'jpn',
304 'jv': 'jav',
305 'ka': 'kat',
306 'kg': 'kon',
307 'ki': 'kik',
308 'kj': 'kua',
309 'kk': 'kaz',
310 'kl': 'kal',
311 'km': 'khm',
312 'kn': 'kan',
313 'ko': 'kor',
314 'kr': 'kau',
315 'ks': 'kas',
316 'ku': 'kur',
317 'kv': 'kom',
318 'kw': 'cor',
319 'ky': 'kir',
320 'la': 'lat',
321 'lb': 'ltz',
322 'lg': 'lug',
323 'li': 'lim',
324 'ln': 'lin',
325 'lo': 'lao',
326 'lt': 'lit',
327 'lu': 'lub',
328 'lv': 'lav',
329 'mg': 'mlg',
330 'mh': 'mah',
331 'mi': 'mri',
332 'mk': 'mkd',
333 'ml': 'mal',
334 'mn': 'mon',
335 'mr': 'mar',
336 'ms': 'msa',
337 'mt': 'mlt',
338 'my': 'mya',
339 'na': 'nau',
340 'nb': 'nob',
341 'nd': 'nde',
342 'ne': 'nep',
343 'ng': 'ndo',
344 'nl': 'nld',
345 'nn': 'nno',
346 'no': 'nor',
347 'nr': 'nbl',
348 'nv': 'nav',
349 'ny': 'nya',
350 'oc': 'oci',
351 'oj': 'oji',
352 'om': 'orm',
353 'or': 'ori',
354 'os': 'oss',
355 'pa': 'pan',
356 'pi': 'pli',
357 'pl': 'pol',
358 'ps': 'pus',
359 'pt': 'por',
360 'qu': 'que',
361 'rm': 'roh',
362 'rn': 'run',
363 'ro': 'ron',
364 'ru': 'rus',
365 'rw': 'kin',
366 'sa': 'san',
367 'sc': 'srd',
368 'sd': 'snd',
369 'se': 'sme',
370 'sg': 'sag',
371 'si': 'sin',
372 'sk': 'slk',
373 'sl': 'slv',
374 'sm': 'smo',
375 'sn': 'sna',
376 'so': 'som',
377 'sq': 'sqi',
378 'sr': 'srp',
379 'ss': 'ssw',
380 'st': 'sot',
381 'su': 'sun',
382 'sv': 'swe',
383 'sw': 'swa',
384 'ta': 'tam',
385 'te': 'tel',
386 'tg': 'tgk',
387 'th': 'tha',
388 'ti': 'tir',
389 'tk': 'tuk',
390 'tl': 'tgl',
391 'tn': 'tsn',
392 'to': 'ton',
393 'tr': 'tur',
394 'ts': 'tso',
395 'tt': 'tat',
396 'tw': 'twi',
397 'ty': 'tah',
398 'ug': 'uig',
399 'uk': 'ukr',
400 'ur': 'urd',
401 'uz': 'uzb',
402 've': 'ven',
403 'vi': 'vie',
404 'vo': 'vol',
405 'wa': 'wln',
406 'wo': 'wol',
407 'xh': 'xho',
408 'yi': 'yid',
409 'yo': 'yor',
410 'za': 'zha',
411 'zh': 'zho',
412 'zu': 'zul',
413 }
414
415 def __init__(self, downloader=None, subtitlesformat='srt'):
416 super(FFmpegEmbedSubtitlePP, self).__init__(downloader)
417 self._subformat = subtitlesformat
418
419 @classmethod
420 def _conver_lang_code(cls, code):
421 """Convert language code from ISO 639-1 to ISO 639-2/T"""
422 return cls._lang_map.get(code[:2])
423
424 def run(self, information):
425 if information['ext'] != u'mp4':
426 self._downloader.to_screen(u'[ffmpeg] Subtitles can only be embedded in mp4 files')
427 return True, information
428 if not information.get('subtitles'):
429 self._downloader.to_screen(u'[ffmpeg] There aren\'t any subtitles to embed')
430 return True, information
431
432 sub_langs = [key for key in information['subtitles']]
433 filename = information['filepath']
434 input_files = [filename] + [subtitles_filename(filename, lang, self._subformat) for lang in sub_langs]
435
436 opts = ['-map', '0:0', '-map', '0:1', '-c:v', 'copy', '-c:a', 'copy']
437 for (i, lang) in enumerate(sub_langs):
438 opts.extend(['-map', '%d:0' % (i+1), '-c:s:%d' % i, 'mov_text'])
439 lang_code = self._conver_lang_code(lang)
440 if lang_code is not None:
441 opts.extend(['-metadata:s:s:%d' % i, 'language=%s' % lang_code])
442 opts.extend(['-f', 'mp4'])
443
444 temp_filename = filename + u'.temp'
445 self._downloader.to_screen(u'[ffmpeg] Embedding subtitles in \'%s\'' % filename)
446 self.run_ffmpeg_multiple_files(input_files, temp_filename, opts)
447 os.remove(encodeFilename(filename))
448 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
449
450 return True, information
451
452
453 class FFmpegMetadataPP(FFmpegPostProcessor):
454 def run(self, info):
455 metadata = {}
456 if info.get('title') is not None:
457 metadata['title'] = info['title']
458 if info.get('upload_date') is not None:
459 metadata['date'] = info['upload_date']
460 if info.get('uploader') is not None:
461 metadata['artist'] = info['uploader']
462 elif info.get('uploader_id') is not None:
463 metadata['artist'] = info['uploader_id']
464
465 if not metadata:
466 self._downloader.to_screen(u'[ffmpeg] There isn\'t any metadata to add')
467 return True, info
468
469 filename = info['filepath']
470 temp_filename = prepend_extension(filename, 'temp')
471
472 if info['ext'] == u'm4a':
473 options = ['-vn', '-acodec', 'copy']
474 else:
475 options = ['-c', 'copy']
476
477 for (name, value) in metadata.items():
478 options.extend(['-metadata', '%s=%s' % (name, value)])
479
480 self._downloader.to_screen(u'[ffmpeg] Adding metadata to \'%s\'' % filename)
481 self.run_ffmpeg(filename, temp_filename, options)
482 os.remove(encodeFilename(filename))
483 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
484 return True, info
485
486
487 class FFmpegMergerPP(FFmpegPostProcessor):
488 def run(self, info):
489 filename = info['filepath']
490 args = ['-c', 'copy']
491 self._downloader.to_screen(u'[ffmpeg] Merging formats into "%s"' % filename)
492 self.run_ffmpeg_multiple_files(info['__files_to_merge'], filename, args)
493 return True, info
494
495
496 class FFmpegAudioFixPP(FFmpegPostProcessor):
497 def run(self, info):
498 filename = info['filepath']
499 temp_filename = prepend_extension(filename, 'temp')
500
501 options = ['-vn', '-acodec', 'copy']
502 self._downloader.to_screen(u'[ffmpeg] Fixing audio file "%s"' % filename)
503 self.run_ffmpeg(filename, temp_filename, options)
504
505 os.remove(encodeFilename(filename))
506 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
507
508 return True, info