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