11 from .common
import PostProcessor
12 from ..compat
import imghdr
18 _get_exe_version_output
,
36 EXT_TO_OUT_FORMATS
= {
51 # name: (ext, encoder, opts)
52 'mp3': ('mp3', 'libmp3lame', ()),
53 'aac': ('m4a', 'aac', ('-f', 'adts')),
54 'm4a': ('m4a', 'aac', ('-bsf:a', 'aac_adtstoasc')),
55 'opus': ('opus', 'libopus', ()),
56 'vorbis': ('ogg', 'libvorbis', ()),
57 'flac': ('flac', 'flac', ()),
58 'alac': ('m4a', None, ('-acodec', 'alac')),
59 'wav': ('wav', None, ('-f', 'wav')),
63 def create_mapping_re(supported
):
64 return re
.compile(r
'{0}(?:/{0})*$'.format(r
'(?:\s*\w+\s*>)?\s*(?:{})\s*'.format('|'.join(supported
))))
67 def resolve_mapping(source
, mapping
):
69 Get corresponding item from a mapping string like 'A>B/C>D/E'
70 @returns (target, error_message)
72 for pair
in mapping
.lower().split('/'):
73 kv
= pair
.split('>', 1)
74 if len(kv
) == 1 or kv
[0].strip() == source
:
75 target
= kv
[-1].strip()
77 return target
, f
'already is in target format {source}'
79 return None, f
'could not find a mapping for {source}'
82 class FFmpegPostProcessorError(PostProcessingError
):
86 class FFmpegPostProcessor(PostProcessor
):
87 _ffmpeg_location
= contextvars
.ContextVar('ffmpeg_location', default
=None)
89 def __init__(self
, downloader
=None):
90 PostProcessor
.__init
__(self
, downloader
)
91 self
._prefer
_ffmpeg
= self
.get_param('prefer_ffmpeg', True)
92 self
._paths
= self
._determine
_executables
()
95 def get_versions_and_features(downloader
=None):
96 pp
= FFmpegPostProcessor(downloader
)
97 return pp
._versions
, pp
._features
100 def get_versions(downloader
=None):
101 return FFmpegPostProcessor
.get_versions_and_features(downloader
)[0]
103 _ffmpeg_to_avconv
= {'ffmpeg': 'avconv', 'ffprobe': 'avprobe'}
105 def _determine_executables(self
):
106 programs
= [*self
._ffmpeg
_to
_avconv
.keys(), *self
._ffmpeg
_to
_avconv
.values()]
108 location
= self
.get_param('ffmpeg_location', self
._ffmpeg
_location
.get())
110 return {p
: p
for p
in programs
}
112 if not os
.path
.exists(location
):
114 f
'ffmpeg-location {location} does not exist! Continuing without ffmpeg', only_once
=True)
116 elif os
.path
.isdir(location
):
117 dirname
, basename
, filename
= location
, None, None
119 filename
= os
.path
.basename(location
)
120 basename
= next((p
for p
in programs
if p
in filename
), 'ffmpeg')
121 dirname
= os
.path
.dirname(os
.path
.abspath(location
))
122 if basename
in self
._ffmpeg
_to
_avconv
:
123 self
._prefer
_ffmpeg
= True
125 paths
= {p
: os
.path
.join(dirname
, p
) for p
in programs
}
126 if basename
and basename
in filename
:
128 path
= os
.path
.join(dirname
, filename
.replace(basename
, p
))
129 if os
.path
.exists(path
):
132 paths
[basename
] = location
135 _version_cache
, _features_cache
= {None: None}, {}
137 def _get_ffmpeg_version(self
, prog
):
138 path
= self
._paths
.get(prog
)
139 if path
in self
._version
_cache
:
140 return self
._version
_cache
[path
], self
._features
_cache
.get(path
, {})
141 out
= _get_exe_version_output(path
, ['-bsfs'])
142 ver
= detect_exe_version(out
) if out
else False
145 r
'(?:\d+:)?([0-9.]+)-[0-9]+ubuntu[0-9.]+$', # Ubuntu, see [1]
146 r
'n([0-9.]+)$', # Arch Linux
147 # 1. http://www.ducea.com/2006/06/17/ubuntu-package-version-naming-explanation/
150 mobj
= re
.match(regex
, ver
)
153 self
._version
_cache
[path
] = ver
154 if prog
!= 'ffmpeg' or not out
:
157 mobj
= re
.search(r
'(?m)^\s+libavformat\s+(?:[0-9. ]+)\s+/\s+(?P<runtime>[0-9. ]+)', out
)
158 lavf_runtime_version
= mobj
.group('runtime').replace(' ', '') if mobj
else None
159 self
._features
_cache
[path
] = features
= {
160 'fdk': '--enable-libfdk-aac' in out
,
161 'setts': 'setts' in out
.splitlines(),
162 'needs_adtstoasc': is_outdated_version(lavf_runtime_version
, '57.56.100', False),
168 return filter_dict({self
.basename
: self
._version
, self
.probe_basename
: self
._probe
_version
})
170 @functools.cached_property
172 _
= self
._version
# run property
175 @functools.cached_property
176 def probe_basename(self
):
177 _
= self
._probe
_version
# run property
178 return self
.probe_basename
180 def _get_version(self
, kind
):
181 executables
= (kind
, )
182 if not self
._prefer
_ffmpeg
:
183 executables
= (kind
, self
._ffmpeg
_to
_avconv
[kind
])
184 basename
, version
, features
= next(filter(
185 lambda x
: x
[1], ((p
, *self
._get
_ffmpeg
_version
(p
)) for p
in executables
)), (None, None, {}))
187 self
.basename
, self
._features
= basename
, features
189 self
.probe_basename
= basename
190 if basename
== self
._ffmpeg
_to
_avconv
[kind
]:
191 self
.deprecated_feature(f
'Support for {self._ffmpeg_to_avconv[kind]} is deprecated and '
192 f
'may be removed in a future version. Use {kind} instead')
195 @functools.cached_property
197 return self
._get
_version
('ffmpeg')
199 @functools.cached_property
200 def _probe_version(self
):
201 return self
._get
_version
('ffprobe')
205 return self
.basename
is not None
208 def executable(self
):
209 return self
._paths
.get(self
.basename
)
212 def probe_available(self
):
213 return self
.probe_basename
is not None
216 def probe_executable(self
):
217 return self
._paths
.get(self
.probe_basename
)
220 def stream_copy_opts(copy
=True, *, ext
=None):
221 yield from ('-map', '0')
222 # Don't copy Apple TV chapters track, bin_data
223 # See https://github.com/yt-dlp/yt-dlp/issues/2, #19042, #19024, https://trac.ffmpeg.org/ticket/6016
224 yield from ('-dn', '-ignore_unknown')
226 yield from ('-c', 'copy')
227 if ext
in ('mp4', 'mov', 'm4a'):
228 yield from ('-c:s', 'mov_text')
230 def check_version(self
):
231 if not self
.available
:
232 raise FFmpegPostProcessorError('ffmpeg not found. Please install or provide the path using --ffmpeg-location')
234 required_version
= '10-0' if self
.basename
== 'avconv' else '1.0'
235 if is_outdated_version(self
._version
, required_version
):
236 self
.report_warning(f
'Your copy of {self.basename} is outdated, update {self.basename} '
237 f
'to version {required_version} or newer if you encounter any errors')
239 def get_audio_codec(self
, path
):
240 if not self
.probe_available
and not self
.available
:
241 raise PostProcessingError('ffprobe and ffmpeg not found. Please install or provide the path using --ffmpeg-location')
243 if self
.probe_available
:
245 self
.probe_executable
,
246 encodeArgument('-show_streams')]
250 encodeArgument('-i')]
251 cmd
.append(self
._ffmpeg
_filename
_argument
(path
))
252 self
.write_debug(f
'{self.basename} command line: {shell_quote(cmd)}')
253 stdout
, stderr
, returncode
= Popen
.run(
254 cmd
, text
=True, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
255 if returncode
!= (0 if self
.probe_available
else 1):
259 output
= stdout
if self
.probe_available
else stderr
260 if self
.probe_available
:
262 for line
in output
.split('\n'):
263 if line
.startswith('codec_name='):
264 audio_codec
= line
.split('=')[1].strip()
265 elif line
.strip() == 'codec_type=audio' and audio_codec
is not None:
268 # Stream #FILE_INDEX:STREAM_INDEX[STREAM_ID](LANGUAGE): CODEC_TYPE: CODEC_NAME
270 r
'Stream\s*#\d+:\d+(?:\[0x[0-9a-f]+\])?(?:\([a-z]{3}\))?:\s*Audio:\s*([0-9a-z]+)',
276 def get_metadata_object(self
, path
, opts
=[]):
277 if self
.probe_basename
!= 'ffprobe':
278 if self
.probe_available
:
279 self
.report_warning('Only ffprobe is supported for metadata extraction')
280 raise PostProcessingError('ffprobe not found. Please install or provide the path using --ffmpeg-location')
284 self
.probe_executable
,
285 encodeArgument('-hide_banner'),
286 encodeArgument('-show_format'),
287 encodeArgument('-show_streams'),
288 encodeArgument('-print_format'),
289 encodeArgument('json'),
293 cmd
.append(self
._ffmpeg
_filename
_argument
(path
))
294 self
.write_debug(f
'ffprobe command line: {shell_quote(cmd)}')
295 stdout
, _
, _
= Popen
.run(cmd
, text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
296 return json
.loads(stdout
)
298 def get_stream_number(self
, path
, keys
, value
):
299 streams
= self
.get_metadata_object(path
)['streams']
301 (i
for i
, stream
in enumerate(streams
) if traverse_obj(stream
, keys
, casesense
=False) == value
),
303 return num
, len(streams
)
305 def _fixup_chapters(self
, info
):
306 last_chapter
= traverse_obj(info
, ('chapters', -1))
307 if last_chapter
and not last_chapter
.get('end_time'):
308 last_chapter
['end_time'] = self
._get
_real
_video
_duration
(info
['filepath'])
310 def _get_real_video_duration(self
, filepath
, fatal
=True):
312 duration
= float_or_none(
313 traverse_obj(self
.get_metadata_object(filepath
), ('format', 'duration')))
315 raise PostProcessingError('ffprobe returned empty duration')
317 except PostProcessingError
as e
:
319 raise PostProcessingError(f
'Unable to determine video duration: {e.msg}')
321 def _duration_mismatch(self
, d1
, d2
, tolerance
=2):
324 # The duration is often only known to nearest second. So there can be <1sec disparity natually.
325 # Further excuse an additional <1sec difference.
326 return abs(d1
- d2
) > tolerance
328 def run_ffmpeg_multiple_files(self
, input_paths
, out_path
, opts
, **kwargs
):
329 return self
.real_run_ffmpeg(
330 [(path
, []) for path
in input_paths
],
331 [(out_path
, opts
)], **kwargs
)
333 def real_run_ffmpeg(self
, input_path_opts
, output_path_opts
, *, expected_retcodes
=(0,)):
337 os
.stat(path
).st_mtime
for path
, _
in input_path_opts
if path
)
339 cmd
= [self
.executable
, encodeArgument('-y')]
340 # avconv does not have repeat option
341 if self
.basename
== 'ffmpeg':
342 cmd
+= [encodeArgument('-loglevel'), encodeArgument('repeat+info')]
344 def make_args(file, args
, name
, number
):
345 keys
= [f
'_{name}{number}', f
'_{name}']
347 args
+= ['-movflags', '+faststart']
350 args
+= self
._configuration
_args
(self
.basename
, keys
)
354 [encodeArgument(arg
) for arg
in args
]
355 + [self
._ffmpeg
_filename
_argument
(file)])
357 for arg_type
, path_opts
in (('i', input_path_opts
), ('o', output_path_opts
)):
358 cmd
+= itertools
.chain
.from_iterable(
359 make_args(path
, list(opts
), arg_type
, i
+ 1)
360 for i
, (path
, opts
) in enumerate(path_opts
) if path
)
362 self
.write_debug(f
'ffmpeg command line: {shell_quote(cmd)}')
363 _
, stderr
, returncode
= Popen
.run(
364 cmd
, text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
365 if returncode
not in variadic(expected_retcodes
):
366 self
.write_debug(stderr
)
367 raise FFmpegPostProcessorError(stderr
.strip().splitlines()[-1])
368 for out_path
, _
in output_path_opts
:
370 self
.try_utime(out_path
, oldest_mtime
, oldest_mtime
)
373 def run_ffmpeg(self
, path
, out_path
, opts
, **kwargs
):
374 return self
.run_ffmpeg_multiple_files([path
], out_path
, opts
, **kwargs
)
377 def _ffmpeg_filename_argument(fn
):
378 # Always use 'file:' because the filename may contain ':' (ffmpeg
379 # interprets that as a protocol) or can start with '-' (-- is broken in
380 # ffmpeg, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details)
381 # Also leave '-' intact in order not to break streaming to stdout.
382 if fn
.startswith(('http://', 'https://')):
384 return 'file:' + fn
if fn
!= '-' else fn
387 def _quote_for_ffmpeg(string
):
388 # See https://ffmpeg.org/ffmpeg-utils.html#toc-Quoting-and-escaping
389 # A sequence of '' produces '\'''\'';
390 # final replace removes the empty '' between \' \'.
391 string
= string
.replace("'", r
"'\''").replace("'''", "'")
392 # Handle potential ' at string boundaries.
393 string
= string
[1:] if string
[0] == "'" else "'" + string
394 return string
[:-1] if string
[-1] == "'" else string
+ "'"
396 def force_keyframes(self
, filename
, timestamps
):
397 timestamps
= orderedSet(timestamps
)
398 if timestamps
[0] == 0:
399 timestamps
= timestamps
[1:]
400 keyframe_file
= prepend_extension(filename
, 'keyframes.temp')
401 self
.to_screen(f
'Re-encoding "{filename}" with appropriate keyframes')
402 self
.run_ffmpeg(filename
, keyframe_file
, [
403 *self
.stream_copy_opts(False, ext
=determine_ext(filename
)),
404 '-force_key_frames', ','.join(f
'{t:.6f}' for t
in timestamps
)])
407 def concat_files(self
, in_files
, out_file
, concat_opts
=None):
409 Use concat demuxer to concatenate multiple files having identical streams.
411 Only inpoint, outpoint, and duration concat options are supported.
412 See https://ffmpeg.org/ffmpeg-formats.html#concat-1 for details
414 concat_file
= f
'{out_file}.concat'
415 self
.write_debug(f
'Writing concat spec to {concat_file}')
416 with
open(concat_file
, 'w', encoding
='utf-8') as f
:
417 f
.writelines(self
._concat
_spec
(in_files
, concat_opts
))
419 out_flags
= list(self
.stream_copy_opts(ext
=determine_ext(out_file
)))
421 self
.real_run_ffmpeg(
422 [(concat_file
, ['-hide_banner', '-nostdin', '-f', 'concat', '-safe', '0'])],
423 [(out_file
, out_flags
)])
424 self
._delete
_downloaded
_files
(concat_file
)
427 def _concat_spec(cls
, in_files
, concat_opts
=None):
428 if concat_opts
is None:
429 concat_opts
= [{}] * len(in_files
)
430 yield 'ffconcat version 1.0\n'
431 for file, opts
in zip(in_files
, concat_opts
):
432 yield f
'file {cls._quote_for_ffmpeg(cls._ffmpeg_filename_argument(file))}\n'
433 # Iterate explicitly to yield the following directives in order, ignoring the rest.
434 for directive
in 'inpoint', 'outpoint', 'duration':
435 if directive
in opts
:
436 yield f
'{directive} {opts[directive]}\n'
439 class FFmpegExtractAudioPP(FFmpegPostProcessor
):
440 COMMON_AUDIO_EXTS
= (*MEDIA_EXTENSIONS
.common_audio
, 'wma')
441 SUPPORTED_EXTS
= tuple(ACODECS
.keys())
442 FORMAT_RE
= create_mapping_re(('best', *SUPPORTED_EXTS
))
444 def __init__(self
, downloader
=None, preferredcodec
=None, preferredquality
=None, nopostoverwrites
=False):
445 FFmpegPostProcessor
.__init
__(self
, downloader
)
446 self
.mapping
= preferredcodec
or 'best'
447 self
._preferredquality
= float_or_none(preferredquality
)
448 self
._nopostoverwrites
= nopostoverwrites
450 def _quality_args(self
, codec
):
451 if self
._preferredquality
is None:
453 elif self
._preferredquality
> 10:
454 return ['-b:a', f
'{self._preferredquality}k']
457 'libmp3lame': (10, 0),
458 'libvorbis': (0, 10),
459 # FFmpeg's AAC encoder does not have an upper limit for the value of -q:a.
460 # Experimentally, with values over 4, bitrate changes were minimal or non-existent
462 'libfdk_aac': (1, 5),
467 q
= limits
[1] + (limits
[0] - limits
[1]) * (self
._preferredquality
/ 10)
468 if codec
== 'libfdk_aac':
469 return ['-vbr', f
'{int(q)}']
470 return ['-q:a', f
'{q}']
472 def run_ffmpeg(self
, path
, out_path
, codec
, more_opts
):
476 acodec_opts
= ['-acodec', codec
]
477 opts
= ['-vn', *acodec_opts
, *more_opts
]
479 FFmpegPostProcessor
.run_ffmpeg(self
, path
, out_path
, opts
)
480 except FFmpegPostProcessorError
as err
:
481 raise PostProcessingError(f
'audio conversion failed: {err.msg}')
483 @PostProcessor._restrict
_to
(images
=False)
484 def run(self
, information
):
485 orig_path
= path
= information
['filepath']
486 target_format
, _skip_msg
= resolve_mapping(information
['ext'], self
.mapping
)
487 if target_format
== 'best' and information
['ext'] in self
.COMMON_AUDIO_EXTS
:
488 target_format
, _skip_msg
= None, 'the file is already in a common audio format'
489 if not target_format
:
490 self
.to_screen(f
'Not converting audio {orig_path}; {_skip_msg}')
491 return [], information
493 filecodec
= self
.get_audio_codec(path
)
494 if filecodec
is None:
495 raise PostProcessingError('WARNING: unable to obtain file audio codec with ffprobe')
497 if filecodec
== 'aac' and target_format
in ('m4a', 'best'):
498 # Lossless, but in another container
499 extension
, _
, more_opts
, acodec
= *ACODECS
['m4a'], 'copy'
500 elif target_format
== 'best' or target_format
== filecodec
:
501 # Lossless if possible
503 extension
, _
, more_opts
, acodec
= *ACODECS
[filecodec
], 'copy'
505 extension
, acodec
, more_opts
= ACODECS
['mp3']
507 # We convert the audio (lossy if codec is lossy)
508 extension
, acodec
, more_opts
= ACODECS
[target_format
]
509 if acodec
== 'aac' and self
._features
.get('fdk'):
510 acodec
, more_opts
= 'libfdk_aac', []
512 more_opts
= list(more_opts
)
514 more_opts
= self
._quality
_args
(acodec
)
516 temp_path
= new_path
= replace_extension(path
, extension
, information
['ext'])
520 self
.to_screen(f
'Not converting audio {orig_path}; file is already in target format {target_format}')
521 return [], information
522 orig_path
= prepend_extension(path
, 'orig')
523 temp_path
= prepend_extension(path
, 'temp')
524 if (self
._nopostoverwrites
and os
.path
.exists(new_path
)
525 and os
.path
.exists(orig_path
)):
526 self
.to_screen(f
'Post-process file {new_path} exists, skipping')
527 return [], information
529 self
.to_screen(f
'Destination: {new_path}')
530 self
.run_ffmpeg(path
, temp_path
, acodec
, more_opts
)
532 os
.replace(path
, orig_path
)
533 os
.replace(temp_path
, new_path
)
534 information
['filepath'] = new_path
535 information
['ext'] = extension
537 # Try to update the date time for extracted audio file.
538 if information
.get('filetime') is not None:
540 new_path
, time
.time(), information
['filetime'], errnote
='Cannot update utime of audio file')
542 return [orig_path
], information
545 class FFmpegVideoConvertorPP(FFmpegPostProcessor
):
547 *sorted((*MEDIA_EXTENSIONS
.common_video
, 'gif')),
548 *sorted((*MEDIA_EXTENSIONS
.common_audio
, 'aac', 'vorbis')),
550 FORMAT_RE
= create_mapping_re(SUPPORTED_EXTS
)
551 _ACTION
= 'converting'
553 def __init__(self
, downloader
=None, preferedformat
=None):
554 super().__init
__(downloader
)
555 self
.mapping
= preferedformat
558 def _options(target_ext
):
559 yield from FFmpegPostProcessor
.stream_copy_opts(False)
560 if target_ext
== 'avi':
561 yield from ('-c:v', 'libxvid', '-vtag', 'XVID')
563 @PostProcessor._restrict
_to
(images
=False)
565 filename
, source_ext
= info
['filepath'], info
['ext'].lower()
566 target_ext
, _skip_msg
= resolve_mapping(source_ext
, self
.mapping
)
568 self
.to_screen(f
'Not {self._ACTION} media file "{filename}"; {_skip_msg}')
571 outpath
= replace_extension(filename
, target_ext
, source_ext
)
572 self
.to_screen(f
'{self._ACTION.title()} video from {source_ext} to {target_ext}; Destination: {outpath}')
573 self
.run_ffmpeg(filename
, outpath
, self
._options
(target_ext
))
575 info
['filepath'] = outpath
576 info
['format'] = info
['ext'] = target_ext
577 return [filename
], info
580 class FFmpegVideoRemuxerPP(FFmpegVideoConvertorPP
):
584 def _options(target_ext
):
585 return FFmpegPostProcessor
.stream_copy_opts()
588 class FFmpegEmbedSubtitlePP(FFmpegPostProcessor
):
589 SUPPORTED_EXTS
= ('mp4', 'mov', 'm4a', 'webm', 'mkv', 'mka')
591 def __init__(self
, downloader
=None, already_have_subtitle
=False):
592 super().__init
__(downloader
)
593 self
._already
_have
_subtitle
= already_have_subtitle
595 @PostProcessor._restrict
_to
(images
=False)
597 if info
['ext'] not in self
.SUPPORTED_EXTS
:
598 self
.to_screen(f
'Subtitles can only be embedded in {", ".join(self.SUPPORTED_EXTS)} files')
600 subtitles
= info
.get('requested_subtitles')
602 self
.to_screen('There aren\'t any subtitles to embed')
605 filename
= info
['filepath']
607 # Disabled temporarily. There needs to be a way to override this
608 # in case of duration actually mismatching in extractor
609 # See: https://github.com/yt-dlp/yt-dlp/issues/1870, https://github.com/yt-dlp/yt-dlp/issues/1385
611 if info.get('duration') and not info.get('__real_download') and self._duration_mismatch(
612 self._get_real_video_duration(filename, False), info['duration']):
613 self.to_screen(f'Skipping {self.pp_key()} since the real and expected durations mismatch')
618 sub_langs
, sub_names
, sub_filenames
= [], [], []
619 webm_vtt_warn
= False
622 for lang
, sub_info
in subtitles
.items():
623 if not os
.path
.exists(sub_info
.get('filepath', '')):
624 self
.report_warning(f
'Skipping embedding {lang} subtitle because the file is missing')
626 sub_ext
= sub_info
['ext']
627 if sub_ext
== 'json':
628 self
.report_warning('JSON subtitles cannot be embedded')
629 elif ext
!= 'webm' or ext
== 'webm' and sub_ext
== 'vtt':
630 sub_langs
.append(lang
)
631 sub_names
.append(sub_info
.get('name'))
632 sub_filenames
.append(sub_info
['filepath'])
634 if not webm_vtt_warn
and ext
== 'webm' and sub_ext
!= 'vtt':
636 self
.report_warning('Only WebVTT subtitles can be embedded in webm files')
637 if not mp4_ass_warn
and ext
== 'mp4' and sub_ext
== 'ass':
639 self
.report_warning('ASS subtitles cannot be properly embedded in mp4 files; expect issues')
644 input_files
= [filename
, *sub_filenames
]
647 *self
.stream_copy_opts(ext
=info
['ext']),
648 # Don't copy the existing subtitles, we may be running the
649 # postprocessor a second time
652 for i
, (lang
, name
) in enumerate(zip(sub_langs
, sub_names
)):
653 opts
.extend(['-map', f
'{i + 1}:0'])
654 lang_code
= ISO639Utils
.short2long(lang
) or lang
655 opts
.extend([f
'-metadata:s:s:{i}', f
'language={lang_code}'])
657 opts
.extend([f
'-metadata:s:s:{i}', f
'handler_name={name}',
658 f
'-metadata:s:s:{i}', f
'title={name}'])
660 temp_filename
= prepend_extension(filename
, 'temp')
661 self
.to_screen(f
'Embedding subtitles in "{filename}"')
662 self
.run_ffmpeg_multiple_files(input_files
, temp_filename
, opts
)
663 os
.replace(temp_filename
, filename
)
665 files_to_delete
= [] if self
._already
_have
_subtitle
else sub_filenames
666 return files_to_delete
, info
669 class FFmpegMetadataPP(FFmpegPostProcessor
):
671 def __init__(self
, downloader
, add_metadata
=True, add_chapters
=True, add_infojson
='if_exists'):
672 FFmpegPostProcessor
.__init
__(self
, downloader
)
673 self
._add
_metadata
= add_metadata
674 self
._add
_chapters
= add_chapters
675 self
._add
_infojson
= add_infojson
678 def _options(target_ext
):
679 audio_only
= target_ext
== 'm4a'
680 yield from FFmpegPostProcessor
.stream_copy_opts(not audio_only
)
682 yield from ('-vn', '-acodec', 'copy')
684 @PostProcessor._restrict
_to
(images
=False)
686 self
._fixup
_chapters
(info
)
687 filename
, metadata_filename
= info
['filepath'], None
688 files_to_delete
, options
= [], []
689 if self
._add
_chapters
and info
.get('chapters'):
690 metadata_filename
= replace_extension(filename
, 'meta')
691 options
.extend(self
._get
_chapter
_opts
(info
['chapters'], metadata_filename
))
692 files_to_delete
.append(metadata_filename
)
693 if self
._add
_metadata
:
694 options
.extend(self
._get
_metadata
_opts
(info
))
696 if self
._add
_infojson
:
697 if info
['ext'] in ('mkv', 'mka'):
698 infojson_filename
= info
.get('infojson_filename')
699 options
.extend(self
._get
_infojson
_opts
(info
, infojson_filename
))
700 if not infojson_filename
:
701 files_to_delete
.append(info
.get('infojson_filename'))
702 elif self
._add
_infojson
is True:
703 self
.to_screen('The info-json can only be attached to mkv/mka files')
706 self
.to_screen('There isn\'t any metadata to add')
709 temp_filename
= prepend_extension(filename
, 'temp')
710 self
.to_screen(f
'Adding metadata to "{filename}"')
711 self
.run_ffmpeg_multiple_files(
712 (filename
, metadata_filename
), temp_filename
,
713 itertools
.chain(self
._options
(info
['ext']), *options
))
714 self
._delete
_downloaded
_files
(*files_to_delete
)
715 os
.replace(temp_filename
, filename
)
719 def _get_chapter_opts(chapters
, metadata_filename
):
720 with
open(metadata_filename
, 'w', encoding
='utf-8') as f
:
721 def ffmpeg_escape(text
):
722 return re
.sub(r
'([\\=;#\n])', r
'\\\1', text
)
724 metadata_file_content
= ';FFMETADATA1\n'
725 for chapter
in chapters
:
726 metadata_file_content
+= '[CHAPTER]\nTIMEBASE=1/1000\n'
727 metadata_file_content
+= 'START=%d\n' % (chapter
['start_time'] * 1000)
728 metadata_file_content
+= 'END=%d\n' % (chapter
['end_time'] * 1000)
729 chapter_title
= chapter
.get('title')
731 metadata_file_content
+= f
'title={ffmpeg_escape(chapter_title)}\n'
732 f
.write(metadata_file_content
)
733 yield ('-map_metadata', '1')
735 def _get_metadata_opts(self
, info
):
737 metadata
= collections
.defaultdict(dict)
739 def add(meta_list
, info_list
=None):
741 info
[key
] for key
in [f
'{meta_prefix}_', *variadic(info_list
or meta_list
)]
742 if info
.get(key
) is not None), None)
743 if value
not in ('', None):
744 value
= ', '.join(map(str, variadic(value
)))
745 value
= value
.replace('\0', '') # nul character cannot be passed in command line
746 metadata
['common'].update({meta_f
: value
for meta_f
in variadic(meta_list
)})
748 # Info on media metadata/metadata supported by ffmpeg:
749 # https://wiki.multimedia.cx/index.php/FFmpeg_Metadata
750 # https://kdenlive.org/en/project/adding-meta-data-to-mp4-video/
751 # https://kodi.wiki/view/Video_file_tagging
753 add('title', ('track', 'title'))
754 add('date', 'upload_date')
755 add(('description', 'synopsis'), 'description')
756 add(('purl', 'comment'), 'webpage_url')
757 add('track', 'track_number')
758 add('artist', ('artist', 'artists', 'creator', 'creators', 'uploader', 'uploader_id'))
759 add('composer', ('composer', 'composers'))
760 add('genre', ('genre', 'genres'))
762 add('album_artist', ('album_artist', 'album_artists'))
763 add('disc', 'disc_number')
764 add('show', 'series')
766 add('episode_id', ('episode', 'episode_id'))
767 add('episode_sort', 'episode_number')
768 if 'embed-metadata' in self
.get_param('compat_opts', []):
769 add('comment', 'description')
770 metadata
['common'].pop('synopsis', None)
772 meta_regex
= rf
'{re.escape(meta_prefix)}(?P<i>\d+)?_(?P<key>.+)'
773 for key
, value
in info
.items():
774 mobj
= re
.fullmatch(meta_regex
, key
)
775 if value
is not None and mobj
:
776 metadata
[mobj
.group('i') or 'common'][mobj
.group('key')] = value
.replace('\0', '')
778 # Write id3v1 metadata also since Windows Explorer can't handle id3v2 tags
779 yield ('-write_id3v1', '1')
781 for name
, value
in metadata
['common'].items():
782 yield ('-metadata', f
'{name}={value}')
785 for fmt
in info
.get('requested_formats') or [info
]:
786 stream_count
= 2 if 'none' not in (fmt
.get('vcodec'), fmt
.get('acodec')) else 1
787 lang
= ISO639Utils
.short2long(fmt
.get('language') or '') or fmt
.get('language')
788 for i
in range(stream_idx
, stream_idx
+ stream_count
):
790 metadata
[str(i
)].setdefault('language', lang
)
791 for name
, value
in metadata
[str(i
)].items():
792 yield (f
'-metadata:s:{i}', f
'{name}={value}')
793 stream_idx
+= stream_count
795 def _get_infojson_opts(self
, info
, infofn
):
796 if not infofn
or not os
.path
.exists(infofn
):
797 if self
._add
_infojson
is not True:
799 infofn
= infofn
or '%s.temp' % (
800 self
._downloader
.prepare_filename(info
, 'infojson')
801 or replace_extension(self
._downloader
.prepare_filename(info
), 'info.json', info
['ext']))
802 if not self
._downloader
._ensure
_dir
_exists
(infofn
):
804 self
.write_debug(f
'Writing info-json to: {infofn}')
805 write_json_file(self
._downloader
.sanitize_info(info
, self
.get_param('clean_infojson', True)), infofn
)
806 info
['infojson_filename'] = infofn
808 old_stream
, new_stream
= self
.get_stream_number(info
['filepath'], ('tags', 'mimetype'), 'application/json')
809 if old_stream
is not None:
810 yield ('-map', f
'-0:{old_stream}')
814 '-attach', self
._ffmpeg
_filename
_argument
(infofn
),
815 f
'-metadata:s:{new_stream}', 'mimetype=application/json',
816 f
'-metadata:s:{new_stream}', 'filename=info.json',
820 class FFmpegMergerPP(FFmpegPostProcessor
):
821 SUPPORTED_EXTS
= MEDIA_EXTENSIONS
.common_video
823 @PostProcessor._restrict
_to
(images
=False)
825 filename
= info
['filepath']
826 temp_filename
= prepend_extension(filename
, 'temp')
827 args
= ['-c', 'copy']
829 for (i
, fmt
) in enumerate(info
['requested_formats']):
830 if fmt
.get('acodec') != 'none':
831 args
.extend(['-map', f
'{i}:a:0'])
832 aac_fixup
= fmt
['protocol'].startswith('m3u8') and self
.get_audio_codec(fmt
['filepath']) == 'aac'
834 args
.extend([f
'-bsf:a:{audio_streams}', 'aac_adtstoasc'])
836 if fmt
.get('vcodec') != 'none':
837 args
.extend(['-map', f
'{i}:v:0'])
838 self
.to_screen(f
'Merging formats into "{filename}"')
839 self
.run_ffmpeg_multiple_files(info
['__files_to_merge'], temp_filename
, args
)
840 os
.rename(temp_filename
, filename
)
841 return info
['__files_to_merge'], info
844 # TODO: figure out merge-capable ffmpeg version
845 if self
.basename
!= 'avconv':
848 required_version
= '10-0'
849 if is_outdated_version(
850 self
._versions
[self
.basename
], required_version
):
851 warning
= (f
'Your copy of {self.basename} is outdated and unable to properly mux separate video and audio files, '
852 'yt-dlp will download single file media. '
853 f
'Update {self.basename} to version {required_version} or newer to fix this.')
854 self
.report_warning(warning
)
859 class FFmpegFixupPostProcessor(FFmpegPostProcessor
):
860 def _fixup(self
, msg
, filename
, options
):
861 temp_filename
= prepend_extension(filename
, 'temp')
863 self
.to_screen(f
'{msg} of "{filename}"')
864 self
.run_ffmpeg(filename
, temp_filename
, options
)
866 os
.replace(temp_filename
, filename
)
869 class FFmpegFixupStretchedPP(FFmpegFixupPostProcessor
):
870 @PostProcessor._restrict
_to
(images
=False, audio
=False)
872 stretched_ratio
= info
.get('stretched_ratio')
873 if stretched_ratio
not in (None, 1):
874 self
._fixup
('Fixing aspect ratio', info
['filepath'], [
875 *self
.stream_copy_opts(), '-aspect', f
'{stretched_ratio:f}'])
879 class FFmpegFixupM4aPP(FFmpegFixupPostProcessor
):
880 @PostProcessor._restrict
_to
(images
=False, video
=False)
882 if info
.get('container') == 'm4a_dash':
883 self
._fixup
('Correcting container', info
['filepath'], [*self
.stream_copy_opts(), '-f', 'mp4'])
887 class FFmpegFixupM3u8PP(FFmpegFixupPostProcessor
):
888 def _needs_fixup(self
, info
):
889 yield info
['ext'] in ('mp4', 'm4a')
890 yield info
['protocol'].startswith('m3u8')
892 metadata
= self
.get_metadata_object(info
['filepath'])
893 except PostProcessingError
as e
:
894 self
.report_warning(f
'Unable to extract metadata: {e.msg}')
897 yield traverse_obj(metadata
, ('format', 'format_name'), casesense
=False) == 'mpegts'
899 @PostProcessor._restrict
_to
(images
=False)
901 if all(self
._needs
_fixup
(info
)):
903 if self
.get_audio_codec(info
['filepath']) == 'aac':
904 args
.extend(['-bsf:a', 'aac_adtstoasc'])
905 self
._fixup
('Fixing MPEG-TS in MP4 container', info
['filepath'], [
906 *self
.stream_copy_opts(), *args
])
910 class FFmpegFixupTimestampPP(FFmpegFixupPostProcessor
):
912 def __init__(self
, downloader
=None, trim
=0.001):
913 # "trim" should be used when the video contains unintended packets
914 super().__init
__(downloader
)
915 assert isinstance(trim
, (int, float))
916 self
.trim
= str(trim
)
918 @PostProcessor._restrict
_to
(images
=False)
920 if not self
._features
.get('setts'):
922 'A re-encode is needed to fix timestamps in older versions of ffmpeg. '
923 'Please install ffmpeg 4.4 or later to fixup without re-encoding')
924 opts
= ['-vf', 'setpts=PTS-STARTPTS']
926 opts
= ['-c', 'copy', '-bsf', 'setts=ts=TS-STARTPTS']
927 self
._fixup
('Fixing frame timestamp', info
['filepath'], [*opts
, *self
.stream_copy_opts(False), '-ss', self
.trim
])
931 class FFmpegCopyStreamPP(FFmpegFixupPostProcessor
):
932 MESSAGE
= 'Copying stream'
934 @PostProcessor._restrict
_to
(images
=False)
936 self
._fixup
(self
.MESSAGE
, info
['filepath'], self
.stream_copy_opts())
940 class FFmpegFixupDurationPP(FFmpegCopyStreamPP
):
941 MESSAGE
= 'Fixing video duration'
944 class FFmpegFixupDuplicateMoovPP(FFmpegCopyStreamPP
):
945 MESSAGE
= 'Fixing duplicate MOOV atoms'
948 class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor
):
949 SUPPORTED_EXTS
= MEDIA_EXTENSIONS
.subtitles
951 def __init__(self
, downloader
=None, format
=None):
952 super().__init
__(downloader
)
956 subs
= info
.get('requested_subtitles')
957 new_ext
= self
.format
959 if new_format
== 'vtt':
960 new_format
= 'webvtt'
962 self
.to_screen('There aren\'t any subtitles to convert')
964 self
.to_screen('Converting subtitles')
966 for lang
, sub
in subs
.items():
967 if not os
.path
.exists(sub
.get('filepath', '')):
968 self
.report_warning(f
'Skipping embedding {lang} subtitle because the file is missing')
972 self
.to_screen(f
'Subtitle file for {new_ext} is already in the requested format')
976 'You have requested to convert json subtitles into another format, '
977 'which is currently not possible')
979 old_file
= sub
['filepath']
980 sub_filenames
.append(old_file
)
981 new_file
= replace_extension(old_file
, new_ext
)
983 if ext
in ('dfxp', 'ttml', 'tt'):
985 'You have requested to convert dfxp (TTML) subtitles into another format, '
986 'which results in style information loss')
989 srt_file
= replace_extension(old_file
, 'srt')
991 with
open(dfxp_file
, 'rb') as f
:
992 srt_data
= dfxp2srt(f
.read())
994 with
open(srt_file
, 'w', encoding
='utf-8') as f
:
1001 'filepath': srt_file
,
1004 if new_ext
== 'srt':
1007 sub_filenames
.append(srt_file
)
1009 self
.run_ffmpeg(old_file
, new_file
, ['-f', new_format
])
1011 with
open(new_file
, encoding
='utf-8') as f
:
1015 'filepath': new_file
,
1018 info
['__files_to_move'][new_file
] = replace_extension(
1019 info
['__files_to_move'][sub
['filepath']], new_ext
)
1021 return sub_filenames
, info
1024 class FFmpegSplitChaptersPP(FFmpegPostProcessor
):
1025 def __init__(self
, downloader
, force_keyframes
=False):
1026 FFmpegPostProcessor
.__init
__(self
, downloader
)
1027 self
._force
_keyframes
= force_keyframes
1029 def _prepare_filename(self
, number
, chapter
, info
):
1032 'section_number': number
,
1033 'section_title': chapter
.get('title'),
1034 'section_start': chapter
.get('start_time'),
1035 'section_end': chapter
.get('end_time'),
1037 return self
._downloader
.prepare_filename(info
, 'chapter')
1039 def _ffmpeg_args_for_chapter(self
, number
, chapter
, info
):
1040 destination
= self
._prepare
_filename
(number
, chapter
, info
)
1041 if not self
._downloader
._ensure
_dir
_exists
(destination
):
1044 chapter
['filepath'] = destination
1045 self
.to_screen('Chapter %03d; Destination: %s' % (number
, destination
))
1048 ['-ss', str(chapter
['start_time']),
1049 '-t', str(chapter
['end_time'] - chapter
['start_time'])])
1051 @PostProcessor._restrict
_to
(images
=False)
1052 def run(self
, info
):
1053 self
._fixup
_chapters
(info
)
1054 chapters
= info
.get('chapters') or []
1056 self
.to_screen('Chapter information is unavailable')
1059 in_file
= info
['filepath']
1060 if self
._force
_keyframes
and len(chapters
) > 1:
1061 in_file
= self
.force_keyframes(in_file
, (c
['start_time'] for c
in chapters
))
1062 self
.to_screen(f
'Splitting video by chapters; {len(chapters)} chapters found')
1063 for idx
, chapter
in enumerate(chapters
):
1064 destination
, opts
= self
._ffmpeg
_args
_for
_chapter
(idx
+ 1, chapter
, info
)
1065 self
.real_run_ffmpeg([(in_file
, opts
)], [(destination
, self
.stream_copy_opts())])
1066 if in_file
!= info
['filepath']:
1067 self
._delete
_downloaded
_files
(in_file
, msg
=None)
1071 class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor
):
1072 SUPPORTED_EXTS
= MEDIA_EXTENSIONS
.thumbnails
1073 FORMAT_RE
= create_mapping_re(SUPPORTED_EXTS
)
1075 def __init__(self
, downloader
=None, format
=None):
1076 super().__init
__(downloader
)
1077 self
.mapping
= format
1080 def is_webp(cls
, path
):
1081 deprecation_warning(f
'{cls.__module__}.{cls.__name__}.is_webp is deprecated')
1082 return imghdr
.what(path
) == 'webp'
1084 def fixup_webp(self
, info
, idx
=-1):
1085 thumbnail_filename
= info
['thumbnails'][idx
]['filepath']
1086 _
, thumbnail_ext
= os
.path
.splitext(thumbnail_filename
)
1088 if thumbnail_ext
.lower() != '.webp' and imghdr
.what(thumbnail_filename
) == 'webp':
1089 self
.to_screen(f
'Correcting thumbnail "{thumbnail_filename}" extension to webp')
1090 webp_filename
= replace_extension(thumbnail_filename
, 'webp')
1091 os
.replace(thumbnail_filename
, webp_filename
)
1092 info
['thumbnails'][idx
]['filepath'] = webp_filename
1093 info
['__files_to_move'][webp_filename
] = replace_extension(
1094 info
['__files_to_move'].pop(thumbnail_filename
), 'webp')
1097 def _options(target_ext
):
1098 yield from ('-update', '1')
1099 if target_ext
== 'jpg':
1100 yield from ('-bsf:v', 'mjpeg2jpeg')
1102 def convert_thumbnail(self
, thumbnail_filename
, target_ext
):
1103 thumbnail_conv_filename
= replace_extension(thumbnail_filename
, target_ext
)
1105 self
.to_screen(f
'Converting thumbnail "{thumbnail_filename}" to {target_ext}')
1106 _
, source_ext
= os
.path
.splitext(thumbnail_filename
)
1107 self
.real_run_ffmpeg(
1108 [(thumbnail_filename
, [] if source_ext
== '.gif' else ['-f', 'image2', '-pattern_type', 'none'])],
1109 [(thumbnail_conv_filename
, self
._options
(target_ext
))])
1110 return thumbnail_conv_filename
1112 def run(self
, info
):
1113 files_to_delete
= []
1114 has_thumbnail
= False
1116 for idx
, thumbnail_dict
in enumerate(info
.get('thumbnails') or []):
1117 original_thumbnail
= thumbnail_dict
.get('filepath')
1118 if not original_thumbnail
:
1120 has_thumbnail
= True
1121 self
.fixup_webp(info
, idx
)
1122 original_thumbnail
= thumbnail_dict
['filepath'] # Path can change during fixup
1123 thumbnail_ext
= os
.path
.splitext(original_thumbnail
)[1][1:].lower()
1124 if thumbnail_ext
== 'jpeg':
1125 thumbnail_ext
= 'jpg'
1126 target_ext
, _skip_msg
= resolve_mapping(thumbnail_ext
, self
.mapping
)
1128 self
.to_screen(f
'Not converting thumbnail "{original_thumbnail}"; {_skip_msg}')
1130 thumbnail_dict
['filepath'] = self
.convert_thumbnail(original_thumbnail
, target_ext
)
1131 files_to_delete
.append(original_thumbnail
)
1132 info
['__files_to_move'][thumbnail_dict
['filepath']] = replace_extension(
1133 info
['__files_to_move'][original_thumbnail
], target_ext
)
1135 if not has_thumbnail
:
1136 self
.to_screen('There aren\'t any thumbnails to convert')
1137 return files_to_delete
, info
1140 class FFmpegConcatPP(FFmpegPostProcessor
):
1141 def __init__(self
, downloader
, only_multi_video
=False):
1142 self
._only
_multi
_video
= only_multi_video
1143 super().__init
__(downloader
)
1145 def _get_codecs(self
, file):
1146 codecs
= traverse_obj(self
.get_metadata_object(file), ('streams', ..., 'codec_name'))
1147 self
.write_debug(f
'Codecs = {", ".join(codecs)}')
1148 return tuple(codecs
)
1150 def concat_files(self
, in_files
, out_file
):
1151 if not self
._downloader
._ensure
_dir
_exists
(out_file
):
1153 if len(in_files
) == 1:
1154 if os
.path
.realpath(in_files
[0]) != os
.path
.realpath(out_file
):
1155 self
.to_screen(f
'Moving "{in_files[0]}" to "{out_file}"')
1156 os
.replace(in_files
[0], out_file
)
1159 if len(set(map(self
._get
_codecs
, in_files
))) > 1:
1160 raise PostProcessingError(
1161 'The files have different streams/codecs and cannot be concatenated. '
1162 'Either select different formats or --recode-video them to a common format')
1164 self
.to_screen(f
'Concatenating {len(in_files)} files; Destination: {out_file}')
1165 super().concat_files(in_files
, out_file
)
1168 @PostProcessor._restrict
_to
(images
=False, simulated
=False)
1169 def run(self
, info
):
1170 entries
= info
.get('entries') or []
1171 if not any(entries
) or (self
._only
_multi
_video
and info
['_type'] != 'multi_video'):
1173 elif traverse_obj(entries
, (..., lambda k
, v
: k
== 'requested_downloads' and len(v
) > 1)):
1174 raise PostProcessingError('Concatenation is not supported when downloading multiple separate formats')
1176 in_files
= traverse_obj(entries
, (..., 'requested_downloads', 0, 'filepath')) or []
1177 if len(in_files
) < len(entries
):
1178 raise PostProcessingError('Aborting concatenation because some downloads failed')
1180 exts
= traverse_obj(entries
, (..., 'requested_downloads', 0, 'ext'), (..., 'ext'))
1181 ie_copy
= collections
.ChainMap({'ext': exts
[0] if len(set(exts
)) == 1 else 'mkv'},
1182 info
, self
._downloader
._playlist
_infodict
(info
))
1183 out_file
= self
._downloader
.prepare_filename(ie_copy
, 'pl_video')
1185 files_to_delete
= self
.concat_files(in_files
, out_file
)
1187 info
['requested_downloads'] = [{
1188 'filepath': out_file
,
1189 'ext': ie_copy
['ext'],
1191 return files_to_delete
, info