ffmpeg.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. import collections
  2. import contextvars
  3. import functools
  4. import itertools
  5. import json
  6. import os
  7. import re
  8. import subprocess
  9. import time
  10. from .common import PostProcessor
  11. from ..compat import imghdr
  12. from ..utils import (
  13. MEDIA_EXTENSIONS,
  14. ISO639Utils,
  15. Popen,
  16. PostProcessingError,
  17. _get_exe_version_output,
  18. deprecation_warning,
  19. detect_exe_version,
  20. determine_ext,
  21. dfxp2srt,
  22. encodeArgument,
  23. encodeFilename,
  24. filter_dict,
  25. float_or_none,
  26. is_outdated_version,
  27. orderedSet,
  28. prepend_extension,
  29. replace_extension,
  30. shell_quote,
  31. traverse_obj,
  32. variadic,
  33. write_json_file,
  34. )
  35. EXT_TO_OUT_FORMATS = {
  36. 'aac': 'adts',
  37. 'flac': 'flac',
  38. 'm4a': 'ipod',
  39. 'mka': 'matroska',
  40. 'mkv': 'matroska',
  41. 'mpg': 'mpeg',
  42. 'ogv': 'ogg',
  43. 'ts': 'mpegts',
  44. 'wma': 'asf',
  45. 'wmv': 'asf',
  46. 'weba': 'webm',
  47. 'vtt': 'webvtt',
  48. }
  49. ACODECS = {
  50. # name: (ext, encoder, opts)
  51. 'mp3': ('mp3', 'libmp3lame', ()),
  52. 'aac': ('m4a', 'aac', ('-f', 'adts')),
  53. 'm4a': ('m4a', 'aac', ('-bsf:a', 'aac_adtstoasc')),
  54. 'opus': ('opus', 'libopus', ()),
  55. 'vorbis': ('ogg', 'libvorbis', ()),
  56. 'flac': ('flac', 'flac', ()),
  57. 'alac': ('m4a', None, ('-acodec', 'alac')),
  58. 'wav': ('wav', None, ('-f', 'wav')),
  59. }
  60. def create_mapping_re(supported):
  61. return re.compile(r'{0}(?:/{0})*$'.format(r'(?:\s*\w+\s*>)?\s*(?:{})\s*'.format('|'.join(supported))))
  62. def resolve_mapping(source, mapping):
  63. """
  64. Get corresponding item from a mapping string like 'A>B/C>D/E'
  65. @returns (target, error_message)
  66. """
  67. for pair in mapping.lower().split('/'):
  68. kv = pair.split('>', 1)
  69. if len(kv) == 1 or kv[0].strip() == source:
  70. target = kv[-1].strip()
  71. if target == source:
  72. return target, f'already is in target format {source}'
  73. return target, None
  74. return None, f'could not find a mapping for {source}'
  75. class FFmpegPostProcessorError(PostProcessingError):
  76. pass
  77. class FFmpegPostProcessor(PostProcessor):
  78. _ffmpeg_location = contextvars.ContextVar('ffmpeg_location', default=None)
  79. def __init__(self, downloader=None):
  80. PostProcessor.__init__(self, downloader)
  81. self._prefer_ffmpeg = self.get_param('prefer_ffmpeg', True)
  82. self._paths = self._determine_executables()
  83. @staticmethod
  84. def get_versions_and_features(downloader=None):
  85. pp = FFmpegPostProcessor(downloader)
  86. return pp._versions, pp._features
  87. @staticmethod
  88. def get_versions(downloader=None):
  89. return FFmpegPostProcessor.get_versions_and_features(downloader)[0]
  90. _ffmpeg_to_avconv = {'ffmpeg': 'avconv', 'ffprobe': 'avprobe'}
  91. def _determine_executables(self):
  92. programs = [*self._ffmpeg_to_avconv.keys(), *self._ffmpeg_to_avconv.values()]
  93. location = self.get_param('ffmpeg_location', self._ffmpeg_location.get())
  94. if location is None:
  95. return {p: p for p in programs}
  96. if not os.path.exists(location):
  97. self.report_warning(
  98. f'ffmpeg-location {location} does not exist! Continuing without ffmpeg', only_once=True)
  99. return {}
  100. elif os.path.isdir(location):
  101. dirname, basename, filename = location, None, None
  102. else:
  103. filename = os.path.basename(location)
  104. basename = next((p for p in programs if p in filename), 'ffmpeg')
  105. dirname = os.path.dirname(os.path.abspath(location))
  106. if basename in self._ffmpeg_to_avconv:
  107. self._prefer_ffmpeg = True
  108. paths = {p: os.path.join(dirname, p) for p in programs}
  109. if basename and basename in filename:
  110. for p in programs:
  111. path = os.path.join(dirname, filename.replace(basename, p))
  112. if os.path.exists(path):
  113. paths[p] = path
  114. if basename:
  115. paths[basename] = location
  116. return paths
  117. _version_cache, _features_cache = {None: None}, {}
  118. def _get_ffmpeg_version(self, prog):
  119. path = self._paths.get(prog)
  120. if path in self._version_cache:
  121. return self._version_cache[path], self._features_cache.get(path, {})
  122. out = _get_exe_version_output(path, ['-bsfs'])
  123. ver = detect_exe_version(out) if out else False
  124. if ver:
  125. regexs = [
  126. r'(?:\d+:)?([0-9.]+)-[0-9]+ubuntu[0-9.]+$', # Ubuntu, see [1]
  127. r'n([0-9.]+)$', # Arch Linux
  128. # 1. http://www.ducea.com/2006/06/17/ubuntu-package-version-naming-explanation/
  129. ]
  130. for regex in regexs:
  131. mobj = re.match(regex, ver)
  132. if mobj:
  133. ver = mobj.group(1)
  134. self._version_cache[path] = ver
  135. if prog != 'ffmpeg' or not out:
  136. return ver, {}
  137. mobj = re.search(r'(?m)^\s+libavformat\s+(?:[0-9. ]+)\s+/\s+(?P<runtime>[0-9. ]+)', out)
  138. lavf_runtime_version = mobj.group('runtime').replace(' ', '') if mobj else None
  139. self._features_cache[path] = features = {
  140. 'fdk': '--enable-libfdk-aac' in out,
  141. 'setts': 'setts' in out.splitlines(),
  142. 'needs_adtstoasc': is_outdated_version(lavf_runtime_version, '57.56.100', False),
  143. }
  144. return ver, features
  145. @property
  146. def _versions(self):
  147. return filter_dict({self.basename: self._version, self.probe_basename: self._probe_version})
  148. @functools.cached_property
  149. def basename(self):
  150. _ = self._version # run property
  151. return self.basename
  152. @functools.cached_property
  153. def probe_basename(self):
  154. _ = self._probe_version # run property
  155. return self.probe_basename
  156. def _get_version(self, kind):
  157. executables = (kind, )
  158. if not self._prefer_ffmpeg:
  159. executables = (kind, self._ffmpeg_to_avconv[kind])
  160. basename, version, features = next(filter(
  161. lambda x: x[1], ((p, *self._get_ffmpeg_version(p)) for p in executables)), (None, None, {}))
  162. if kind == 'ffmpeg':
  163. self.basename, self._features = basename, features
  164. else:
  165. self.probe_basename = basename
  166. if basename == self._ffmpeg_to_avconv[kind]:
  167. self.deprecated_feature(f'Support for {self._ffmpeg_to_avconv[kind]} is deprecated and '
  168. f'may be removed in a future version. Use {kind} instead')
  169. return version
  170. @functools.cached_property
  171. def _version(self):
  172. return self._get_version('ffmpeg')
  173. @functools.cached_property
  174. def _probe_version(self):
  175. return self._get_version('ffprobe')
  176. @property
  177. def available(self):
  178. return self.basename is not None
  179. @property
  180. def executable(self):
  181. return self._paths.get(self.basename)
  182. @property
  183. def probe_available(self):
  184. return self.probe_basename is not None
  185. @property
  186. def probe_executable(self):
  187. return self._paths.get(self.probe_basename)
  188. @staticmethod
  189. def stream_copy_opts(copy=True, *, ext=None):
  190. yield from ('-map', '0')
  191. # Don't copy Apple TV chapters track, bin_data
  192. # See https://github.com/yt-dlp/yt-dlp/issues/2, #19042, #19024, https://trac.ffmpeg.org/ticket/6016
  193. yield from ('-dn', '-ignore_unknown')
  194. if copy:
  195. yield from ('-c', 'copy')
  196. if ext in ('mp4', 'mov', 'm4a'):
  197. yield from ('-c:s', 'mov_text')
  198. def check_version(self):
  199. if not self.available:
  200. raise FFmpegPostProcessorError('ffmpeg not found. Please install or provide the path using --ffmpeg-location')
  201. required_version = '10-0' if self.basename == 'avconv' else '1.0'
  202. if is_outdated_version(self._version, required_version):
  203. self.report_warning(f'Your copy of {self.basename} is outdated, update {self.basename} '
  204. f'to version {required_version} or newer if you encounter any errors')
  205. def get_audio_codec(self, path):
  206. if not self.probe_available and not self.available:
  207. raise PostProcessingError('ffprobe and ffmpeg not found. Please install or provide the path using --ffmpeg-location')
  208. try:
  209. if self.probe_available:
  210. cmd = [
  211. encodeFilename(self.probe_executable, True),
  212. encodeArgument('-show_streams')]
  213. else:
  214. cmd = [
  215. encodeFilename(self.executable, True),
  216. encodeArgument('-i')]
  217. cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
  218. self.write_debug(f'{self.basename} command line: {shell_quote(cmd)}')
  219. stdout, stderr, returncode = Popen.run(
  220. cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  221. if returncode != (0 if self.probe_available else 1):
  222. return None
  223. except OSError:
  224. return None
  225. output = stdout if self.probe_available else stderr
  226. if self.probe_available:
  227. audio_codec = None
  228. for line in output.split('\n'):
  229. if line.startswith('codec_name='):
  230. audio_codec = line.split('=')[1].strip()
  231. elif line.strip() == 'codec_type=audio' and audio_codec is not None:
  232. return audio_codec
  233. else:
  234. # Stream #FILE_INDEX:STREAM_INDEX[STREAM_ID](LANGUAGE): CODEC_TYPE: CODEC_NAME
  235. mobj = re.search(
  236. r'Stream\s*#\d+:\d+(?:\[0x[0-9a-f]+\])?(?:\([a-z]{3}\))?:\s*Audio:\s*([0-9a-z]+)',
  237. output)
  238. if mobj:
  239. return mobj.group(1)
  240. return None
  241. def get_metadata_object(self, path, opts=[]):
  242. if self.probe_basename != 'ffprobe':
  243. if self.probe_available:
  244. self.report_warning('Only ffprobe is supported for metadata extraction')
  245. raise PostProcessingError('ffprobe not found. Please install or provide the path using --ffmpeg-location')
  246. self.check_version()
  247. cmd = [
  248. encodeFilename(self.probe_executable, True),
  249. encodeArgument('-hide_banner'),
  250. encodeArgument('-show_format'),
  251. encodeArgument('-show_streams'),
  252. encodeArgument('-print_format'),
  253. encodeArgument('json'),
  254. ]
  255. cmd += opts
  256. cmd.append(self._ffmpeg_filename_argument(path))
  257. self.write_debug(f'ffprobe command line: {shell_quote(cmd)}')
  258. stdout, _, _ = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  259. return json.loads(stdout)
  260. def get_stream_number(self, path, keys, value):
  261. streams = self.get_metadata_object(path)['streams']
  262. num = next(
  263. (i for i, stream in enumerate(streams) if traverse_obj(stream, keys, casesense=False) == value),
  264. None)
  265. return num, len(streams)
  266. def _fixup_chapters(self, info):
  267. last_chapter = traverse_obj(info, ('chapters', -1))
  268. if last_chapter and not last_chapter.get('end_time'):
  269. last_chapter['end_time'] = self._get_real_video_duration(info['filepath'])
  270. def _get_real_video_duration(self, filepath, fatal=True):
  271. try:
  272. duration = float_or_none(
  273. traverse_obj(self.get_metadata_object(filepath), ('format', 'duration')))
  274. if not duration:
  275. raise PostProcessingError('ffprobe returned empty duration')
  276. return duration
  277. except PostProcessingError as e:
  278. if fatal:
  279. raise PostProcessingError(f'Unable to determine video duration: {e.msg}')
  280. def _duration_mismatch(self, d1, d2, tolerance=2):
  281. if not d1 or not d2:
  282. return None
  283. # The duration is often only known to nearest second. So there can be <1sec disparity natually.
  284. # Further excuse an additional <1sec difference.
  285. return abs(d1 - d2) > tolerance
  286. def run_ffmpeg_multiple_files(self, input_paths, out_path, opts, **kwargs):
  287. return self.real_run_ffmpeg(
  288. [(path, []) for path in input_paths],
  289. [(out_path, opts)], **kwargs)
  290. def real_run_ffmpeg(self, input_path_opts, output_path_opts, *, expected_retcodes=(0,)):
  291. self.check_version()
  292. oldest_mtime = min(
  293. os.stat(encodeFilename(path)).st_mtime for path, _ in input_path_opts if path)
  294. cmd = [encodeFilename(self.executable, True), encodeArgument('-y')]
  295. # avconv does not have repeat option
  296. if self.basename == 'ffmpeg':
  297. cmd += [encodeArgument('-loglevel'), encodeArgument('repeat+info')]
  298. def make_args(file, args, name, number):
  299. keys = [f'_{name}{number}', f'_{name}']
  300. if name == 'o':
  301. args += ['-movflags', '+faststart']
  302. if number == 1:
  303. keys.append('')
  304. args += self._configuration_args(self.basename, keys)
  305. if name == 'i':
  306. args.append('-i')
  307. return (
  308. [encodeArgument(arg) for arg in args]
  309. + [encodeFilename(self._ffmpeg_filename_argument(file), True)])
  310. for arg_type, path_opts in (('i', input_path_opts), ('o', output_path_opts)):
  311. cmd += itertools.chain.from_iterable(
  312. make_args(path, list(opts), arg_type, i + 1)
  313. for i, (path, opts) in enumerate(path_opts) if path)
  314. self.write_debug(f'ffmpeg command line: {shell_quote(cmd)}')
  315. _, stderr, returncode = Popen.run(
  316. cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  317. if returncode not in variadic(expected_retcodes):
  318. self.write_debug(stderr)
  319. raise FFmpegPostProcessorError(stderr.strip().splitlines()[-1])
  320. for out_path, _ in output_path_opts:
  321. if out_path:
  322. self.try_utime(out_path, oldest_mtime, oldest_mtime)
  323. return stderr
  324. def run_ffmpeg(self, path, out_path, opts, **kwargs):
  325. return self.run_ffmpeg_multiple_files([path], out_path, opts, **kwargs)
  326. @staticmethod
  327. def _ffmpeg_filename_argument(fn):
  328. # Always use 'file:' because the filename may contain ':' (ffmpeg
  329. # interprets that as a protocol) or can start with '-' (-- is broken in
  330. # ffmpeg, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details)
  331. # Also leave '-' intact in order not to break streaming to stdout.
  332. if fn.startswith(('http://', 'https://')):
  333. return fn
  334. return 'file:' + fn if fn != '-' else fn
  335. @staticmethod
  336. def _quote_for_ffmpeg(string):
  337. # See https://ffmpeg.org/ffmpeg-utils.html#toc-Quoting-and-escaping
  338. # A sequence of '' produces '\'''\'';
  339. # final replace removes the empty '' between \' \'.
  340. string = string.replace("'", r"'\''").replace("'''", "'")
  341. # Handle potential ' at string boundaries.
  342. string = string[1:] if string[0] == "'" else "'" + string
  343. return string[:-1] if string[-1] == "'" else string + "'"
  344. def force_keyframes(self, filename, timestamps):
  345. timestamps = orderedSet(timestamps)
  346. if timestamps[0] == 0:
  347. timestamps = timestamps[1:]
  348. keyframe_file = prepend_extension(filename, 'keyframes.temp')
  349. self.to_screen(f'Re-encoding "{filename}" with appropriate keyframes')
  350. self.run_ffmpeg(filename, keyframe_file, [
  351. *self.stream_copy_opts(False, ext=determine_ext(filename)),
  352. '-force_key_frames', ','.join(f'{t:.6f}' for t in timestamps)])
  353. return keyframe_file
  354. def concat_files(self, in_files, out_file, concat_opts=None):
  355. """
  356. Use concat demuxer to concatenate multiple files having identical streams.
  357. Only inpoint, outpoint, and duration concat options are supported.
  358. See https://ffmpeg.org/ffmpeg-formats.html#concat-1 for details
  359. """
  360. concat_file = f'{out_file}.concat'
  361. self.write_debug(f'Writing concat spec to {concat_file}')
  362. with open(concat_file, 'w', encoding='utf-8') as f:
  363. f.writelines(self._concat_spec(in_files, concat_opts))
  364. out_flags = list(self.stream_copy_opts(ext=determine_ext(out_file)))
  365. self.real_run_ffmpeg(
  366. [(concat_file, ['-hide_banner', '-nostdin', '-f', 'concat', '-safe', '0'])],
  367. [(out_file, out_flags)])
  368. self._delete_downloaded_files(concat_file)
  369. @classmethod
  370. def _concat_spec(cls, in_files, concat_opts=None):
  371. if concat_opts is None:
  372. concat_opts = [{}] * len(in_files)
  373. yield 'ffconcat version 1.0\n'
  374. for file, opts in zip(in_files, concat_opts):
  375. yield f'file {cls._quote_for_ffmpeg(cls._ffmpeg_filename_argument(file))}\n'
  376. # Iterate explicitly to yield the following directives in order, ignoring the rest.
  377. for directive in 'inpoint', 'outpoint', 'duration':
  378. if directive in opts:
  379. yield f'{directive} {opts[directive]}\n'
  380. class FFmpegExtractAudioPP(FFmpegPostProcessor):
  381. COMMON_AUDIO_EXTS = (*MEDIA_EXTENSIONS.common_audio, 'wma')
  382. SUPPORTED_EXTS = tuple(ACODECS.keys())
  383. FORMAT_RE = create_mapping_re(('best', *SUPPORTED_EXTS))
  384. def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
  385. FFmpegPostProcessor.__init__(self, downloader)
  386. self.mapping = preferredcodec or 'best'
  387. self._preferredquality = float_or_none(preferredquality)
  388. self._nopostoverwrites = nopostoverwrites
  389. def _quality_args(self, codec):
  390. if self._preferredquality is None:
  391. return []
  392. elif self._preferredquality > 10:
  393. return ['-b:a', f'{self._preferredquality}k']
  394. limits = {
  395. 'libmp3lame': (10, 0),
  396. 'libvorbis': (0, 10),
  397. # FFmpeg's AAC encoder does not have an upper limit for the value of -q:a.
  398. # Experimentally, with values over 4, bitrate changes were minimal or non-existent
  399. 'aac': (0.1, 4),
  400. 'libfdk_aac': (1, 5),
  401. }.get(codec)
  402. if not limits:
  403. return []
  404. q = limits[1] + (limits[0] - limits[1]) * (self._preferredquality / 10)
  405. if codec == 'libfdk_aac':
  406. return ['-vbr', f'{int(q)}']
  407. return ['-q:a', f'{q}']
  408. def run_ffmpeg(self, path, out_path, codec, more_opts):
  409. if codec is None:
  410. acodec_opts = []
  411. else:
  412. acodec_opts = ['-acodec', codec]
  413. opts = ['-vn', *acodec_opts, *more_opts]
  414. try:
  415. FFmpegPostProcessor.run_ffmpeg(self, path, out_path, opts)
  416. except FFmpegPostProcessorError as err:
  417. raise PostProcessingError(f'audio conversion failed: {err.msg}')
  418. @PostProcessor._restrict_to(images=False)
  419. def run(self, information):
  420. orig_path = path = information['filepath']
  421. target_format, _skip_msg = resolve_mapping(information['ext'], self.mapping)
  422. if target_format == 'best' and information['ext'] in self.COMMON_AUDIO_EXTS:
  423. target_format, _skip_msg = None, 'the file is already in a common audio format'
  424. if not target_format:
  425. self.to_screen(f'Not converting audio {orig_path}; {_skip_msg}')
  426. return [], information
  427. filecodec = self.get_audio_codec(path)
  428. if filecodec is None:
  429. raise PostProcessingError('WARNING: unable to obtain file audio codec with ffprobe')
  430. if filecodec == 'aac' and target_format in ('m4a', 'best'):
  431. # Lossless, but in another container
  432. extension, _, more_opts, acodec = *ACODECS['m4a'], 'copy'
  433. elif target_format == 'best' or target_format == filecodec:
  434. # Lossless if possible
  435. try:
  436. extension, _, more_opts, acodec = *ACODECS[filecodec], 'copy'
  437. except KeyError:
  438. extension, acodec, more_opts = ACODECS['mp3']
  439. else:
  440. # We convert the audio (lossy if codec is lossy)
  441. extension, acodec, more_opts = ACODECS[target_format]
  442. if acodec == 'aac' and self._features.get('fdk'):
  443. acodec, more_opts = 'libfdk_aac', []
  444. more_opts = list(more_opts)
  445. if acodec != 'copy':
  446. more_opts = self._quality_args(acodec)
  447. temp_path = new_path = replace_extension(path, extension, information['ext'])
  448. if new_path == path:
  449. if acodec == 'copy':
  450. self.to_screen(f'Not converting audio {orig_path}; file is already in target format {target_format}')
  451. return [], information
  452. orig_path = prepend_extension(path, 'orig')
  453. temp_path = prepend_extension(path, 'temp')
  454. if (self._nopostoverwrites and os.path.exists(encodeFilename(new_path))
  455. and os.path.exists(encodeFilename(orig_path))):
  456. self.to_screen(f'Post-process file {new_path} exists, skipping')
  457. return [], information
  458. self.to_screen(f'Destination: {new_path}')
  459. self.run_ffmpeg(path, temp_path, acodec, more_opts)
  460. os.replace(path, orig_path)
  461. os.replace(temp_path, new_path)
  462. information['filepath'] = new_path
  463. information['ext'] = extension
  464. # Try to update the date time for extracted audio file.
  465. if information.get('filetime') is not None:
  466. self.try_utime(
  467. new_path, time.time(), information['filetime'], errnote='Cannot update utime of audio file')
  468. return [orig_path], information
  469. class FFmpegVideoConvertorPP(FFmpegPostProcessor):
  470. SUPPORTED_EXTS = (
  471. *sorted((*MEDIA_EXTENSIONS.common_video, 'gif')),
  472. *sorted((*MEDIA_EXTENSIONS.common_audio, 'aac', 'vorbis')),
  473. )
  474. FORMAT_RE = create_mapping_re(SUPPORTED_EXTS)
  475. _ACTION = 'converting'
  476. def __init__(self, downloader=None, preferedformat=None):
  477. super().__init__(downloader)
  478. self.mapping = preferedformat
  479. @staticmethod
  480. def _options(target_ext):
  481. yield from FFmpegPostProcessor.stream_copy_opts(False)
  482. if target_ext == 'avi':
  483. yield from ('-c:v', 'libxvid', '-vtag', 'XVID')
  484. @PostProcessor._restrict_to(images=False)
  485. def run(self, info):
  486. filename, source_ext = info['filepath'], info['ext'].lower()
  487. target_ext, _skip_msg = resolve_mapping(source_ext, self.mapping)
  488. if _skip_msg:
  489. self.to_screen(f'Not {self._ACTION} media file "{filename}"; {_skip_msg}')
  490. return [], info
  491. outpath = replace_extension(filename, target_ext, source_ext)
  492. self.to_screen(f'{self._ACTION.title()} video from {source_ext} to {target_ext}; Destination: {outpath}')
  493. self.run_ffmpeg(filename, outpath, self._options(target_ext))
  494. info['filepath'] = outpath
  495. info['format'] = info['ext'] = target_ext
  496. return [filename], info
  497. class FFmpegVideoRemuxerPP(FFmpegVideoConvertorPP):
  498. _ACTION = 'remuxing'
  499. @staticmethod
  500. def _options(target_ext):
  501. return FFmpegPostProcessor.stream_copy_opts()
  502. class FFmpegEmbedSubtitlePP(FFmpegPostProcessor):
  503. SUPPORTED_EXTS = ('mp4', 'mov', 'm4a', 'webm', 'mkv', 'mka')
  504. def __init__(self, downloader=None, already_have_subtitle=False):
  505. super().__init__(downloader)
  506. self._already_have_subtitle = already_have_subtitle
  507. @PostProcessor._restrict_to(images=False)
  508. def run(self, info):
  509. if info['ext'] not in self.SUPPORTED_EXTS:
  510. self.to_screen(f'Subtitles can only be embedded in {", ".join(self.SUPPORTED_EXTS)} files')
  511. return [], info
  512. subtitles = info.get('requested_subtitles')
  513. if not subtitles:
  514. self.to_screen('There aren\'t any subtitles to embed')
  515. return [], info
  516. filename = info['filepath']
  517. # Disabled temporarily. There needs to be a way to override this
  518. # in case of duration actually mismatching in extractor
  519. # See: https://github.com/yt-dlp/yt-dlp/issues/1870, https://github.com/yt-dlp/yt-dlp/issues/1385
  520. '''
  521. if info.get('duration') and not info.get('__real_download') and self._duration_mismatch(
  522. self._get_real_video_duration(filename, False), info['duration']):
  523. self.to_screen(f'Skipping {self.pp_key()} since the real and expected durations mismatch')
  524. return [], info
  525. '''
  526. ext = info['ext']
  527. sub_langs, sub_names, sub_filenames = [], [], []
  528. webm_vtt_warn = False
  529. mp4_ass_warn = False
  530. for lang, sub_info in subtitles.items():
  531. if not os.path.exists(sub_info.get('filepath', '')):
  532. self.report_warning(f'Skipping embedding {lang} subtitle because the file is missing')
  533. continue
  534. sub_ext = sub_info['ext']
  535. if sub_ext == 'json':
  536. self.report_warning('JSON subtitles cannot be embedded')
  537. elif ext != 'webm' or ext == 'webm' and sub_ext == 'vtt':
  538. sub_langs.append(lang)
  539. sub_names.append(sub_info.get('name'))
  540. sub_filenames.append(sub_info['filepath'])
  541. else:
  542. if not webm_vtt_warn and ext == 'webm' and sub_ext != 'vtt':
  543. webm_vtt_warn = True
  544. self.report_warning('Only WebVTT subtitles can be embedded in webm files')
  545. if not mp4_ass_warn and ext == 'mp4' and sub_ext == 'ass':
  546. mp4_ass_warn = True
  547. self.report_warning('ASS subtitles cannot be properly embedded in mp4 files; expect issues')
  548. if not sub_langs:
  549. return [], info
  550. input_files = [filename, *sub_filenames]
  551. opts = [
  552. *self.stream_copy_opts(ext=info['ext']),
  553. # Don't copy the existing subtitles, we may be running the
  554. # postprocessor a second time
  555. '-map', '-0:s',
  556. ]
  557. for i, (lang, name) in enumerate(zip(sub_langs, sub_names)):
  558. opts.extend(['-map', f'{i + 1}:0'])
  559. lang_code = ISO639Utils.short2long(lang) or lang
  560. opts.extend([f'-metadata:s:s:{i}', f'language={lang_code}'])
  561. if name:
  562. opts.extend([f'-metadata:s:s:{i}', f'handler_name={name}',
  563. f'-metadata:s:s:{i}', f'title={name}'])
  564. temp_filename = prepend_extension(filename, 'temp')
  565. self.to_screen(f'Embedding subtitles in "{filename}"')
  566. self.run_ffmpeg_multiple_files(input_files, temp_filename, opts)
  567. os.replace(temp_filename, filename)
  568. files_to_delete = [] if self._already_have_subtitle else sub_filenames
  569. return files_to_delete, info
  570. class FFmpegMetadataPP(FFmpegPostProcessor):
  571. def __init__(self, downloader, add_metadata=True, add_chapters=True, add_infojson='if_exists'):
  572. FFmpegPostProcessor.__init__(self, downloader)
  573. self._add_metadata = add_metadata
  574. self._add_chapters = add_chapters
  575. self._add_infojson = add_infojson
  576. @staticmethod
  577. def _options(target_ext):
  578. audio_only = target_ext == 'm4a'
  579. yield from FFmpegPostProcessor.stream_copy_opts(not audio_only)
  580. if audio_only:
  581. yield from ('-vn', '-acodec', 'copy')
  582. @PostProcessor._restrict_to(images=False)
  583. def run(self, info):
  584. self._fixup_chapters(info)
  585. filename, metadata_filename = info['filepath'], None
  586. files_to_delete, options = [], []
  587. if self._add_chapters and info.get('chapters'):
  588. metadata_filename = replace_extension(filename, 'meta')
  589. options.extend(self._get_chapter_opts(info['chapters'], metadata_filename))
  590. files_to_delete.append(metadata_filename)
  591. if self._add_metadata:
  592. options.extend(self._get_metadata_opts(info))
  593. if self._add_infojson:
  594. if info['ext'] in ('mkv', 'mka'):
  595. infojson_filename = info.get('infojson_filename')
  596. options.extend(self._get_infojson_opts(info, infojson_filename))
  597. if not infojson_filename:
  598. files_to_delete.append(info.get('infojson_filename'))
  599. elif self._add_infojson is True:
  600. self.to_screen('The info-json can only be attached to mkv/mka files')
  601. if not options:
  602. self.to_screen('There isn\'t any metadata to add')
  603. return [], info
  604. temp_filename = prepend_extension(filename, 'temp')
  605. self.to_screen(f'Adding metadata to "{filename}"')
  606. self.run_ffmpeg_multiple_files(
  607. (filename, metadata_filename), temp_filename,
  608. itertools.chain(self._options(info['ext']), *options))
  609. self._delete_downloaded_files(*files_to_delete)
  610. os.replace(temp_filename, filename)
  611. return [], info
  612. @staticmethod
  613. def _get_chapter_opts(chapters, metadata_filename):
  614. with open(metadata_filename, 'w', encoding='utf-8') as f:
  615. def ffmpeg_escape(text):
  616. return re.sub(r'([\\=;#\n])', r'\\\1', text)
  617. metadata_file_content = ';FFMETADATA1\n'
  618. for chapter in chapters:
  619. metadata_file_content += '[CHAPTER]\nTIMEBASE=1/1000\n'
  620. metadata_file_content += 'START=%d\n' % (chapter['start_time'] * 1000)
  621. metadata_file_content += 'END=%d\n' % (chapter['end_time'] * 1000)
  622. chapter_title = chapter.get('title')
  623. if chapter_title:
  624. metadata_file_content += f'title={ffmpeg_escape(chapter_title)}\n'
  625. f.write(metadata_file_content)
  626. yield ('-map_metadata', '1')
  627. def _get_metadata_opts(self, info):
  628. meta_prefix = 'meta'
  629. metadata = collections.defaultdict(dict)
  630. def add(meta_list, info_list=None):
  631. value = next((
  632. info[key] for key in [f'{meta_prefix}_', *variadic(info_list or meta_list)]
  633. if info.get(key) is not None), None)
  634. if value not in ('', None):
  635. value = ', '.join(map(str, variadic(value)))
  636. value = value.replace('\0', '') # nul character cannot be passed in command line
  637. metadata['common'].update({meta_f: value for meta_f in variadic(meta_list)})
  638. # Info on media metadata/metadata supported by ffmpeg:
  639. # https://wiki.multimedia.cx/index.php/FFmpeg_Metadata
  640. # https://kdenlive.org/en/project/adding-meta-data-to-mp4-video/
  641. # https://kodi.wiki/view/Video_file_tagging
  642. add('title', ('track', 'title'))
  643. add('date', 'upload_date')
  644. add(('description', 'synopsis'), 'description')
  645. add(('purl', 'comment'), 'webpage_url')
  646. add('track', 'track_number')
  647. add('artist', ('artist', 'artists', 'creator', 'creators', 'uploader', 'uploader_id'))
  648. add('composer', ('composer', 'composers'))
  649. add('genre', ('genre', 'genres'))
  650. add('album')
  651. add('album_artist', ('album_artist', 'album_artists'))
  652. add('disc', 'disc_number')
  653. add('show', 'series')
  654. add('season_number')
  655. add('episode_id', ('episode', 'episode_id'))
  656. add('episode_sort', 'episode_number')
  657. if 'embed-metadata' in self.get_param('compat_opts', []):
  658. add('comment', 'description')
  659. metadata['common'].pop('synopsis', None)
  660. meta_regex = rf'{re.escape(meta_prefix)}(?P<i>\d+)?_(?P<key>.+)'
  661. for key, value in info.items():
  662. mobj = re.fullmatch(meta_regex, key)
  663. if value is not None and mobj:
  664. metadata[mobj.group('i') or 'common'][mobj.group('key')] = value.replace('\0', '')
  665. # Write id3v1 metadata also since Windows Explorer can't handle id3v2 tags
  666. yield ('-write_id3v1', '1')
  667. for name, value in metadata['common'].items():
  668. yield ('-metadata', f'{name}={value}')
  669. stream_idx = 0
  670. for fmt in info.get('requested_formats') or [info]:
  671. stream_count = 2 if 'none' not in (fmt.get('vcodec'), fmt.get('acodec')) else 1
  672. lang = ISO639Utils.short2long(fmt.get('language') or '') or fmt.get('language')
  673. for i in range(stream_idx, stream_idx + stream_count):
  674. if lang:
  675. metadata[str(i)].setdefault('language', lang)
  676. for name, value in metadata[str(i)].items():
  677. yield (f'-metadata:s:{i}', f'{name}={value}')
  678. stream_idx += stream_count
  679. def _get_infojson_opts(self, info, infofn):
  680. if not infofn or not os.path.exists(infofn):
  681. if self._add_infojson is not True:
  682. return
  683. infofn = infofn or '%s.temp' % (
  684. self._downloader.prepare_filename(info, 'infojson')
  685. or replace_extension(self._downloader.prepare_filename(info), 'info.json', info['ext']))
  686. if not self._downloader._ensure_dir_exists(infofn):
  687. return
  688. self.write_debug(f'Writing info-json to: {infofn}')
  689. write_json_file(self._downloader.sanitize_info(info, self.get_param('clean_infojson', True)), infofn)
  690. info['infojson_filename'] = infofn
  691. old_stream, new_stream = self.get_stream_number(info['filepath'], ('tags', 'mimetype'), 'application/json')
  692. if old_stream is not None:
  693. yield ('-map', f'-0:{old_stream}')
  694. new_stream -= 1
  695. yield (
  696. '-attach', self._ffmpeg_filename_argument(infofn),
  697. f'-metadata:s:{new_stream}', 'mimetype=application/json',
  698. f'-metadata:s:{new_stream}', 'filename=info.json',
  699. )
  700. class FFmpegMergerPP(FFmpegPostProcessor):
  701. SUPPORTED_EXTS = MEDIA_EXTENSIONS.common_video
  702. @PostProcessor._restrict_to(images=False)
  703. def run(self, info):
  704. filename = info['filepath']
  705. temp_filename = prepend_extension(filename, 'temp')
  706. args = ['-c', 'copy']
  707. audio_streams = 0
  708. for (i, fmt) in enumerate(info['requested_formats']):
  709. if fmt.get('acodec') != 'none':
  710. args.extend(['-map', f'{i}:a:0'])
  711. aac_fixup = fmt['protocol'].startswith('m3u8') and self.get_audio_codec(fmt['filepath']) == 'aac'
  712. if aac_fixup:
  713. args.extend([f'-bsf:a:{audio_streams}', 'aac_adtstoasc'])
  714. audio_streams += 1
  715. if fmt.get('vcodec') != 'none':
  716. args.extend(['-map', f'{i}:v:0'])
  717. self.to_screen(f'Merging formats into "{filename}"')
  718. self.run_ffmpeg_multiple_files(info['__files_to_merge'], temp_filename, args)
  719. os.rename(encodeFilename(temp_filename), encodeFilename(filename))
  720. return info['__files_to_merge'], info
  721. def can_merge(self):
  722. # TODO: figure out merge-capable ffmpeg version
  723. if self.basename != 'avconv':
  724. return True
  725. required_version = '10-0'
  726. if is_outdated_version(
  727. self._versions[self.basename], required_version):
  728. warning = (f'Your copy of {self.basename} is outdated and unable to properly mux separate video and audio files, '
  729. 'yt-dlp will download single file media. '
  730. f'Update {self.basename} to version {required_version} or newer to fix this.')
  731. self.report_warning(warning)
  732. return False
  733. return True
  734. class FFmpegFixupPostProcessor(FFmpegPostProcessor):
  735. def _fixup(self, msg, filename, options):
  736. temp_filename = prepend_extension(filename, 'temp')
  737. self.to_screen(f'{msg} of "{filename}"')
  738. self.run_ffmpeg(filename, temp_filename, options)
  739. os.replace(temp_filename, filename)
  740. class FFmpegFixupStretchedPP(FFmpegFixupPostProcessor):
  741. @PostProcessor._restrict_to(images=False, audio=False)
  742. def run(self, info):
  743. stretched_ratio = info.get('stretched_ratio')
  744. if stretched_ratio not in (None, 1):
  745. self._fixup('Fixing aspect ratio', info['filepath'], [
  746. *self.stream_copy_opts(), '-aspect', f'{stretched_ratio:f}'])
  747. return [], info
  748. class FFmpegFixupM4aPP(FFmpegFixupPostProcessor):
  749. @PostProcessor._restrict_to(images=False, video=False)
  750. def run(self, info):
  751. if info.get('container') == 'm4a_dash':
  752. self._fixup('Correcting container', info['filepath'], [*self.stream_copy_opts(), '-f', 'mp4'])
  753. return [], info
  754. class FFmpegFixupM3u8PP(FFmpegFixupPostProcessor):
  755. def _needs_fixup(self, info):
  756. yield info['ext'] in ('mp4', 'm4a')
  757. yield info['protocol'].startswith('m3u8')
  758. try:
  759. metadata = self.get_metadata_object(info['filepath'])
  760. except PostProcessingError as e:
  761. self.report_warning(f'Unable to extract metadata: {e.msg}')
  762. yield True
  763. else:
  764. yield traverse_obj(metadata, ('format', 'format_name'), casesense=False) == 'mpegts'
  765. @PostProcessor._restrict_to(images=False)
  766. def run(self, info):
  767. if all(self._needs_fixup(info)):
  768. args = ['-f', 'mp4']
  769. if self.get_audio_codec(info['filepath']) == 'aac':
  770. args.extend(['-bsf:a', 'aac_adtstoasc'])
  771. self._fixup('Fixing MPEG-TS in MP4 container', info['filepath'], [
  772. *self.stream_copy_opts(), *args])
  773. return [], info
  774. class FFmpegFixupTimestampPP(FFmpegFixupPostProcessor):
  775. def __init__(self, downloader=None, trim=0.001):
  776. # "trim" should be used when the video contains unintended packets
  777. super().__init__(downloader)
  778. assert isinstance(trim, (int, float))
  779. self.trim = str(trim)
  780. @PostProcessor._restrict_to(images=False)
  781. def run(self, info):
  782. if not self._features.get('setts'):
  783. self.report_warning(
  784. 'A re-encode is needed to fix timestamps in older versions of ffmpeg. '
  785. 'Please install ffmpeg 4.4 or later to fixup without re-encoding')
  786. opts = ['-vf', 'setpts=PTS-STARTPTS']
  787. else:
  788. opts = ['-c', 'copy', '-bsf', 'setts=ts=TS-STARTPTS']
  789. self._fixup('Fixing frame timestamp', info['filepath'], [*opts, *self.stream_copy_opts(False), '-ss', self.trim])
  790. return [], info
  791. class FFmpegCopyStreamPP(FFmpegFixupPostProcessor):
  792. MESSAGE = 'Copying stream'
  793. @PostProcessor._restrict_to(images=False)
  794. def run(self, info):
  795. self._fixup(self.MESSAGE, info['filepath'], self.stream_copy_opts())
  796. return [], info
  797. class FFmpegFixupDurationPP(FFmpegCopyStreamPP):
  798. MESSAGE = 'Fixing video duration'
  799. class FFmpegFixupDuplicateMoovPP(FFmpegCopyStreamPP):
  800. MESSAGE = 'Fixing duplicate MOOV atoms'
  801. class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor):
  802. SUPPORTED_EXTS = MEDIA_EXTENSIONS.subtitles
  803. def __init__(self, downloader=None, format=None):
  804. super().__init__(downloader)
  805. self.format = format
  806. def run(self, info):
  807. subs = info.get('requested_subtitles')
  808. new_ext = self.format
  809. new_format = new_ext
  810. if new_format == 'vtt':
  811. new_format = 'webvtt'
  812. if subs is None:
  813. self.to_screen('There aren\'t any subtitles to convert')
  814. return [], info
  815. self.to_screen('Converting subtitles')
  816. sub_filenames = []
  817. for lang, sub in subs.items():
  818. if not os.path.exists(sub.get('filepath', '')):
  819. self.report_warning(f'Skipping embedding {lang} subtitle because the file is missing')
  820. continue
  821. ext = sub['ext']
  822. if ext == new_ext:
  823. self.to_screen(f'Subtitle file for {new_ext} is already in the requested format')
  824. continue
  825. elif ext == 'json':
  826. self.to_screen(
  827. 'You have requested to convert json subtitles into another format, '
  828. 'which is currently not possible')
  829. continue
  830. old_file = sub['filepath']
  831. sub_filenames.append(old_file)
  832. new_file = replace_extension(old_file, new_ext)
  833. if ext in ('dfxp', 'ttml', 'tt'):
  834. self.report_warning(
  835. 'You have requested to convert dfxp (TTML) subtitles into another format, '
  836. 'which results in style information loss')
  837. dfxp_file = old_file
  838. srt_file = replace_extension(old_file, 'srt')
  839. with open(dfxp_file, 'rb') as f:
  840. srt_data = dfxp2srt(f.read())
  841. with open(srt_file, 'w', encoding='utf-8') as f:
  842. f.write(srt_data)
  843. old_file = srt_file
  844. subs[lang] = {
  845. 'ext': 'srt',
  846. 'data': srt_data,
  847. 'filepath': srt_file,
  848. }
  849. if new_ext == 'srt':
  850. continue
  851. else:
  852. sub_filenames.append(srt_file)
  853. self.run_ffmpeg(old_file, new_file, ['-f', new_format])
  854. with open(new_file, encoding='utf-8') as f:
  855. subs[lang] = {
  856. 'ext': new_ext,
  857. 'data': f.read(),
  858. 'filepath': new_file,
  859. }
  860. info['__files_to_move'][new_file] = replace_extension(
  861. info['__files_to_move'][sub['filepath']], new_ext)
  862. return sub_filenames, info
  863. class FFmpegSplitChaptersPP(FFmpegPostProcessor):
  864. def __init__(self, downloader, force_keyframes=False):
  865. FFmpegPostProcessor.__init__(self, downloader)
  866. self._force_keyframes = force_keyframes
  867. def _prepare_filename(self, number, chapter, info):
  868. info = info.copy()
  869. info.update({
  870. 'section_number': number,
  871. 'section_title': chapter.get('title'),
  872. 'section_start': chapter.get('start_time'),
  873. 'section_end': chapter.get('end_time'),
  874. })
  875. return self._downloader.prepare_filename(info, 'chapter')
  876. def _ffmpeg_args_for_chapter(self, number, chapter, info):
  877. destination = self._prepare_filename(number, chapter, info)
  878. if not self._downloader._ensure_dir_exists(encodeFilename(destination)):
  879. return
  880. chapter['filepath'] = destination
  881. self.to_screen('Chapter %03d; Destination: %s' % (number, destination))
  882. return (
  883. destination,
  884. ['-ss', str(chapter['start_time']),
  885. '-t', str(chapter['end_time'] - chapter['start_time'])])
  886. @PostProcessor._restrict_to(images=False)
  887. def run(self, info):
  888. self._fixup_chapters(info)
  889. chapters = info.get('chapters') or []
  890. if not chapters:
  891. self.to_screen('Chapter information is unavailable')
  892. return [], info
  893. in_file = info['filepath']
  894. if self._force_keyframes and len(chapters) > 1:
  895. in_file = self.force_keyframes(in_file, (c['start_time'] for c in chapters))
  896. self.to_screen(f'Splitting video by chapters; {len(chapters)} chapters found')
  897. for idx, chapter in enumerate(chapters):
  898. destination, opts = self._ffmpeg_args_for_chapter(idx + 1, chapter, info)
  899. self.real_run_ffmpeg([(in_file, opts)], [(destination, self.stream_copy_opts())])
  900. if in_file != info['filepath']:
  901. self._delete_downloaded_files(in_file, msg=None)
  902. return [], info
  903. class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):
  904. SUPPORTED_EXTS = MEDIA_EXTENSIONS.thumbnails
  905. FORMAT_RE = create_mapping_re(SUPPORTED_EXTS)
  906. def __init__(self, downloader=None, format=None):
  907. super().__init__(downloader)
  908. self.mapping = format
  909. @classmethod
  910. def is_webp(cls, path):
  911. deprecation_warning(f'{cls.__module__}.{cls.__name__}.is_webp is deprecated')
  912. return imghdr.what(path) == 'webp'
  913. def fixup_webp(self, info, idx=-1):
  914. thumbnail_filename = info['thumbnails'][idx]['filepath']
  915. _, thumbnail_ext = os.path.splitext(thumbnail_filename)
  916. if thumbnail_ext:
  917. if thumbnail_ext.lower() != '.webp' and imghdr.what(thumbnail_filename) == 'webp':
  918. self.to_screen(f'Correcting thumbnail "{thumbnail_filename}" extension to webp')
  919. webp_filename = replace_extension(thumbnail_filename, 'webp')
  920. os.replace(thumbnail_filename, webp_filename)
  921. info['thumbnails'][idx]['filepath'] = webp_filename
  922. info['__files_to_move'][webp_filename] = replace_extension(
  923. info['__files_to_move'].pop(thumbnail_filename), 'webp')
  924. @staticmethod
  925. def _options(target_ext):
  926. yield from ('-update', '1')
  927. if target_ext == 'jpg':
  928. yield from ('-bsf:v', 'mjpeg2jpeg')
  929. def convert_thumbnail(self, thumbnail_filename, target_ext):
  930. thumbnail_conv_filename = replace_extension(thumbnail_filename, target_ext)
  931. self.to_screen(f'Converting thumbnail "{thumbnail_filename}" to {target_ext}')
  932. _, source_ext = os.path.splitext(thumbnail_filename)
  933. self.real_run_ffmpeg(
  934. [(thumbnail_filename, [] if source_ext == '.gif' else ['-f', 'image2', '-pattern_type', 'none'])],
  935. [(thumbnail_conv_filename, self._options(target_ext))])
  936. return thumbnail_conv_filename
  937. def run(self, info):
  938. files_to_delete = []
  939. has_thumbnail = False
  940. for idx, thumbnail_dict in enumerate(info.get('thumbnails') or []):
  941. original_thumbnail = thumbnail_dict.get('filepath')
  942. if not original_thumbnail:
  943. continue
  944. has_thumbnail = True
  945. self.fixup_webp(info, idx)
  946. original_thumbnail = thumbnail_dict['filepath'] # Path can change during fixup
  947. thumbnail_ext = os.path.splitext(original_thumbnail)[1][1:].lower()
  948. if thumbnail_ext == 'jpeg':
  949. thumbnail_ext = 'jpg'
  950. target_ext, _skip_msg = resolve_mapping(thumbnail_ext, self.mapping)
  951. if _skip_msg:
  952. self.to_screen(f'Not converting thumbnail "{original_thumbnail}"; {_skip_msg}')
  953. continue
  954. thumbnail_dict['filepath'] = self.convert_thumbnail(original_thumbnail, target_ext)
  955. files_to_delete.append(original_thumbnail)
  956. info['__files_to_move'][thumbnail_dict['filepath']] = replace_extension(
  957. info['__files_to_move'][original_thumbnail], target_ext)
  958. if not has_thumbnail:
  959. self.to_screen('There aren\'t any thumbnails to convert')
  960. return files_to_delete, info
  961. class FFmpegConcatPP(FFmpegPostProcessor):
  962. def __init__(self, downloader, only_multi_video=False):
  963. self._only_multi_video = only_multi_video
  964. super().__init__(downloader)
  965. def _get_codecs(self, file):
  966. codecs = traverse_obj(self.get_metadata_object(file), ('streams', ..., 'codec_name'))
  967. self.write_debug(f'Codecs = {", ".join(codecs)}')
  968. return tuple(codecs)
  969. def concat_files(self, in_files, out_file):
  970. if not self._downloader._ensure_dir_exists(out_file):
  971. return
  972. if len(in_files) == 1:
  973. if os.path.realpath(in_files[0]) != os.path.realpath(out_file):
  974. self.to_screen(f'Moving "{in_files[0]}" to "{out_file}"')
  975. os.replace(in_files[0], out_file)
  976. return []
  977. if len(set(map(self._get_codecs, in_files))) > 1:
  978. raise PostProcessingError(
  979. 'The files have different streams/codecs and cannot be concatenated. '
  980. 'Either select different formats or --recode-video them to a common format')
  981. self.to_screen(f'Concatenating {len(in_files)} files; Destination: {out_file}')
  982. super().concat_files(in_files, out_file)
  983. return in_files
  984. @PostProcessor._restrict_to(images=False, simulated=False)
  985. def run(self, info):
  986. entries = info.get('entries') or []
  987. if not any(entries) or (self._only_multi_video and info['_type'] != 'multi_video'):
  988. return [], info
  989. elif traverse_obj(entries, (..., lambda k, v: k == 'requested_downloads' and len(v) > 1)):
  990. raise PostProcessingError('Concatenation is not supported when downloading multiple separate formats')
  991. in_files = traverse_obj(entries, (..., 'requested_downloads', 0, 'filepath')) or []
  992. if len(in_files) < len(entries):
  993. raise PostProcessingError('Aborting concatenation because some downloads failed')
  994. exts = traverse_obj(entries, (..., 'requested_downloads', 0, 'ext'), (..., 'ext'))
  995. ie_copy = collections.ChainMap({'ext': exts[0] if len(set(exts)) == 1 else 'mkv'},
  996. info, self._downloader._playlist_infodict(info))
  997. out_file = self._downloader.prepare_filename(ie_copy, 'pl_video')
  998. files_to_delete = self.concat_files(in_files, out_file)
  999. info['requested_downloads'] = [{
  1000. 'filepath': out_file,
  1001. 'ext': ie_copy['ext'],
  1002. }]
  1003. return files_to_delete, info