]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/ffmpeg.py
Start new release.
[youtubedl] / youtube_dl / postprocessor / ffmpeg.py
1 from __future__ import unicode_literals
2
3 import io
4 import os
5 import subprocess
6 import time
7 import re
8
9
10 from .common import AudioConversionError, PostProcessor
11
12 from ..compat import (
13 compat_subprocess_get_DEVNULL,
14 )
15 from ..utils import (
16 encodeArgument,
17 encodeFilename,
18 get_exe_version,
19 is_outdated_version,
20 PostProcessingError,
21 prepend_extension,
22 shell_quote,
23 subtitles_filename,
24 dfxp2srt,
25 ISO639Utils,
26 replace_extension,
27 )
28
29
30 EXT_TO_OUT_FORMATS = {
31 'aac': 'adts',
32 'flac': 'flac',
33 'm4a': 'ipod',
34 'mka': 'matroska',
35 'mkv': 'matroska',
36 'mpg': 'mpeg',
37 'ogv': 'ogg',
38 'ts': 'mpegts',
39 'wma': 'asf',
40 'wmv': 'asf',
41 }
42 ACODECS = {
43 'mp3': 'libmp3lame',
44 'aac': 'aac',
45 'flac': 'flac',
46 'm4a': 'aac',
47 'opus': 'libopus',
48 'vorbis': 'libvorbis',
49 'wav': None,
50 }
51
52
53 class FFmpegPostProcessorError(PostProcessingError):
54 pass
55
56
57 class FFmpegPostProcessor(PostProcessor):
58 def __init__(self, downloader=None):
59 PostProcessor.__init__(self, downloader)
60 self._determine_executables()
61
62 def check_version(self):
63 if not self.available:
64 raise FFmpegPostProcessorError('ffmpeg or avconv not found. Please install one.')
65
66 required_version = '10-0' if self.basename == 'avconv' else '1.0'
67 if is_outdated_version(
68 self._versions[self.basename], required_version):
69 warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % (
70 self.basename, self.basename, required_version)
71 if self._downloader:
72 self._downloader.report_warning(warning)
73
74 @staticmethod
75 def get_versions(downloader=None):
76 return FFmpegPostProcessor(downloader)._versions
77
78 def _determine_executables(self):
79 programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
80 prefer_ffmpeg = True
81
82 def get_ffmpeg_version(path):
83 ver = get_exe_version(path, args=['-version'])
84 if ver:
85 regexs = [
86 r'(?:\d+:)?([0-9.]+)-[0-9]+ubuntu[0-9.]+$', # Ubuntu, see [1]
87 r'n([0-9.]+)$', # Arch Linux
88 # 1. http://www.ducea.com/2006/06/17/ubuntu-package-version-naming-explanation/
89 ]
90 for regex in regexs:
91 mobj = re.match(regex, ver)
92 if mobj:
93 ver = mobj.group(1)
94 return ver
95
96 self.basename = None
97 self.probe_basename = None
98
99 self._paths = None
100 self._versions = None
101 if self._downloader:
102 prefer_ffmpeg = self._downloader.params.get('prefer_ffmpeg', True)
103 location = self._downloader.params.get('ffmpeg_location')
104 if location is not None:
105 if not os.path.exists(location):
106 self._downloader.report_warning(
107 'ffmpeg-location %s does not exist! '
108 'Continuing without avconv/ffmpeg.' % (location))
109 self._versions = {}
110 return
111 elif not os.path.isdir(location):
112 basename = os.path.splitext(os.path.basename(location))[0]
113 if basename not in programs:
114 self._downloader.report_warning(
115 'Cannot identify executable %s, its basename should be one of %s. '
116 'Continuing without avconv/ffmpeg.' %
117 (location, ', '.join(programs)))
118 self._versions = {}
119 return None
120 location = os.path.dirname(os.path.abspath(location))
121 if basename in ('ffmpeg', 'ffprobe'):
122 prefer_ffmpeg = True
123
124 self._paths = dict(
125 (p, os.path.join(location, p)) for p in programs)
126 self._versions = dict(
127 (p, get_ffmpeg_version(self._paths[p])) for p in programs)
128 if self._versions is None:
129 self._versions = dict(
130 (p, get_ffmpeg_version(p)) for p in programs)
131 self._paths = dict((p, p) for p in programs)
132
133 if prefer_ffmpeg is False:
134 prefs = ('avconv', 'ffmpeg')
135 else:
136 prefs = ('ffmpeg', 'avconv')
137 for p in prefs:
138 if self._versions[p]:
139 self.basename = p
140 break
141
142 if prefer_ffmpeg is False:
143 prefs = ('avprobe', 'ffprobe')
144 else:
145 prefs = ('ffprobe', 'avprobe')
146 for p in prefs:
147 if self._versions[p]:
148 self.probe_basename = p
149 break
150
151 @property
152 def available(self):
153 return self.basename is not None
154
155 @property
156 def executable(self):
157 return self._paths[self.basename]
158
159 @property
160 def probe_available(self):
161 return self.probe_basename is not None
162
163 @property
164 def probe_executable(self):
165 return self._paths[self.probe_basename]
166
167 def get_audio_codec(self, path):
168 if not self.probe_available:
169 raise PostProcessingError('ffprobe or avprobe not found. Please install one.')
170 try:
171 cmd = [
172 encodeFilename(self.probe_executable, True),
173 encodeArgument('-show_streams'),
174 encodeFilename(self._ffmpeg_filename_argument(path), True)]
175 if self._downloader.params.get('verbose', False):
176 self._downloader.to_screen('[debug] %s command line: %s' % (self.basename, shell_quote(cmd)))
177 handle = subprocess.Popen(cmd, stderr=compat_subprocess_get_DEVNULL(), stdout=subprocess.PIPE, stdin=subprocess.PIPE)
178 output = handle.communicate()[0]
179 if handle.wait() != 0:
180 return None
181 except (IOError, OSError):
182 return None
183 audio_codec = None
184 for line in output.decode('ascii', 'ignore').split('\n'):
185 if line.startswith('codec_name='):
186 audio_codec = line.split('=')[1].strip()
187 elif line.strip() == 'codec_type=audio' and audio_codec is not None:
188 return audio_codec
189 return None
190
191 def run_ffmpeg_multiple_files(self, input_paths, out_path, opts):
192 self.check_version()
193
194 oldest_mtime = min(
195 os.stat(encodeFilename(path)).st_mtime for path in input_paths)
196
197 opts += self._configuration_args()
198
199 files_cmd = []
200 for path in input_paths:
201 files_cmd.extend([
202 encodeArgument('-i'),
203 encodeFilename(self._ffmpeg_filename_argument(path), True)
204 ])
205 cmd = ([encodeFilename(self.executable, True), encodeArgument('-y')] +
206 files_cmd +
207 [encodeArgument(o) for o in opts] +
208 [encodeFilename(self._ffmpeg_filename_argument(out_path), True)])
209
210 if self._downloader.params.get('verbose', False):
211 self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd))
212 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
213 stdout, stderr = p.communicate()
214 if p.returncode != 0:
215 stderr = stderr.decode('utf-8', 'replace')
216 msg = stderr.strip().split('\n')[-1]
217 raise FFmpegPostProcessorError(msg)
218 self.try_utime(out_path, oldest_mtime, oldest_mtime)
219
220 def run_ffmpeg(self, path, out_path, opts):
221 self.run_ffmpeg_multiple_files([path], out_path, opts)
222
223 def _ffmpeg_filename_argument(self, fn):
224 # Always use 'file:' because the filename may contain ':' (ffmpeg
225 # interprets that as a protocol) or can start with '-' (-- is broken in
226 # ffmpeg, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details)
227 # Also leave '-' intact in order not to break streaming to stdout.
228 return 'file:' + fn if fn != '-' else fn
229
230
231 class FFmpegExtractAudioPP(FFmpegPostProcessor):
232 def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
233 FFmpegPostProcessor.__init__(self, downloader)
234 if preferredcodec is None:
235 preferredcodec = 'best'
236 self._preferredcodec = preferredcodec
237 self._preferredquality = preferredquality
238 self._nopostoverwrites = nopostoverwrites
239
240 def run_ffmpeg(self, path, out_path, codec, more_opts):
241 if codec is None:
242 acodec_opts = []
243 else:
244 acodec_opts = ['-acodec', codec]
245 opts = ['-vn'] + acodec_opts + more_opts
246 try:
247 FFmpegPostProcessor.run_ffmpeg(self, path, out_path, opts)
248 except FFmpegPostProcessorError as err:
249 raise AudioConversionError(err.msg)
250
251 def run(self, information):
252 path = information['filepath']
253
254 filecodec = self.get_audio_codec(path)
255 if filecodec is None:
256 raise PostProcessingError('WARNING: unable to obtain file audio codec with ffprobe')
257
258 more_opts = []
259 if self._preferredcodec == 'best' or self._preferredcodec == filecodec or (self._preferredcodec == 'm4a' and filecodec == 'aac'):
260 if filecodec == 'aac' and self._preferredcodec in ['m4a', 'best']:
261 # Lossless, but in another container
262 acodec = 'copy'
263 extension = 'm4a'
264 more_opts = ['-bsf:a', 'aac_adtstoasc']
265 elif filecodec in ['aac', 'flac', 'mp3', 'vorbis', 'opus']:
266 # Lossless if possible
267 acodec = 'copy'
268 extension = filecodec
269 if filecodec == 'aac':
270 more_opts = ['-f', 'adts']
271 if filecodec == 'vorbis':
272 extension = 'ogg'
273 else:
274 # MP3 otherwise.
275 acodec = 'libmp3lame'
276 extension = 'mp3'
277 more_opts = []
278 if self._preferredquality is not None:
279 if int(self._preferredquality) < 10:
280 more_opts += ['-q:a', self._preferredquality]
281 else:
282 more_opts += ['-b:a', self._preferredquality + 'k']
283 else:
284 # We convert the audio (lossy if codec is lossy)
285 acodec = ACODECS[self._preferredcodec]
286 extension = self._preferredcodec
287 more_opts = []
288 if self._preferredquality is not None:
289 # The opus codec doesn't support the -aq option
290 if int(self._preferredquality) < 10 and extension != 'opus':
291 more_opts += ['-q:a', self._preferredquality]
292 else:
293 more_opts += ['-b:a', self._preferredquality + 'k']
294 if self._preferredcodec == 'aac':
295 more_opts += ['-f', 'adts']
296 if self._preferredcodec == 'm4a':
297 more_opts += ['-bsf:a', 'aac_adtstoasc']
298 if self._preferredcodec == 'vorbis':
299 extension = 'ogg'
300 if self._preferredcodec == 'wav':
301 extension = 'wav'
302 more_opts += ['-f', 'wav']
303
304 prefix, sep, ext = path.rpartition('.') # not os.path.splitext, since the latter does not work on unicode in all setups
305 new_path = prefix + sep + extension
306
307 information['filepath'] = new_path
308 information['ext'] = extension
309
310 # If we download foo.mp3 and convert it to... foo.mp3, then don't delete foo.mp3, silly.
311 if (new_path == path or
312 (self._nopostoverwrites and os.path.exists(encodeFilename(new_path)))):
313 self._downloader.to_screen('[ffmpeg] Post-process file %s exists, skipping' % new_path)
314 return [], information
315
316 try:
317 self._downloader.to_screen('[ffmpeg] Destination: ' + new_path)
318 self.run_ffmpeg(path, new_path, acodec, more_opts)
319 except AudioConversionError as e:
320 raise PostProcessingError(
321 'audio conversion failed: ' + e.msg)
322 except Exception:
323 raise PostProcessingError('error running ' + self.basename)
324
325 # Try to update the date time for extracted audio file.
326 if information.get('filetime') is not None:
327 self.try_utime(
328 new_path, time.time(), information['filetime'],
329 errnote='Cannot update utime of audio file')
330
331 return [path], information
332
333
334 class FFmpegVideoConvertorPP(FFmpegPostProcessor):
335 def __init__(self, downloader=None, preferedformat=None):
336 super(FFmpegVideoConvertorPP, self).__init__(downloader)
337 self._preferedformat = preferedformat
338
339 def run(self, information):
340 path = information['filepath']
341 if information['ext'] == self._preferedformat:
342 self._downloader.to_screen('[ffmpeg] Not converting video file %s - already is in target format %s' % (path, self._preferedformat))
343 return [], information
344 options = []
345 if self._preferedformat == 'avi':
346 options.extend(['-c:v', 'libxvid', '-vtag', 'XVID'])
347 prefix, sep, ext = path.rpartition('.')
348 outpath = prefix + sep + self._preferedformat
349 self._downloader.to_screen('[' + 'ffmpeg' + '] Converting video from %s to %s, Destination: ' % (information['ext'], self._preferedformat) + outpath)
350 self.run_ffmpeg(path, outpath, options)
351 information['filepath'] = outpath
352 information['format'] = self._preferedformat
353 information['ext'] = self._preferedformat
354 return [path], information
355
356
357 class FFmpegEmbedSubtitlePP(FFmpegPostProcessor):
358 def run(self, information):
359 if information['ext'] not in ('mp4', 'webm', 'mkv'):
360 self._downloader.to_screen('[ffmpeg] Subtitles can only be embedded in mp4, webm or mkv files')
361 return [], information
362 subtitles = information.get('requested_subtitles')
363 if not subtitles:
364 self._downloader.to_screen('[ffmpeg] There aren\'t any subtitles to embed')
365 return [], information
366
367 filename = information['filepath']
368
369 ext = information['ext']
370 sub_langs = []
371 sub_filenames = []
372 webm_vtt_warn = False
373
374 for lang, sub_info in subtitles.items():
375 sub_ext = sub_info['ext']
376 if ext != 'webm' or ext == 'webm' and sub_ext == 'vtt':
377 sub_langs.append(lang)
378 sub_filenames.append(subtitles_filename(filename, lang, sub_ext))
379 else:
380 if not webm_vtt_warn and ext == 'webm' and sub_ext != 'vtt':
381 webm_vtt_warn = True
382 self._downloader.to_screen('[ffmpeg] Only WebVTT subtitles can be embedded in webm files')
383
384 if not sub_langs:
385 return [], information
386
387 input_files = [filename] + sub_filenames
388
389 opts = [
390 '-map', '0',
391 '-c', 'copy',
392 # Don't copy the existing subtitles, we may be running the
393 # postprocessor a second time
394 '-map', '-0:s',
395 ]
396 if information['ext'] == 'mp4':
397 opts += ['-c:s', 'mov_text']
398 for (i, lang) in enumerate(sub_langs):
399 opts.extend(['-map', '%d:0' % (i + 1)])
400 lang_code = ISO639Utils.short2long(lang) or lang
401 opts.extend(['-metadata:s:s:%d' % i, 'language=%s' % lang_code])
402
403 temp_filename = prepend_extension(filename, 'temp')
404 self._downloader.to_screen('[ffmpeg] Embedding subtitles in \'%s\'' % filename)
405 self.run_ffmpeg_multiple_files(input_files, temp_filename, opts)
406 os.remove(encodeFilename(filename))
407 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
408
409 return sub_filenames, information
410
411
412 class FFmpegMetadataPP(FFmpegPostProcessor):
413 def run(self, info):
414 metadata = {}
415
416 def add(meta_list, info_list=None):
417 if not info_list:
418 info_list = meta_list
419 if not isinstance(meta_list, (list, tuple)):
420 meta_list = (meta_list,)
421 if not isinstance(info_list, (list, tuple)):
422 info_list = (info_list,)
423 for info_f in info_list:
424 if info.get(info_f) is not None:
425 for meta_f in meta_list:
426 metadata[meta_f] = info[info_f]
427 break
428
429 add('title', ('track', 'title'))
430 add('date', 'upload_date')
431 add(('description', 'comment'), 'description')
432 add('purl', 'webpage_url')
433 add('track', 'track_number')
434 add('artist', ('artist', 'creator', 'uploader', 'uploader_id'))
435 add('genre')
436 add('album')
437 add('album_artist')
438 add('disc', 'disc_number')
439
440 if not metadata:
441 self._downloader.to_screen('[ffmpeg] There isn\'t any metadata to add')
442 return [], info
443
444 filename = info['filepath']
445 temp_filename = prepend_extension(filename, 'temp')
446 in_filenames = [filename]
447 options = []
448
449 if info['ext'] == 'm4a':
450 options.extend(['-vn', '-acodec', 'copy'])
451 else:
452 options.extend(['-c', 'copy'])
453
454 for (name, value) in metadata.items():
455 options.extend(['-metadata', '%s=%s' % (name, value)])
456
457 chapters = info.get('chapters', [])
458 if chapters:
459 metadata_filename = replace_extension(filename, 'meta')
460 with io.open(metadata_filename, 'wt', encoding='utf-8') as f:
461 def ffmpeg_escape(text):
462 return re.sub(r'(=|;|#|\\|\n)', r'\\\1', text)
463
464 metadata_file_content = ';FFMETADATA1\n'
465 for chapter in chapters:
466 metadata_file_content += '[CHAPTER]\nTIMEBASE=1/1000\n'
467 metadata_file_content += 'START=%d\n' % (chapter['start_time'] * 1000)
468 metadata_file_content += 'END=%d\n' % (chapter['end_time'] * 1000)
469 chapter_title = chapter.get('title')
470 if chapter_title:
471 metadata_file_content += 'title=%s\n' % ffmpeg_escape(chapter_title)
472 f.write(metadata_file_content)
473 in_filenames.append(metadata_filename)
474 options.extend(['-map_metadata', '1'])
475
476 self._downloader.to_screen('[ffmpeg] Adding metadata to \'%s\'' % filename)
477 self.run_ffmpeg_multiple_files(in_filenames, temp_filename, options)
478 if chapters:
479 os.remove(metadata_filename)
480 os.remove(encodeFilename(filename))
481 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
482 return [], info
483
484
485 class FFmpegMergerPP(FFmpegPostProcessor):
486 def run(self, info):
487 filename = info['filepath']
488 temp_filename = prepend_extension(filename, 'temp')
489 args = ['-c', 'copy', '-map', '0:v:0', '-map', '1:a:0']
490 self._downloader.to_screen('[ffmpeg] Merging formats into "%s"' % filename)
491 self.run_ffmpeg_multiple_files(info['__files_to_merge'], temp_filename, args)
492 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
493 return info['__files_to_merge'], info
494
495 def can_merge(self):
496 # TODO: figure out merge-capable ffmpeg version
497 if self.basename != 'avconv':
498 return True
499
500 required_version = '10-0'
501 if is_outdated_version(
502 self._versions[self.basename], required_version):
503 warning = ('Your copy of %s is outdated and unable to properly mux separate video and audio files, '
504 'youtube-dl will download single file media. '
505 'Update %s to version %s or newer to fix this.') % (
506 self.basename, self.basename, required_version)
507 if self._downloader:
508 self._downloader.report_warning(warning)
509 return False
510 return True
511
512
513 class FFmpegFixupStretchedPP(FFmpegPostProcessor):
514 def run(self, info):
515 stretched_ratio = info.get('stretched_ratio')
516 if stretched_ratio is None or stretched_ratio == 1:
517 return [], info
518
519 filename = info['filepath']
520 temp_filename = prepend_extension(filename, 'temp')
521
522 options = ['-c', 'copy', '-aspect', '%f' % stretched_ratio]
523 self._downloader.to_screen('[ffmpeg] Fixing aspect ratio in "%s"' % filename)
524 self.run_ffmpeg(filename, temp_filename, options)
525
526 os.remove(encodeFilename(filename))
527 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
528
529 return [], info
530
531
532 class FFmpegFixupM4aPP(FFmpegPostProcessor):
533 def run(self, info):
534 if info.get('container') != 'm4a_dash':
535 return [], info
536
537 filename = info['filepath']
538 temp_filename = prepend_extension(filename, 'temp')
539
540 options = ['-c', 'copy', '-f', 'mp4']
541 self._downloader.to_screen('[ffmpeg] Correcting container in "%s"' % filename)
542 self.run_ffmpeg(filename, temp_filename, options)
543
544 os.remove(encodeFilename(filename))
545 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
546
547 return [], info
548
549
550 class FFmpegFixupM3u8PP(FFmpegPostProcessor):
551 def run(self, info):
552 filename = info['filepath']
553 if self.get_audio_codec(filename) == 'aac':
554 temp_filename = prepend_extension(filename, 'temp')
555
556 options = ['-c', 'copy', '-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
557 self._downloader.to_screen('[ffmpeg] Fixing malformed AAC bitstream in "%s"' % filename)
558 self.run_ffmpeg(filename, temp_filename, options)
559
560 os.remove(encodeFilename(filename))
561 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
562 return [], info
563
564
565 class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor):
566 def __init__(self, downloader=None, format=None):
567 super(FFmpegSubtitlesConvertorPP, self).__init__(downloader)
568 self.format = format
569
570 def run(self, info):
571 subs = info.get('requested_subtitles')
572 filename = info['filepath']
573 new_ext = self.format
574 new_format = new_ext
575 if new_format == 'vtt':
576 new_format = 'webvtt'
577 if subs is None:
578 self._downloader.to_screen('[ffmpeg] There aren\'t any subtitles to convert')
579 return [], info
580 self._downloader.to_screen('[ffmpeg] Converting subtitles')
581 sub_filenames = []
582 for lang, sub in subs.items():
583 ext = sub['ext']
584 if ext == new_ext:
585 self._downloader.to_screen(
586 '[ffmpeg] Subtitle file for %s is already in the requested format' % new_ext)
587 continue
588 old_file = subtitles_filename(filename, lang, ext)
589 sub_filenames.append(old_file)
590 new_file = subtitles_filename(filename, lang, new_ext)
591
592 if ext in ('dfxp', 'ttml', 'tt'):
593 self._downloader.report_warning(
594 'You have requested to convert dfxp (TTML) subtitles into another format, '
595 'which results in style information loss')
596
597 dfxp_file = old_file
598 srt_file = subtitles_filename(filename, lang, 'srt')
599
600 with open(dfxp_file, 'rb') as f:
601 srt_data = dfxp2srt(f.read())
602
603 with io.open(srt_file, 'wt', encoding='utf-8') as f:
604 f.write(srt_data)
605 old_file = srt_file
606
607 subs[lang] = {
608 'ext': 'srt',
609 'data': srt_data
610 }
611
612 if new_ext == 'srt':
613 continue
614 else:
615 sub_filenames.append(srt_file)
616
617 self.run_ffmpeg(old_file, new_file, ['-f', new_format])
618
619 with io.open(new_file, 'rt', encoding='utf-8') as f:
620 subs[lang] = {
621 'ext': new_ext,
622 'data': f.read(),
623 }
624
625 return sub_filenames, info