1 from __future__
import unicode_literals
11 from .common
import AudioConversionError
, PostProcessor
13 from ..compat
import compat_str
19 _get_exe_version_output
,
35 EXT_TO_OUT_FORMATS
= {
54 'vorbis': 'libvorbis',
60 class FFmpegPostProcessorError(PostProcessingError
):
64 class FFmpegPostProcessor(PostProcessor
):
65 def __init__(self
, downloader
=None):
66 PostProcessor
.__init
__(self
, downloader
)
67 self
._determine
_executables
()
69 def check_version(self
):
70 if not self
.available
:
71 raise FFmpegPostProcessorError('ffmpeg not found. Please install or provide the path using --ffmpeg-location')
73 required_version
= '10-0' if self
.basename
== 'avconv' else '1.0'
74 if is_outdated_version(
75 self
._versions
[self
.basename
], required_version
):
76 warning
= 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % (
77 self
.basename
, self
.basename
, required_version
)
78 self
.report_warning(warning
)
81 def get_versions_and_features(downloader
=None):
82 pp
= FFmpegPostProcessor(downloader
)
83 return pp
._versions
, pp
._features
86 def get_versions(downloader
=None):
87 return FFmpegPostProcessor
.get_version_and_features(downloader
)[0]
89 def _determine_executables(self
):
90 programs
= ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
92 def get_ffmpeg_version(path
, prog
):
93 out
= _get_exe_version_output(path
, ['-bsfs'])
94 ver
= detect_exe_version(out
) if out
else False
97 r
'(?:\d+:)?([0-9.]+)-[0-9]+ubuntu[0-9.]+$', # Ubuntu, see [1]
98 r
'n([0-9.]+)$', # Arch Linux
99 # 1. http://www.ducea.com/2006/06/17/ubuntu-package-version-naming-explanation/
102 mobj
= re
.match(regex
, ver
)
105 self
._versions
[prog
] = ver
106 if prog
!= 'ffmpeg' or not out
:
109 mobj
= re
.search(r
'(?m)^\s+libavformat\s+(?:[0-9. ]+)\s+/\s+(?P<runtime>[0-9. ]+)', out
)
110 lavf_runtime_version
= mobj
.group('runtime').replace(' ', '') if mobj
else None
112 'fdk': '--enable-libfdk-aac' in out
,
113 'setts': 'setts' in out
.splitlines(),
114 'needs_adtstoasc': is_outdated_version(lavf_runtime_version
, '57.56.100', False),
118 self
.probe_basename
= None
120 self
._versions
= None
123 prefer_ffmpeg
= self
.get_param('prefer_ffmpeg', True)
124 location
= self
.get_param('ffmpeg_location')
126 self
._paths
= {p: p for p in programs}
128 if not os
.path
.exists(location
):
130 'ffmpeg-location %s does not exist! '
131 'Continuing without ffmpeg.' % (location
))
134 elif os
.path
.isdir(location
):
135 dirname
, basename
= location
, None
137 basename
= os
.path
.splitext(os
.path
.basename(location
))[0]
138 basename
= next((p
for p
in programs
if basename
.startswith(p
)), 'ffmpeg')
139 dirname
= os
.path
.dirname(os
.path
.abspath(location
))
140 if basename
in ('ffmpeg', 'ffprobe'):
144 (p
, os
.path
.join(dirname
, p
)) for p
in programs
)
146 self
._paths
[basename
] = location
150 get_ffmpeg_version(self
._paths
[p
], p
)
152 if prefer_ffmpeg
is False:
153 prefs
= ('avconv', 'ffmpeg')
155 prefs
= ('ffmpeg', 'avconv')
157 if self
._versions
[p
]:
161 if prefer_ffmpeg
is False:
162 prefs
= ('avprobe', 'ffprobe')
164 prefs
= ('ffprobe', 'avprobe')
166 if self
._versions
[p
]:
167 self
.probe_basename
= p
170 if self
.basename
== 'avconv':
171 self
.deprecation_warning(
172 'Support for avconv is deprecated and may be removed in a future version. Use ffmpeg instead')
173 if self
.probe_basename
== 'avprobe':
174 self
.deprecation_warning(
175 'Support for avprobe is deprecated and may be removed in a future version. Use ffprobe instead')
179 return self
.basename
is not None
182 def executable(self
):
183 return self
._paths
[self
.basename
]
186 def probe_available(self
):
187 return self
.probe_basename
is not None
190 def probe_executable(self
):
191 return self
._paths
[self
.probe_basename
]
193 def get_audio_codec(self
, path
):
194 if not self
.probe_available
and not self
.available
:
195 raise PostProcessingError('ffprobe and ffmpeg not found. Please install or provide the path using --ffmpeg-location')
197 if self
.probe_available
:
199 encodeFilename(self
.probe_executable
, True),
200 encodeArgument('-show_streams')]
203 encodeFilename(self
.executable
, True),
204 encodeArgument('-i')]
205 cmd
.append(encodeFilename(self
._ffmpeg
_filename
_argument
(path
), True))
206 self
.write_debug('%s command line: %s' % (self
.basename
, shell_quote(cmd
)))
207 handle
= Popen(cmd
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
208 stdout_data
, stderr_data
= handle
.communicate_or_kill()
209 expected_ret
= 0 if self
.probe_available
else 1
210 if handle
.wait() != expected_ret
:
212 except (IOError, OSError):
214 output
= (stdout_data
if self
.probe_available
else stderr_data
).decode('ascii', 'ignore')
215 if self
.probe_available
:
217 for line
in output
.split('\n'):
218 if line
.startswith('codec_name='):
219 audio_codec
= line
.split('=')[1].strip()
220 elif line
.strip() == 'codec_type=audio' and audio_codec
is not None:
223 # Stream #FILE_INDEX:STREAM_INDEX[STREAM_ID](LANGUAGE): CODEC_TYPE: CODEC_NAME
225 r
'Stream\s*#\d+:\d+(?:\[0x[0-9a-f]+\])?(?:\([a-z]{3}\))?:\s*Audio:\s*([0-9a-z]+)',
231 def get_metadata_object(self
, path
, opts
=[]):
232 if self
.probe_basename
!= 'ffprobe':
233 if self
.probe_available
:
234 self
.report_warning('Only ffprobe is supported for metadata extraction')
235 raise PostProcessingError('ffprobe not found. Please install or provide the path using --ffmpeg-location')
239 encodeFilename(self
.probe_executable
, True),
240 encodeArgument('-hide_banner'),
241 encodeArgument('-show_format'),
242 encodeArgument('-show_streams'),
243 encodeArgument('-print_format'),
244 encodeArgument('json'),
248 cmd
.append(encodeFilename(self
._ffmpeg
_filename
_argument
(path
), True))
249 self
.write_debug('ffprobe command line: %s' % shell_quote(cmd
))
250 p
= Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
251 stdout
, stderr
= p
.communicate()
252 return json
.loads(stdout
.decode('utf-8', 'replace'))
254 def get_stream_number(self
, path
, keys
, value
):
255 streams
= self
.get_metadata_object(path
)['streams']
257 (i
for i
, stream
in enumerate(streams
) if traverse_obj(stream
, keys
, casesense
=False) == value
),
259 return num
, len(streams
)
261 def _get_real_video_duration(self
, filepath
, fatal
=True):
263 duration
= float_or_none(
264 traverse_obj(self
.get_metadata_object(filepath
), ('format', 'duration')))
266 raise PostProcessingError('ffprobe returned empty duration')
268 except PostProcessingError
as e
:
270 raise PostProcessingError(f
'Unable to determine video duration: {e.msg}')
272 def _duration_mismatch(self
, d1
, d2
):
275 # The duration is often only known to nearest second. So there can be <1sec disparity natually.
276 # Further excuse an additional <1sec difference.
277 return abs(d1
- d2
) > 2
279 def run_ffmpeg_multiple_files(self
, input_paths
, out_path
, opts
, **kwargs
):
280 return self
.real_run_ffmpeg(
281 [(path
, []) for path
in input_paths
],
282 [(out_path
, opts
)], **kwargs
)
284 def real_run_ffmpeg(self
, input_path_opts
, output_path_opts
, *, expected_retcodes
=(0,)):
288 os
.stat(encodeFilename(path
)).st_mtime
for path
, _
in input_path_opts
if path
)
290 cmd
= [encodeFilename(self
.executable
, True), encodeArgument('-y')]
291 # avconv does not have repeat option
292 if self
.basename
== 'ffmpeg':
293 cmd
+= [encodeArgument('-loglevel'), encodeArgument('repeat+info')]
295 def make_args(file, args
, name
, number
):
296 keys
= ['_%s%d' % (name
, number
), '_%s' % name
]
298 args
+= ['-movflags', '+faststart']
301 args
+= self
._configuration
_args
(self
.basename
, keys
)
305 [encodeArgument(arg
) for arg
in args
]
306 + [encodeFilename(self
._ffmpeg
_filename
_argument
(file), True)])
308 for arg_type
, path_opts
in (('i', input_path_opts
), ('o', output_path_opts
)):
309 cmd
+= itertools
.chain
.from_iterable(
310 make_args(path
, list(opts
), arg_type
, i
+ 1)
311 for i
, (path
, opts
) in enumerate(path_opts
) if path
)
313 self
.write_debug('ffmpeg command line: %s' % shell_quote(cmd
))
314 p
= Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
315 stdout
, stderr
= p
.communicate_or_kill()
316 if p
.returncode
not in variadic(expected_retcodes
):
317 stderr
= stderr
.decode('utf-8', 'replace').strip()
318 self
.write_debug(stderr
)
319 raise FFmpegPostProcessorError(stderr
.split('\n')[-1])
320 for out_path
, _
in output_path_opts
:
322 self
.try_utime(out_path
, oldest_mtime
, oldest_mtime
)
323 return stderr
.decode('utf-8', 'replace')
325 def run_ffmpeg(self
, path
, out_path
, opts
, **kwargs
):
326 return self
.run_ffmpeg_multiple_files([path
], out_path
, opts
, **kwargs
)
329 def _ffmpeg_filename_argument(fn
):
330 # Always use 'file:' because the filename may contain ':' (ffmpeg
331 # interprets that as a protocol) or can start with '-' (-- is broken in
332 # ffmpeg, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details)
333 # Also leave '-' intact in order not to break streaming to stdout.
334 if fn
.startswith(('http://', 'https://')):
336 return 'file:' + fn
if fn
!= '-' else fn
339 def _quote_for_ffmpeg(string
):
340 # See https://ffmpeg.org/ffmpeg-utils.html#toc-Quoting-and-escaping
341 # A sequence of '' produces '\'''\'';
342 # final replace removes the empty '' between \' \'.
343 string
= string
.replace("'", r
"'\''").replace("'''", "'")
344 # Handle potential ' at string boundaries.
345 string
= string
[1:] if string
[0] == "'" else "'" + string
346 return string
[:-1] if string
[-1] == "'" else string
+ "'"
348 def force_keyframes(self
, filename
, timestamps
):
349 timestamps
= orderedSet(timestamps
)
350 if timestamps
[0] == 0:
351 timestamps
= timestamps
[1:]
352 keyframe_file
= prepend_extension(filename
, 'keyframes.temp')
353 self
.to_screen(f
'Re-encoding "{filename}" with appropriate keyframes')
354 self
.run_ffmpeg(filename
, keyframe_file
, ['-force_key_frames', ','.join(
355 f
'{t:.6f}' for t
in timestamps
)])
358 def concat_files(self
, in_files
, out_file
, concat_opts
=None):
360 Use concat demuxer to concatenate multiple files having identical streams.
362 Only inpoint, outpoint, and duration concat options are supported.
363 See https://ffmpeg.org/ffmpeg-formats.html#concat-1 for details
365 concat_file
= f
'{out_file}.concat'
366 self
.write_debug(f
'Writing concat spec to {concat_file}')
367 with open(concat_file
, 'wt', encoding
='utf-8') as f
:
368 f
.writelines(self
._concat
_spec
(in_files
, concat_opts
))
370 out_flags
= ['-c', 'copy']
371 if out_file
.rpartition('.')[-1] in ('mp4', 'mov'):
372 # For some reason, '-c copy' is not enough to copy subtitles
373 out_flags
.extend(['-c:s', 'mov_text'])
376 self
.real_run_ffmpeg(
377 [(concat_file
, ['-hide_banner', '-nostdin', '-f', 'concat', '-safe', '0'])],
378 [(out_file
, out_flags
)])
380 os
.remove(concat_file
)
383 def _concat_spec(cls
, in_files
, concat_opts
=None):
384 if concat_opts
is None:
385 concat_opts
= [{}] * len(in_files
)
386 yield 'ffconcat version 1.0\n'
387 for file, opts
in zip(in_files
, concat_opts
):
388 yield f
'file {cls._quote_for_ffmpeg(cls._ffmpeg_filename_argument(file))}\n'
389 # Iterate explicitly to yield the following directives in order, ignoring the rest.
390 for directive
in 'inpoint', 'outpoint', 'duration':
391 if directive
in opts
:
392 yield f
'{directive} {opts[directive]}\n'
395 class FFmpegExtractAudioPP(FFmpegPostProcessor
):
396 COMMON_AUDIO_EXTS
= ('wav', 'flac', 'm4a', 'aiff', 'mp3', 'ogg', 'mka', 'opus', 'wma')
397 SUPPORTED_EXTS
= ('best', 'aac', 'flac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav', 'alac')
399 def __init__(self
, downloader
=None, preferredcodec
=None, preferredquality
=None, nopostoverwrites
=False):
400 FFmpegPostProcessor
.__init
__(self
, downloader
)
401 self
._preferredcodec
= preferredcodec
or 'best'
402 self
._preferredquality
= float_or_none(preferredquality
)
403 self
._nopostoverwrites
= nopostoverwrites
405 def _quality_args(self
, codec
):
406 if self
._preferredquality
is None:
408 elif self
._preferredquality
> 10:
409 return ['-b:a', f
'{self._preferredquality}k']
412 'libmp3lame': (10, 0),
413 'libvorbis': (0, 10),
414 # FFmpeg's AAC encoder does not have an upper limit for the value of -q:a.
415 # Experimentally, with values over 4, bitrate changes were minimal or non-existent
417 'libfdk_aac': (1, 5),
422 q
= limits
[1] + (limits
[0] - limits
[1]) * (self
._preferredquality
/ 10)
423 if codec
== 'libfdk_aac':
424 return ['-vbr', f
'{int(q)}']
425 return ['-q:a', f
'{q}']
427 def run_ffmpeg(self
, path
, out_path
, codec
, more_opts
):
431 acodec_opts
= ['-acodec', codec
]
432 opts
= ['-vn'] + acodec_opts
+ more_opts
434 FFmpegPostProcessor
.run_ffmpeg(self
, path
, out_path
, opts
)
435 except FFmpegPostProcessorError
as err
:
436 raise AudioConversionError(err
.msg
)
438 @PostProcessor._restrict_to(images
=False)
439 def run(self
, information
):
440 orig_path
= path
= information
['filepath']
441 orig_ext
= information
['ext']
443 if self
._preferredcodec
== 'best' and orig_ext
in self
.COMMON_AUDIO_EXTS
:
444 self
.to_screen('Skipping audio extraction since the file is already in a common audio format')
445 return [], information
447 filecodec
= self
.get_audio_codec(path
)
448 if filecodec
is None:
449 raise PostProcessingError('WARNING: unable to obtain file audio codec with ffprobe')
452 if self
._preferredcodec
== 'best' or self
._preferredcodec
== filecodec
or (self
._preferredcodec
== 'm4a' and filecodec
== 'aac'):
453 if filecodec
== 'aac' and self
._preferredcodec
in ['m4a', 'best']:
454 # Lossless, but in another container
457 more_opts
= ['-bsf:a', 'aac_adtstoasc']
458 elif filecodec
in ['aac', 'flac', 'mp3', 'vorbis', 'opus']:
459 # Lossless if possible
461 extension
= filecodec
462 if filecodec
== 'aac':
463 more_opts
= ['-f', 'adts']
464 if filecodec
== 'vorbis':
466 elif filecodec
== 'alac':
469 more_opts
+= ['-acodec', 'alac']
472 acodec
= 'libmp3lame'
474 more_opts
= self
._quality
_args
(acodec
)
476 # We convert the audio (lossy if codec is lossy)
477 acodec
= ACODECS
[self
._preferredcodec
]
478 if acodec
== 'aac' and self
._features
.get('fdk'):
479 acodec
= 'libfdk_aac'
480 extension
= self
._preferredcodec
481 more_opts
= self
._quality
_args
(acodec
)
482 if self
._preferredcodec
== 'aac':
483 more_opts
+= ['-f', 'adts']
484 elif self
._preferredcodec
== 'm4a':
485 more_opts
+= ['-bsf:a', 'aac_adtstoasc']
486 elif self
._preferredcodec
== 'vorbis':
488 elif self
._preferredcodec
== 'wav':
490 more_opts
+= ['-f', 'wav']
491 elif self
._preferredcodec
== 'alac':
493 more_opts
+= ['-acodec', 'alac']
495 prefix
, sep
, ext
= path
.rpartition('.') # not os.path.splitext, since the latter does not work on unicode in all setups
496 temp_path
= new_path
= prefix
+ sep
+ extension
499 orig_path
= prepend_extension(path
, 'orig')
500 temp_path
= prepend_extension(path
, 'temp')
501 if (self
._nopostoverwrites
and os
.path
.exists(encodeFilename(new_path
))
502 and os
.path
.exists(encodeFilename(orig_path
))):
503 self
.to_screen('Post-process file %s exists, skipping' % new_path
)
504 return [], information
507 self
.to_screen(f
'Destination: {new_path}')
508 self
.run_ffmpeg(path
, temp_path
, acodec
, more_opts
)
509 except AudioConversionError
as e
:
510 raise PostProcessingError(
511 'audio conversion failed: ' + e
.msg
)
513 raise PostProcessingError('error running ' + self
.basename
)
515 os
.replace(path
, orig_path
)
516 os
.replace(temp_path
, new_path
)
517 information
['filepath'] = new_path
518 information
['ext'] = extension
520 # Try to update the date time for extracted audio file.
521 if information
.get('filetime') is not None:
523 new_path
, time
.time(), information
['filetime'],
524 errnote
='Cannot update utime of audio file')
526 return [orig_path
], information
529 class FFmpegVideoConvertorPP(FFmpegPostProcessor
):
530 SUPPORTED_EXTS
= ('mp4', 'mkv', 'flv', 'webm', 'mov', 'avi', 'mp3', 'mka', 'm4a', 'ogg', 'opus')
531 FORMAT_RE
= re
.compile(r
'{0}(?:/{0})*$'.format(r
'(?:\w+>)?(?:%s)' % '|'.join(SUPPORTED_EXTS
)))
532 _ACTION
= 'converting'
534 def __init__(self
, downloader
=None, preferedformat
=None):
535 super(FFmpegVideoConvertorPP
, self
).__init
__(downloader
)
536 self
._preferedformats
= preferedformat
.lower().split('/')
538 def _target_ext(self
, source_ext
):
539 for pair
in self
._preferedformats
:
541 if len(kv
) == 1 or kv
[0].strip() == source_ext
:
542 return kv
[-1].strip()
545 def _options(target_ext
):
546 if target_ext
== 'avi':
547 return ['-c:v', 'libxvid', '-vtag', 'XVID']
550 @PostProcessor._restrict_to(images
=False)
552 filename
, source_ext
= info
['filepath'], info
['ext'].lower()
553 target_ext
= self
._target
_ext
(source_ext
)
555 f
'could not find a mapping for {source_ext}' if not target_ext
556 else f
'already is in target format {source_ext}' if source_ext
== target_ext
559 self
.to_screen(f
'Not {self._ACTION} media file {filename!r}; {_skip_msg}')
562 outpath
= replace_extension(filename
, target_ext
, source_ext
)
563 self
.to_screen(f
'{self._ACTION.title()} video from {source_ext} to {target_ext}; Destination: {outpath}')
564 self
.run_ffmpeg(filename
, outpath
, self
._options
(target_ext
))
566 info
['filepath'] = outpath
567 info
['format'] = info
['ext'] = target_ext
568 return [filename
], info
571 class FFmpegVideoRemuxerPP(FFmpegVideoConvertorPP
):
575 def _options(target_ext
):
576 return ['-c', 'copy', '-map', '0', '-dn']
579 class FFmpegEmbedSubtitlePP(FFmpegPostProcessor
):
580 def __init__(self
, downloader
=None, already_have_subtitle
=False):
581 super(FFmpegEmbedSubtitlePP
, self
).__init
__(downloader
)
582 self
._already
_have
_subtitle
= already_have_subtitle
584 @PostProcessor._restrict_to(images
=False)
586 if info
['ext'] not in ('mp4', 'webm', 'mkv'):
587 self
.to_screen('Subtitles can only be embedded in mp4, webm or mkv files')
589 subtitles
= info
.get('requested_subtitles')
591 self
.to_screen('There aren\'t any subtitles to embed')
594 filename
= info
['filepath']
596 # Disabled temporarily. There needs to be a way to overide this
597 # in case of duration actually mismatching in extractor
598 # See: https://github.com/yt-dlp/yt-dlp/issues/1870, https://github.com/yt-dlp/yt-dlp/issues/1385
600 if info.get('duration') and not info.get('__real_download') and self._duration_mismatch(
601 self._get_real_video_duration(filename, False), info['duration']):
602 self.to_screen(f'Skipping {self.pp_key()} since the real and expected durations mismatch')
607 sub_langs
, sub_names
, sub_filenames
= [], [], []
608 webm_vtt_warn
= False
611 for lang
, sub_info
in subtitles
.items():
612 if not os
.path
.exists(sub_info
.get('filepath', '')):
613 self
.report_warning(f
'Skipping embedding {lang} subtitle because the file is missing')
615 sub_ext
= sub_info
['ext']
616 if sub_ext
== 'json':
617 self
.report_warning('JSON subtitles cannot be embedded')
618 elif ext
!= 'webm' or ext
== 'webm' and sub_ext
== 'vtt':
619 sub_langs
.append(lang
)
620 sub_names
.append(sub_info
.get('name'))
621 sub_filenames
.append(sub_info
['filepath'])
623 if not webm_vtt_warn
and ext
== 'webm' and sub_ext
!= 'vtt':
625 self
.report_warning('Only WebVTT subtitles can be embedded in webm files')
626 if not mp4_ass_warn
and ext
== 'mp4' and sub_ext
== 'ass':
628 self
.report_warning('ASS subtitles cannot be properly embedded in mp4 files; expect issues')
633 input_files
= [filename
] + sub_filenames
636 '-c', 'copy', '-map', '0', '-dn',
637 # Don't copy the existing subtitles, we may be running the
638 # postprocessor a second time
640 # Don't copy Apple TV chapters track, bin_data (see #19042, #19024,
641 # https://trac.ffmpeg.org/ticket/6016)
644 if info
['ext'] == 'mp4':
645 opts
+= ['-c:s', 'mov_text']
646 for i
, (lang
, name
) in enumerate(zip(sub_langs
, sub_names
)):
647 opts
.extend(['-map', '%d:0' % (i
+ 1)])
648 lang_code
= ISO639Utils
.short2long(lang
) or lang
649 opts
.extend(['-metadata:s:s:%d' % i
, 'language=%s' % lang_code
])
651 opts
.extend(['-metadata:s:s:%d' % i
, 'handler_name=%s' % name
,
652 '-metadata:s:s:%d' % i
, 'title=%s' % name
])
654 temp_filename
= prepend_extension(filename
, 'temp')
655 self
.to_screen('Embedding subtitles in "%s"' % filename
)
656 self
.run_ffmpeg_multiple_files(input_files
, temp_filename
, opts
)
657 os
.replace(temp_filename
, filename
)
659 files_to_delete
= [] if self
._already
_have
_subtitle
else sub_filenames
660 return files_to_delete
, info
663 class FFmpegMetadataPP(FFmpegPostProcessor
):
665 def __init__(self
, downloader
, add_metadata
=True, add_chapters
=True, add_infojson
='if_exists'):
666 FFmpegPostProcessor
.__init
__(self
, downloader
)
667 self
._add
_metadata
= add_metadata
668 self
._add
_chapters
= add_chapters
669 self
._add
_infojson
= add_infojson
672 def _options(target_ext
):
673 yield from ('-map', '0', '-dn')
674 if target_ext
== 'm4a':
675 yield from ('-vn', '-acodec', 'copy')
677 yield from ('-c', 'copy')
679 @PostProcessor._restrict_to(images
=False)
681 filename
, metadata_filename
= info
['filepath'], None
682 files_to_delete
, options
= [], []
683 if self
._add
_chapters
and info
.get('chapters'):
684 metadata_filename
= replace_extension(filename
, 'meta')
685 options
.extend(self
._get
_chapter
_opts
(info
['chapters'], metadata_filename
))
686 files_to_delete
.append(metadata_filename
)
687 if self
._add
_metadata
:
688 options
.extend(self
._get
_metadata
_opts
(info
))
690 if self
._add
_infojson
:
691 if info
['ext'] in ('mkv', 'mka'):
692 infojson_filename
= info
.get('infojson_filename')
693 options
.extend(self
._get
_infojson
_opts
(info
, infojson_filename
))
694 if not infojson_filename
:
695 files_to_delete
.append(info
.get('infojson_filename'))
696 elif self
._add
_infojson
is True:
697 self
.to_screen('The info-json can only be attached to mkv/mka files')
700 self
.to_screen('There isn\'t any metadata to add')
703 temp_filename
= prepend_extension(filename
, 'temp')
704 self
.to_screen('Adding metadata to "%s"' % filename
)
705 self
.run_ffmpeg_multiple_files(
706 (filename
, metadata_filename
), temp_filename
,
707 itertools
.chain(self
._options
(info
['ext']), *options
))
708 for file in filter(None, files_to_delete
):
709 os
.remove(file) # Don't obey --keep-files
710 os
.replace(temp_filename
, filename
)
714 def _get_chapter_opts(chapters
, metadata_filename
):
715 with io
.open(metadata_filename
, 'wt', encoding
='utf-8') as f
:
716 def ffmpeg_escape(text
):
717 return re
.sub(r
'([\\=;#\n])', r
'\\\1', text
)
719 metadata_file_content
= ';FFMETADATA1\n'
720 for chapter
in chapters
:
721 metadata_file_content
+= '[CHAPTER]\nTIMEBASE=1/1000\n'
722 metadata_file_content
+= 'START=%d\n' % (chapter
['start_time'] * 1000)
723 metadata_file_content
+= 'END=%d\n' % (chapter
['end_time'] * 1000)
724 chapter_title
= chapter
.get('title')
726 metadata_file_content
+= 'title=%s\n' % ffmpeg_escape(chapter_title
)
727 f
.write(metadata_file_content
)
728 yield ('-map_metadata', '1')
730 def _get_metadata_opts(self
, info
):
732 meta_prefix
= 'meta_'
734 def add(meta_list
, info_list
=None):
736 str(info
[key
]) for key
in [meta_prefix
] + list(variadic(info_list
or meta_list
))
737 if info
.get(key
) is not None), None)
738 if value
not in ('', None):
739 metadata
.update({meta_f: value for meta_f in variadic(meta_list)}
)
741 # See [1-4] for some info on media metadata/metadata supported
743 # 1. https://kdenlive.org/en/project/adding-meta-data-to-mp4-video/
744 # 2. https://wiki.multimedia.cx/index.php/FFmpeg_Metadata
745 # 3. https://kodi.wiki/view/Video_file_tagging
747 add('title', ('track', 'title'))
748 add('date', 'upload_date')
749 add(('description', 'synopsis'), 'description')
750 add(('purl', 'comment'), 'webpage_url')
751 add('track', 'track_number')
752 add('artist', ('artist', 'creator', 'uploader', 'uploader_id'))
756 add('disc', 'disc_number')
757 add('show', 'series')
759 add('episode_id', ('episode', 'episode_id'))
760 add('episode_sort', 'episode_number')
761 if 'embed-metadata' in self
.get_param('compat_opts', []):
762 add('comment', 'description')
763 metadata
.pop('synopsis', None)
765 for key
, value
in info
.items():
766 if value
is not None and key
!= meta_prefix
and key
.startswith(meta_prefix
):
767 metadata
[key
[len(meta_prefix
):]] = value
769 for name
, value
in metadata
.items():
770 yield ('-metadata', f
'{name}={value}')
773 for fmt
in info
.get('requested_formats') or []:
774 stream_count
= 2 if 'none' not in (fmt
.get('vcodec'), fmt
.get('acodec')) else 1
775 if fmt
.get('language'):
776 lang
= ISO639Utils
.short2long(fmt
['language']) or fmt
['language']
777 for i
in range(stream_count
):
778 yield ('-metadata:s:%d' % (stream_idx
+ i
), 'language=%s' % lang
)
779 stream_idx
+= stream_count
781 def _get_infojson_opts(self
, info
, infofn
):
782 if not infofn
or not os
.path
.exists(infofn
):
783 if self
._add
_infojson
is not True:
785 infofn
= infofn
or '%s.temp' % (
786 self
._downloader
.prepare_filename(info
, 'infojson')
787 or replace_extension(self
._downloader
.prepare_filename(info
), 'info.json', info
['ext']))
788 if not self
._downloader
._ensure
_dir
_exists
(infofn
):
790 self
.write_debug(f
'Writing info-json to: {infofn}')
791 write_json_file(self
._downloader
.sanitize_info(info
, self
.get_param('clean_infojson', True)), infofn
)
792 info
['infojson_filename'] = infofn
794 old_stream
, new_stream
= self
.get_stream_number(info
['filepath'], ('tags', 'mimetype'), 'application/json')
795 if old_stream
is not None:
796 yield ('-map', '-0:%d' % old_stream
)
799 yield ('-attach', infofn
,
800 '-metadata:s:%d' % new_stream
, 'mimetype=application/json')
803 class FFmpegMergerPP(FFmpegPostProcessor
):
804 @PostProcessor._restrict_to(images
=False)
806 filename
= info
['filepath']
807 temp_filename
= prepend_extension(filename
, 'temp')
808 args
= ['-c', 'copy']
810 for (i
, fmt
) in enumerate(info
['requested_formats']):
811 if fmt
.get('acodec') != 'none':
812 args
.extend(['-map', f
'{i}:a:0'])
813 aac_fixup
= fmt
['protocol'].startswith('m3u8') and self
.get_audio_codec(fmt
['filepath']) == 'aac'
815 args
.extend([f
'-bsf:a:{audio_streams}', 'aac_adtstoasc'])
817 if fmt
.get('vcodec') != 'none':
818 args
.extend(['-map', '%u:v:0' % (i
)])
819 self
.to_screen('Merging formats into "%s"' % filename
)
820 self
.run_ffmpeg_multiple_files(info
['__files_to_merge'], temp_filename
, args
)
821 os
.rename(encodeFilename(temp_filename
), encodeFilename(filename
))
822 return info
['__files_to_merge'], info
825 # TODO: figure out merge-capable ffmpeg version
826 if self
.basename
!= 'avconv':
829 required_version
= '10-0'
830 if is_outdated_version(
831 self
._versions
[self
.basename
], required_version
):
832 warning
= ('Your copy of %s is outdated and unable to properly mux separate video and audio files, '
833 'yt-dlp will download single file media. '
834 'Update %s to version %s or newer to fix this.') % (
835 self
.basename
, self
.basename
, required_version
)
836 self
.report_warning(warning
)
841 class FFmpegFixupPostProcessor(FFmpegPostProcessor
):
842 def _fixup(self
, msg
, filename
, options
):
843 temp_filename
= prepend_extension(filename
, 'temp')
845 self
.to_screen(f
'{msg} of "{filename}"')
846 self
.run_ffmpeg(filename
, temp_filename
, options
)
848 os
.replace(temp_filename
, filename
)
851 class FFmpegFixupStretchedPP(FFmpegFixupPostProcessor
):
852 @PostProcessor._restrict_to(images
=False, audio
=False)
854 stretched_ratio
= info
.get('stretched_ratio')
855 if stretched_ratio
not in (None, 1):
856 self
._fixup
('Fixing aspect ratio', info
['filepath'], [
857 '-c', 'copy', '-map', '0', '-dn', '-aspect', '%f' % stretched_ratio
])
861 class FFmpegFixupM4aPP(FFmpegFixupPostProcessor
):
862 @PostProcessor._restrict_to(images
=False, video
=False)
864 if info
.get('container') == 'm4a_dash':
865 self
._fixup
('Correcting container', info
['filepath'], [
866 '-c', 'copy', '-map', '0', '-dn', '-f', 'mp4'])
870 class FFmpegFixupM3u8PP(FFmpegFixupPostProcessor
):
871 def _needs_fixup(self
, info
):
872 yield info
['ext'] in ('mp4', 'm4a')
873 yield info
['protocol'].startswith('m3u8')
875 metadata
= self
.get_metadata_object(info
['filepath'])
876 except PostProcessingError
as e
:
877 self
.report_warning(f
'Unable to extract metadata: {e.msg}')
880 yield traverse_obj(metadata
, ('format', 'format_name'), casesense
=False) == 'mpegts'
882 @PostProcessor._restrict_to(images
=False)
884 if all(self
._needs
_fixup
(info
)):
885 self
._fixup
('Fixing MPEG-TS in MP4 container', info
['filepath'], [
886 '-c', 'copy', '-map', '0', '-dn', '-f', 'mp4', '-bsf:a', 'aac_adtstoasc'])
890 class FFmpegFixupTimestampPP(FFmpegFixupPostProcessor
):
892 def __init__(self
, downloader
=None, trim
=0.001):
893 # "trim" should be used when the video contains unintended packets
894 super(FFmpegFixupTimestampPP
, self
).__init
__(downloader
)
895 assert isinstance(trim
, (int, float))
896 self
.trim
= str(trim
)
898 @PostProcessor._restrict_to(images
=False)
900 if not self
._features
.get('setts'):
902 'A re-encode is needed to fix timestamps in older versions of ffmpeg. '
903 'Please install ffmpeg 4.4 or later to fixup without re-encoding')
904 opts
= ['-vf', 'setpts=PTS-STARTPTS']
906 opts
= ['-c', 'copy', '-bsf', 'setts=ts=TS-STARTPTS']
907 self
._fixup
('Fixing frame timestamp', info
['filepath'], opts
+ ['-map', '0', '-dn', '-ss', self
.trim
])
911 class FFmpegCopyStreamPostProcessor(FFmpegFixupPostProcessor
):
912 MESSAGE
= 'Copying stream'
914 @PostProcessor._restrict_to(images
=False)
916 self
._fixup
(self
.MESSAGE
, info
['filepath'], ['-c', 'copy', '-map', '0', '-dn'])
920 class FFmpegFixupDurationPP(FFmpegCopyStreamPostProcessor
):
921 MESSAGE
= 'Fixing video duration'
924 class FFmpegFixupDuplicateMoovPP(FFmpegCopyStreamPostProcessor
):
925 MESSAGE
= 'Fixing duplicate MOOV atoms'
928 class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor
):
929 SUPPORTED_EXTS
= ('srt', 'vtt', 'ass', 'lrc')
931 def __init__(self
, downloader
=None, format
=None):
932 super(FFmpegSubtitlesConvertorPP
, self
).__init
__(downloader
)
936 subs
= info
.get('requested_subtitles')
937 new_ext
= self
.format
939 if new_format
== 'vtt':
940 new_format
= 'webvtt'
942 self
.to_screen('There aren\'t any subtitles to convert')
944 self
.to_screen('Converting subtitles')
946 for lang
, sub
in subs
.items():
947 if not os
.path
.exists(sub
.get('filepath', '')):
948 self
.report_warning(f
'Skipping embedding {lang} subtitle because the file is missing')
952 self
.to_screen('Subtitle file for %s is already in the requested format' % new_ext
)
956 'You have requested to convert json subtitles into another format, '
957 'which is currently not possible')
959 old_file
= sub
['filepath']
960 sub_filenames
.append(old_file
)
961 new_file
= replace_extension(old_file
, new_ext
)
963 if ext
in ('dfxp', 'ttml', 'tt'):
965 'You have requested to convert dfxp (TTML) subtitles into another format, '
966 'which results in style information loss')
969 srt_file
= replace_extension(old_file
, 'srt')
971 with open(dfxp_file
, 'rb') as f
:
972 srt_data
= dfxp2srt(f
.read())
974 with io
.open(srt_file
, 'wt', encoding
='utf-8') as f
:
981 'filepath': srt_file
,
987 sub_filenames
.append(srt_file
)
989 self
.run_ffmpeg(old_file
, new_file
, ['-f', new_format
])
991 with io
.open(new_file
, 'rt', encoding
='utf-8') as f
:
995 'filepath': new_file
,
998 info
['__files_to_move'][new_file
] = replace_extension(
999 info
['__files_to_move'][sub
['filepath']], new_ext
)
1001 return sub_filenames
, info
1004 class FFmpegSplitChaptersPP(FFmpegPostProcessor
):
1005 def __init__(self
, downloader
, force_keyframes
=False):
1006 FFmpegPostProcessor
.__init
__(self
, downloader
)
1007 self
._force
_keyframes
= force_keyframes
1009 def _prepare_filename(self
, number
, chapter
, info
):
1012 'section_number': number
,
1013 'section_title': chapter
.get('title'),
1014 'section_start': chapter
.get('start_time'),
1015 'section_end': chapter
.get('end_time'),
1017 return self
._downloader
.prepare_filename(info
, 'chapter')
1019 def _ffmpeg_args_for_chapter(self
, number
, chapter
, info
):
1020 destination
= self
._prepare
_filename
(number
, chapter
, info
)
1021 if not self
._downloader
._ensure
_dir
_exists
(encodeFilename(destination
)):
1024 chapter
['filepath'] = destination
1025 self
.to_screen('Chapter %03d; Destination: %s' % (number
, destination
))
1028 ['-ss', compat_str(chapter
['start_time']),
1029 '-t', compat_str(chapter
['end_time'] - chapter
['start_time'])])
1031 @PostProcessor._restrict_to(images
=False)
1032 def run(self
, info
):
1033 chapters
= info
.get('chapters') or []
1035 self
.to_screen('Chapter information is unavailable')
1038 in_file
= info
['filepath']
1039 if self
._force
_keyframes
and len(chapters
) > 1:
1040 in_file
= self
.force_keyframes(in_file
, (c
['start_time'] for c
in chapters
))
1041 self
.to_screen('Splitting video by chapters; %d chapters found' % len(chapters
))
1042 for idx
, chapter
in enumerate(chapters
):
1043 destination
, opts
= self
._ffmpeg
_args
_for
_chapter
(idx
+ 1, chapter
, info
)
1044 self
.real_run_ffmpeg([(in_file
, opts
)], [(destination
, ['-c', 'copy'])])
1045 if in_file
!= info
['filepath']:
1050 class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor
):
1051 SUPPORTED_EXTS
= ('jpg', 'png')
1053 def __init__(self
, downloader
=None, format
=None):
1054 super(FFmpegThumbnailsConvertorPP
, self
).__init
__(downloader
)
1055 self
.format
= format
1059 with open(encodeFilename(path
), 'rb') as f
:
1061 return b
[0:4] == b
'RIFF' and b
[8:] == b
'WEBP'
1063 def fixup_webp(self
, info
, idx
=-1):
1064 thumbnail_filename
= info
['thumbnails'][idx
]['filepath']
1065 _
, thumbnail_ext
= os
.path
.splitext(thumbnail_filename
)
1067 thumbnail_ext
= thumbnail_ext
[1:].lower()
1068 if thumbnail_ext
!= 'webp' and self
.is_webp(thumbnail_filename
):
1069 self
.to_screen('Correcting thumbnail "%s" extension to webp' % thumbnail_filename
)
1070 webp_filename
= replace_extension(thumbnail_filename
, 'webp')
1071 os
.replace(thumbnail_filename
, webp_filename
)
1072 info
['thumbnails'][idx
]['filepath'] = webp_filename
1073 info
['__files_to_move'][webp_filename
] = replace_extension(
1074 info
['__files_to_move'].pop(thumbnail_filename
), 'webp')
1077 def _options(target_ext
):
1078 if target_ext
== 'jpg':
1079 return ['-bsf:v', 'mjpeg2jpeg']
1082 def convert_thumbnail(self
, thumbnail_filename
, target_ext
):
1083 thumbnail_conv_filename
= replace_extension(thumbnail_filename
, target_ext
)
1085 self
.to_screen('Converting thumbnail "%s" to %s' % (thumbnail_filename
, target_ext
))
1086 self
.real_run_ffmpeg(
1087 [(thumbnail_filename
, ['-f', 'image2', '-pattern_type', 'none'])],
1088 [(thumbnail_conv_filename
.replace('%', '%%'), self
._options
(target_ext
))])
1089 return thumbnail_conv_filename
1091 def run(self
, info
):
1092 files_to_delete
= []
1093 has_thumbnail
= False
1095 for idx
, thumbnail_dict
in enumerate(info
['thumbnails']):
1096 if 'filepath' not in thumbnail_dict
:
1098 has_thumbnail
= True
1099 self
.fixup_webp(info
, idx
)
1100 original_thumbnail
= thumbnail_dict
['filepath']
1101 _
, thumbnail_ext
= os
.path
.splitext(original_thumbnail
)
1103 thumbnail_ext
= thumbnail_ext
[1:].lower()
1104 if thumbnail_ext
== 'jpeg':
1105 thumbnail_ext
= 'jpg'
1106 if thumbnail_ext
== self
.format
:
1107 self
.to_screen('Thumbnail "%s" is already in the requested format' % original_thumbnail
)
1109 thumbnail_dict
['filepath'] = self
.convert_thumbnail(original_thumbnail
, self
.format
)
1110 files_to_delete
.append(original_thumbnail
)
1111 info
['__files_to_move'][thumbnail_dict
['filepath']] = replace_extension(
1112 info
['__files_to_move'][original_thumbnail
], self
.format
)
1114 if not has_thumbnail
:
1115 self
.to_screen('There aren\'t any thumbnails to convert')
1116 return files_to_delete
, info