]>
Commit | Line | Data |
---|---|---|
3aa578ca PH |
1 | from __future__ import unicode_literals |
2 | ||
e9fade72 | 3 | import io |
496c1923 PH |
4 | import os |
5 | import subprocess | |
496c1923 PH |
6 | import time |
7 | ||
8 | ||
9 | from .common import AudioConversionError, PostProcessor | |
10 | ||
8c25f81b | 11 | from ..compat import ( |
496c1923 | 12 | compat_subprocess_get_DEVNULL, |
8c25f81b PH |
13 | ) |
14 | from ..utils import ( | |
f07b74fc | 15 | encodeArgument, |
496c1923 | 16 | encodeFilename, |
95807118 | 17 | get_exe_version, |
48844745 | 18 | is_outdated_version, |
496c1923 PH |
19 | PostProcessingError, |
20 | prepend_extension, | |
21 | shell_quote, | |
22 | subtitles_filename, | |
23 | ) | |
24 | ||
25 | ||
496c1923 PH |
26 | class FFmpegPostProcessorError(PostProcessingError): |
27 | pass | |
28 | ||
d799b47b | 29 | |
496c1923 | 30 | class FFmpegPostProcessor(PostProcessor): |
d47aeb22 | 31 | def __init__(self, downloader=None): |
496c1923 | 32 | PostProcessor.__init__(self, downloader) |
73fac4e9 | 33 | self._determine_executables() |
496c1923 | 34 | |
48844745 | 35 | def check_version(self): |
f740fae2 | 36 | if not self.available: |
3aa578ca | 37 | raise FFmpegPostProcessorError('ffmpeg or avconv not found. Please install one.') |
48844745 | 38 | |
65bf37ef | 39 | required_version = '10-0' if self.basename == 'avconv' else '1.0' |
48844745 | 40 | if is_outdated_version( |
73fac4e9 | 41 | self._versions[self.basename], required_version): |
3aa578ca | 42 | warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % ( |
73fac4e9 | 43 | self.basename, self.basename, required_version) |
6194bb14 PH |
44 | if self._downloader: |
45 | self._downloader.report_warning(warning) | |
48844745 | 46 | |
496c1923 | 47 | @staticmethod |
73fac4e9 PH |
48 | def get_versions(downloader=None): |
49 | return FFmpegPostProcessor(downloader)._versions | |
6271f1ca | 50 | |
73fac4e9 PH |
51 | def _determine_executables(self): |
52 | programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe'] | |
53 | prefer_ffmpeg = self._downloader.params.get('prefer_ffmpeg', False) | |
54 | ||
55 | self.basename = None | |
56 | self.probe_basename = None | |
57 | ||
58 | self._paths = None | |
59 | self._versions = None | |
60 | if self._downloader: | |
61 | location = self._downloader.params.get('ffmpeg_location') | |
62 | if location is not None: | |
63 | if not os.path.exists(location): | |
64 | self._downloader.report_warning( | |
65 | 'ffmpeg-location %s does not exist! ' | |
66 | 'Continuing without avconv/ffmpeg.' % (location)) | |
67 | self._versions = {} | |
68 | return | |
69 | elif not os.path.isdir(location): | |
70 | basename = os.path.splitext(os.path.basename(location))[0] | |
71 | if basename not in programs: | |
72 | self._downloader.report_warning( | |
73 | 'Cannot identify executable %s, its basename should be one of %s. ' | |
74 | 'Continuing without avconv/ffmpeg.' % | |
75 | (location, ', '.join(programs))) | |
76 | self._versions = {} | |
77 | return None | |
78 | location = os.path.dirname(os.path.abspath(location)) | |
79 | if basename in ('ffmpeg', 'ffprobe'): | |
80 | prefer_ffmpeg = True | |
81 | ||
82 | self._paths = dict( | |
83 | (p, os.path.join(location, p)) for p in programs) | |
84 | self._versions = dict( | |
85 | (p, get_exe_version(self._paths[p], args=['-version'])) | |
86 | for p in programs) | |
87 | if self._versions is None: | |
88 | self._versions = dict( | |
89 | (p, get_exe_version(p, args=['-version'])) for p in programs) | |
90 | self._paths = dict((p, p) for p in programs) | |
91 | ||
92 | if prefer_ffmpeg: | |
d28b5171 | 93 | prefs = ('ffmpeg', 'avconv') |
76b1bd67 | 94 | else: |
d28b5171 PH |
95 | prefs = ('avconv', 'ffmpeg') |
96 | for p in prefs: | |
97 | if self._versions[p]: | |
73fac4e9 PH |
98 | self.basename = p |
99 | break | |
76b1bd67 | 100 | |
73fac4e9 | 101 | if prefer_ffmpeg: |
50b51830 | 102 | prefs = ('ffprobe', 'avprobe') |
1a253e13 PH |
103 | else: |
104 | prefs = ('avprobe', 'ffprobe') | |
105 | for p in prefs: | |
106 | if self._versions[p]: | |
73fac4e9 PH |
107 | self.probe_basename = p |
108 | break | |
109 | ||
f740fae2 | 110 | @property |
73fac4e9 PH |
111 | def available(self): |
112 | return self.basename is not None | |
1a253e13 | 113 | |
73fac4e9 PH |
114 | @property |
115 | def executable(self): | |
116 | return self._paths[self.basename] | |
117 | ||
3da4b313 JMF |
118 | @property |
119 | def probe_available(self): | |
120 | return self.probe_basename is not None | |
121 | ||
73fac4e9 PH |
122 | @property |
123 | def probe_executable(self): | |
124 | return self._paths[self.probe_basename] | |
76b1bd67 | 125 | |
496c1923 | 126 | def run_ffmpeg_multiple_files(self, input_paths, out_path, opts): |
48844745 | 127 | self.check_version() |
496c1923 | 128 | |
52afb2ac PH |
129 | oldest_mtime = min( |
130 | os.stat(encodeFilename(path)).st_mtime for path in input_paths) | |
43bc8890 | 131 | |
496c1923 PH |
132 | files_cmd = [] |
133 | for path in input_paths: | |
b0e87c31 | 134 | files_cmd.extend([encodeArgument('-i'), encodeFilename(path, True)]) |
73fac4e9 | 135 | cmd = ([encodeFilename(self.executable, True), encodeArgument('-y')] + |
43bc8890 PH |
136 | files_cmd + |
137 | [encodeArgument(o) for o in opts] + | |
496c1923 PH |
138 | [encodeFilename(self._ffmpeg_filename_argument(out_path), True)]) |
139 | ||
140 | if self._downloader.params.get('verbose', False): | |
3aa578ca | 141 | self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd)) |
cffcbc02 | 142 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) |
62fec3b2 | 143 | stdout, stderr = p.communicate() |
496c1923 PH |
144 | if p.returncode != 0: |
145 | stderr = stderr.decode('utf-8', 'replace') | |
146 | msg = stderr.strip().split('\n')[-1] | |
147 | raise FFmpegPostProcessorError(msg) | |
dd29eb7f | 148 | self.try_utime(out_path, oldest_mtime, oldest_mtime) |
cc55d088 | 149 | |
496c1923 PH |
150 | def run_ffmpeg(self, path, out_path, opts): |
151 | self.run_ffmpeg_multiple_files([path], out_path, opts) | |
152 | ||
153 | def _ffmpeg_filename_argument(self, fn): | |
154 | # ffmpeg broke --, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details | |
3aa578ca PH |
155 | if fn.startswith('-'): |
156 | return './' + fn | |
496c1923 PH |
157 | return fn |
158 | ||
159 | ||
160 | class FFmpegExtractAudioPP(FFmpegPostProcessor): | |
161 | def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False): | |
162 | FFmpegPostProcessor.__init__(self, downloader) | |
163 | if preferredcodec is None: | |
164 | preferredcodec = 'best' | |
165 | self._preferredcodec = preferredcodec | |
166 | self._preferredquality = preferredquality | |
167 | self._nopostoverwrites = nopostoverwrites | |
168 | ||
169 | def get_audio_codec(self, path): | |
1a253e13 | 170 | |
3da4b313 | 171 | if not self.probe_available: |
3aa578ca | 172 | raise PostProcessingError('ffprobe or avprobe not found. Please install one.') |
496c1923 PH |
173 | try: |
174 | cmd = [ | |
73fac4e9 | 175 | encodeFilename(self.probe_executable, True), |
b0e87c31 | 176 | encodeArgument('-show_streams'), |
496c1923 | 177 | encodeFilename(self._ffmpeg_filename_argument(path), True)] |
73fac4e9 | 178 | if self._downloader.params.get('verbose', False): |
5bfd430f | 179 | self._downloader.to_screen('[debug] %s command line: %s' % (self.basename, shell_quote(cmd))) |
cffcbc02 | 180 | handle = subprocess.Popen(cmd, stderr=compat_subprocess_get_DEVNULL(), stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
496c1923 PH |
181 | output = handle.communicate()[0] |
182 | if handle.wait() != 0: | |
183 | return None | |
184 | except (IOError, OSError): | |
185 | return None | |
186 | audio_codec = None | |
187 | for line in output.decode('ascii', 'ignore').split('\n'): | |
188 | if line.startswith('codec_name='): | |
189 | audio_codec = line.split('=')[1].strip() | |
190 | elif line.strip() == 'codec_type=audio' and audio_codec is not None: | |
191 | return audio_codec | |
192 | return None | |
193 | ||
194 | def run_ffmpeg(self, path, out_path, codec, more_opts): | |
496c1923 PH |
195 | if codec is None: |
196 | acodec_opts = [] | |
197 | else: | |
198 | acodec_opts = ['-acodec', codec] | |
199 | opts = ['-vn'] + acodec_opts + more_opts | |
200 | try: | |
201 | FFmpegPostProcessor.run_ffmpeg(self, path, out_path, opts) | |
202 | except FFmpegPostProcessorError as err: | |
203 | raise AudioConversionError(err.msg) | |
204 | ||
205 | def run(self, information): | |
206 | path = information['filepath'] | |
207 | ||
208 | filecodec = self.get_audio_codec(path) | |
209 | if filecodec is None: | |
3aa578ca | 210 | raise PostProcessingError('WARNING: unable to obtain file audio codec with ffprobe') |
496c1923 PH |
211 | |
212 | more_opts = [] | |
213 | if self._preferredcodec == 'best' or self._preferredcodec == filecodec or (self._preferredcodec == 'm4a' and filecodec == 'aac'): | |
214 | if filecodec == 'aac' and self._preferredcodec in ['m4a', 'best']: | |
215 | # Lossless, but in another container | |
216 | acodec = 'copy' | |
217 | extension = 'm4a' | |
467d3c9a | 218 | more_opts = ['-bsf:a', 'aac_adtstoasc'] |
496c1923 PH |
219 | elif filecodec in ['aac', 'mp3', 'vorbis', 'opus']: |
220 | # Lossless if possible | |
221 | acodec = 'copy' | |
222 | extension = filecodec | |
223 | if filecodec == 'aac': | |
224 | more_opts = ['-f', 'adts'] | |
225 | if filecodec == 'vorbis': | |
226 | extension = 'ogg' | |
227 | else: | |
228 | # MP3 otherwise. | |
229 | acodec = 'libmp3lame' | |
230 | extension = 'mp3' | |
231 | more_opts = [] | |
232 | if self._preferredquality is not None: | |
233 | if int(self._preferredquality) < 10: | |
467d3c9a | 234 | more_opts += ['-q:a', self._preferredquality] |
496c1923 | 235 | else: |
467d3c9a | 236 | more_opts += ['-b:a', self._preferredquality + 'k'] |
496c1923 PH |
237 | else: |
238 | # We convert the audio (lossy) | |
239 | acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'opus': 'opus', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec] | |
240 | extension = self._preferredcodec | |
241 | more_opts = [] | |
242 | if self._preferredquality is not None: | |
243 | # The opus codec doesn't support the -aq option | |
244 | if int(self._preferredquality) < 10 and extension != 'opus': | |
467d3c9a | 245 | more_opts += ['-q:a', self._preferredquality] |
496c1923 | 246 | else: |
467d3c9a | 247 | more_opts += ['-b:a', self._preferredquality + 'k'] |
496c1923 PH |
248 | if self._preferredcodec == 'aac': |
249 | more_opts += ['-f', 'adts'] | |
250 | if self._preferredcodec == 'm4a': | |
467d3c9a | 251 | more_opts += ['-bsf:a', 'aac_adtstoasc'] |
496c1923 PH |
252 | if self._preferredcodec == 'vorbis': |
253 | extension = 'ogg' | |
254 | if self._preferredcodec == 'wav': | |
255 | extension = 'wav' | |
256 | more_opts += ['-f', 'wav'] | |
257 | ||
3aa578ca | 258 | prefix, sep, ext = path.rpartition('.') # not os.path.splitext, since the latter does not work on unicode in all setups |
496c1923 PH |
259 | new_path = prefix + sep + extension |
260 | ||
261 | # If we download foo.mp3 and convert it to... foo.mp3, then don't delete foo.mp3, silly. | |
ce81b141 JMF |
262 | if (new_path == path or |
263 | (self._nopostoverwrites and os.path.exists(encodeFilename(new_path)))): | |
264 | self._downloader.to_screen('[youtube] Post-process file %s exists, skipping' % new_path) | |
592e97e8 | 265 | return [], information |
496c1923 PH |
266 | |
267 | try: | |
ce81b141 JMF |
268 | self._downloader.to_screen('[' + self.basename + '] Destination: ' + new_path) |
269 | self.run_ffmpeg(path, new_path, acodec, more_opts) | |
70a1165b JMF |
270 | except AudioConversionError as e: |
271 | raise PostProcessingError( | |
272 | 'audio conversion failed: ' + e.msg) | |
273 | except Exception: | |
274 | raise PostProcessingError('error running ' + self.basename) | |
496c1923 PH |
275 | |
276 | # Try to update the date time for extracted audio file. | |
277 | if information.get('filetime') is not None: | |
dd29eb7f S |
278 | self.try_utime( |
279 | new_path, time.time(), information['filetime'], | |
280 | errnote='Cannot update utime of audio file') | |
496c1923 PH |
281 | |
282 | information['filepath'] = new_path | |
ddbed364 | 283 | information['ext'] = extension |
284 | ||
592e97e8 | 285 | return [path], information |
496c1923 PH |
286 | |
287 | ||
4f026faf | 288 | class FFmpegVideoConvertorPP(FFmpegPostProcessor): |
5f6a1245 | 289 | def __init__(self, downloader=None, preferedformat=None): |
4f026faf | 290 | super(FFmpegVideoConvertorPP, self).__init__(downloader) |
5f6a1245 | 291 | self._preferedformat = preferedformat |
496c1923 PH |
292 | |
293 | def run(self, information): | |
294 | path = information['filepath'] | |
3aa578ca | 295 | prefix, sep, ext = path.rpartition('.') |
496c1923 PH |
296 | outpath = prefix + sep + self._preferedformat |
297 | if information['ext'] == self._preferedformat: | |
3aa578ca | 298 | self._downloader.to_screen('[ffmpeg] Not converting video file %s - already is in target format %s' % (path, self._preferedformat)) |
592e97e8 | 299 | return [], information |
3aa578ca | 300 | self._downloader.to_screen('[' + 'ffmpeg' + '] Converting video from %s to %s, Destination: ' % (information['ext'], self._preferedformat) + outpath) |
496c1923 PH |
301 | self.run_ffmpeg(path, outpath, []) |
302 | information['filepath'] = outpath | |
303 | information['format'] = self._preferedformat | |
304 | information['ext'] = self._preferedformat | |
592e97e8 | 305 | return [path], information |
496c1923 PH |
306 | |
307 | ||
308 | class FFmpegEmbedSubtitlePP(FFmpegPostProcessor): | |
309 | # See http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt | |
310 | _lang_map = { | |
311 | 'aa': 'aar', | |
312 | 'ab': 'abk', | |
313 | 'ae': 'ave', | |
314 | 'af': 'afr', | |
315 | 'ak': 'aka', | |
316 | 'am': 'amh', | |
317 | 'an': 'arg', | |
318 | 'ar': 'ara', | |
319 | 'as': 'asm', | |
320 | 'av': 'ava', | |
321 | 'ay': 'aym', | |
322 | 'az': 'aze', | |
323 | 'ba': 'bak', | |
324 | 'be': 'bel', | |
325 | 'bg': 'bul', | |
326 | 'bh': 'bih', | |
327 | 'bi': 'bis', | |
328 | 'bm': 'bam', | |
329 | 'bn': 'ben', | |
330 | 'bo': 'bod', | |
331 | 'br': 'bre', | |
332 | 'bs': 'bos', | |
333 | 'ca': 'cat', | |
334 | 'ce': 'che', | |
335 | 'ch': 'cha', | |
336 | 'co': 'cos', | |
337 | 'cr': 'cre', | |
338 | 'cs': 'ces', | |
339 | 'cu': 'chu', | |
340 | 'cv': 'chv', | |
341 | 'cy': 'cym', | |
342 | 'da': 'dan', | |
343 | 'de': 'deu', | |
344 | 'dv': 'div', | |
345 | 'dz': 'dzo', | |
346 | 'ee': 'ewe', | |
347 | 'el': 'ell', | |
348 | 'en': 'eng', | |
349 | 'eo': 'epo', | |
350 | 'es': 'spa', | |
351 | 'et': 'est', | |
352 | 'eu': 'eus', | |
353 | 'fa': 'fas', | |
354 | 'ff': 'ful', | |
355 | 'fi': 'fin', | |
356 | 'fj': 'fij', | |
357 | 'fo': 'fao', | |
358 | 'fr': 'fra', | |
359 | 'fy': 'fry', | |
360 | 'ga': 'gle', | |
361 | 'gd': 'gla', | |
362 | 'gl': 'glg', | |
363 | 'gn': 'grn', | |
364 | 'gu': 'guj', | |
365 | 'gv': 'glv', | |
366 | 'ha': 'hau', | |
367 | 'he': 'heb', | |
368 | 'hi': 'hin', | |
369 | 'ho': 'hmo', | |
370 | 'hr': 'hrv', | |
371 | 'ht': 'hat', | |
372 | 'hu': 'hun', | |
373 | 'hy': 'hye', | |
374 | 'hz': 'her', | |
375 | 'ia': 'ina', | |
376 | 'id': 'ind', | |
377 | 'ie': 'ile', | |
378 | 'ig': 'ibo', | |
379 | 'ii': 'iii', | |
380 | 'ik': 'ipk', | |
381 | 'io': 'ido', | |
382 | 'is': 'isl', | |
383 | 'it': 'ita', | |
384 | 'iu': 'iku', | |
385 | 'ja': 'jpn', | |
386 | 'jv': 'jav', | |
387 | 'ka': 'kat', | |
388 | 'kg': 'kon', | |
389 | 'ki': 'kik', | |
390 | 'kj': 'kua', | |
391 | 'kk': 'kaz', | |
392 | 'kl': 'kal', | |
393 | 'km': 'khm', | |
394 | 'kn': 'kan', | |
395 | 'ko': 'kor', | |
396 | 'kr': 'kau', | |
397 | 'ks': 'kas', | |
398 | 'ku': 'kur', | |
399 | 'kv': 'kom', | |
400 | 'kw': 'cor', | |
401 | 'ky': 'kir', | |
402 | 'la': 'lat', | |
403 | 'lb': 'ltz', | |
404 | 'lg': 'lug', | |
405 | 'li': 'lim', | |
406 | 'ln': 'lin', | |
407 | 'lo': 'lao', | |
408 | 'lt': 'lit', | |
409 | 'lu': 'lub', | |
410 | 'lv': 'lav', | |
411 | 'mg': 'mlg', | |
412 | 'mh': 'mah', | |
413 | 'mi': 'mri', | |
414 | 'mk': 'mkd', | |
415 | 'ml': 'mal', | |
416 | 'mn': 'mon', | |
417 | 'mr': 'mar', | |
418 | 'ms': 'msa', | |
419 | 'mt': 'mlt', | |
420 | 'my': 'mya', | |
421 | 'na': 'nau', | |
422 | 'nb': 'nob', | |
423 | 'nd': 'nde', | |
424 | 'ne': 'nep', | |
425 | 'ng': 'ndo', | |
426 | 'nl': 'nld', | |
427 | 'nn': 'nno', | |
428 | 'no': 'nor', | |
429 | 'nr': 'nbl', | |
430 | 'nv': 'nav', | |
431 | 'ny': 'nya', | |
432 | 'oc': 'oci', | |
433 | 'oj': 'oji', | |
434 | 'om': 'orm', | |
435 | 'or': 'ori', | |
436 | 'os': 'oss', | |
437 | 'pa': 'pan', | |
438 | 'pi': 'pli', | |
439 | 'pl': 'pol', | |
440 | 'ps': 'pus', | |
441 | 'pt': 'por', | |
442 | 'qu': 'que', | |
443 | 'rm': 'roh', | |
444 | 'rn': 'run', | |
445 | 'ro': 'ron', | |
446 | 'ru': 'rus', | |
447 | 'rw': 'kin', | |
448 | 'sa': 'san', | |
449 | 'sc': 'srd', | |
450 | 'sd': 'snd', | |
451 | 'se': 'sme', | |
452 | 'sg': 'sag', | |
453 | 'si': 'sin', | |
454 | 'sk': 'slk', | |
455 | 'sl': 'slv', | |
456 | 'sm': 'smo', | |
457 | 'sn': 'sna', | |
458 | 'so': 'som', | |
459 | 'sq': 'sqi', | |
460 | 'sr': 'srp', | |
461 | 'ss': 'ssw', | |
462 | 'st': 'sot', | |
463 | 'su': 'sun', | |
464 | 'sv': 'swe', | |
465 | 'sw': 'swa', | |
466 | 'ta': 'tam', | |
467 | 'te': 'tel', | |
468 | 'tg': 'tgk', | |
469 | 'th': 'tha', | |
470 | 'ti': 'tir', | |
471 | 'tk': 'tuk', | |
472 | 'tl': 'tgl', | |
473 | 'tn': 'tsn', | |
474 | 'to': 'ton', | |
475 | 'tr': 'tur', | |
476 | 'ts': 'tso', | |
477 | 'tt': 'tat', | |
478 | 'tw': 'twi', | |
479 | 'ty': 'tah', | |
480 | 'ug': 'uig', | |
481 | 'uk': 'ukr', | |
482 | 'ur': 'urd', | |
483 | 'uz': 'uzb', | |
484 | 've': 'ven', | |
485 | 'vi': 'vie', | |
486 | 'vo': 'vol', | |
487 | 'wa': 'wln', | |
488 | 'wo': 'wol', | |
489 | 'xh': 'xho', | |
490 | 'yi': 'yid', | |
491 | 'yo': 'yor', | |
492 | 'za': 'zha', | |
493 | 'zh': 'zho', | |
494 | 'zu': 'zul', | |
495 | } | |
496 | ||
496c1923 PH |
497 | @classmethod |
498 | def _conver_lang_code(cls, code): | |
499 | """Convert language code from ISO 639-1 to ISO 639-2/T""" | |
500 | return cls._lang_map.get(code[:2]) | |
501 | ||
502 | def run(self, information): | |
3aa578ca PH |
503 | if information['ext'] != 'mp4': |
504 | self._downloader.to_screen('[ffmpeg] Subtitles can only be embedded in mp4 files') | |
592e97e8 | 505 | return [], information |
c84dd8a9 JMF |
506 | subtitles = information.get('requested_subtitles') |
507 | if not subtitles: | |
3aa578ca | 508 | self._downloader.to_screen('[ffmpeg] There aren\'t any subtitles to embed') |
592e97e8 | 509 | return [], information |
496c1923 | 510 | |
c84dd8a9 | 511 | sub_langs = list(subtitles.keys()) |
496c1923 | 512 | filename = information['filepath'] |
14523ed9 JMF |
513 | sub_filenames = [subtitles_filename(filename, lang, sub_info['ext']) for lang, sub_info in subtitles.items()] |
514 | input_files = [filename] + sub_filenames | |
496c1923 | 515 | |
e205db3b JMF |
516 | opts = [ |
517 | '-map', '0', | |
518 | '-c', 'copy', | |
519 | # Don't copy the existing subtitles, we may be running the | |
520 | # postprocessor a second time | |
521 | '-map', '-0:s', | |
2875cf01 | 522 | '-c:s', 'mov_text', |
e205db3b | 523 | ] |
496c1923 | 524 | for (i, lang) in enumerate(sub_langs): |
2875cf01 | 525 | opts.extend(['-map', '%d:0' % (i + 1)]) |
496c1923 PH |
526 | lang_code = self._conver_lang_code(lang) |
527 | if lang_code is not None: | |
528 | opts.extend(['-metadata:s:s:%d' % i, 'language=%s' % lang_code]) | |
496c1923 | 529 | |
2875cf01 | 530 | temp_filename = prepend_extension(filename, 'temp') |
3aa578ca | 531 | self._downloader.to_screen('[ffmpeg] Embedding subtitles in \'%s\'' % filename) |
496c1923 PH |
532 | self.run_ffmpeg_multiple_files(input_files, temp_filename, opts) |
533 | os.remove(encodeFilename(filename)) | |
534 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
535 | ||
14523ed9 | 536 | return sub_filenames, information |
496c1923 PH |
537 | |
538 | ||
539 | class FFmpegMetadataPP(FFmpegPostProcessor): | |
540 | def run(self, info): | |
541 | metadata = {} | |
88cf6fb3 | 542 | if info.get('title') is not None: |
496c1923 PH |
543 | metadata['title'] = info['title'] |
544 | if info.get('upload_date') is not None: | |
545 | metadata['date'] = info['upload_date'] | |
e7db87f7 | 546 | if info.get('artist') is not None: |
547 | metadata['artist'] = info['artist'] | |
548 | elif info.get('uploader') is not None: | |
496c1923 PH |
549 | metadata['artist'] = info['uploader'] |
550 | elif info.get('uploader_id') is not None: | |
551 | metadata['artist'] = info['uploader_id'] | |
bd3cbe07 DP |
552 | if info.get('description') is not None: |
553 | metadata['description'] = info['description'] | |
2cf0ecac | 554 | metadata['comment'] = info['description'] |
bd3cbe07 | 555 | if info.get('webpage_url') is not None: |
2cf0ecac | 556 | metadata['purl'] = info['webpage_url'] |
e7db87f7 | 557 | if info.get('album') is not None: |
558 | metadata['album'] = info['album'] | |
496c1923 PH |
559 | |
560 | if not metadata: | |
3aa578ca | 561 | self._downloader.to_screen('[ffmpeg] There isn\'t any metadata to add') |
592e97e8 | 562 | return [], info |
496c1923 PH |
563 | |
564 | filename = info['filepath'] | |
565 | temp_filename = prepend_extension(filename, 'temp') | |
566 | ||
3aa578ca | 567 | if info['ext'] == 'm4a': |
39c68260 | 568 | options = ['-vn', '-acodec', 'copy'] |
569 | else: | |
570 | options = ['-c', 'copy'] | |
571 | ||
496c1923 PH |
572 | for (name, value) in metadata.items(): |
573 | options.extend(['-metadata', '%s=%s' % (name, value)]) | |
574 | ||
3aa578ca | 575 | self._downloader.to_screen('[ffmpeg] Adding metadata to \'%s\'' % filename) |
496c1923 PH |
576 | self.run_ffmpeg(filename, temp_filename, options) |
577 | os.remove(encodeFilename(filename)) | |
578 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
592e97e8 | 579 | return [], info |
496c1923 PH |
580 | |
581 | ||
582 | class FFmpegMergerPP(FFmpegPostProcessor): | |
583 | def run(self, info): | |
584 | filename = info['filepath'] | |
5b5fbc08 | 585 | temp_filename = prepend_extension(filename, 'temp') |
bc3e582f | 586 | args = ['-c', 'copy', '-map', '0:v:0', '-map', '1:a:0'] |
3aa578ca | 587 | self._downloader.to_screen('[ffmpeg] Merging formats into "%s"' % filename) |
5b5fbc08 JMF |
588 | self.run_ffmpeg_multiple_files(info['__files_to_merge'], temp_filename, args) |
589 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
d47aeb22 | 590 | return info['__files_to_merge'], info |
496c1923 | 591 | |
0c14e2fb | 592 | |
6271f1ca PH |
593 | class FFmpegFixupStretchedPP(FFmpegPostProcessor): |
594 | def run(self, info): | |
595 | stretched_ratio = info.get('stretched_ratio') | |
596 | if stretched_ratio is None or stretched_ratio == 1: | |
592e97e8 | 597 | return [], info |
6271f1ca PH |
598 | |
599 | filename = info['filepath'] | |
600 | temp_filename = prepend_extension(filename, 'temp') | |
601 | ||
602 | options = ['-c', 'copy', '-aspect', '%f' % stretched_ratio] | |
603 | self._downloader.to_screen('[ffmpeg] Fixing aspect ratio in "%s"' % filename) | |
604 | self.run_ffmpeg(filename, temp_filename, options) | |
605 | ||
606 | os.remove(encodeFilename(filename)) | |
607 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
608 | ||
592e97e8 | 609 | return [], info |
62cd676c PH |
610 | |
611 | ||
612 | class FFmpegFixupM4aPP(FFmpegPostProcessor): | |
613 | def run(self, info): | |
614 | if info.get('container') != 'm4a_dash': | |
592e97e8 | 615 | return [], info |
62cd676c PH |
616 | |
617 | filename = info['filepath'] | |
618 | temp_filename = prepend_extension(filename, 'temp') | |
619 | ||
620 | options = ['-c', 'copy', '-f', 'mp4'] | |
621 | self._downloader.to_screen('[ffmpeg] Correcting container in "%s"' % filename) | |
622 | self.run_ffmpeg(filename, temp_filename, options) | |
623 | ||
624 | os.remove(encodeFilename(filename)) | |
625 | os.rename(encodeFilename(temp_filename), encodeFilename(filename)) | |
626 | ||
592e97e8 | 627 | return [], info |
e9fade72 JMF |
628 | |
629 | ||
630 | class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor): | |
631 | def __init__(self, downloader=None, format=None): | |
632 | super(FFmpegSubtitlesConvertorPP, self).__init__(downloader) | |
633 | self.format = format | |
634 | ||
635 | def run(self, info): | |
636 | subs = info.get('requested_subtitles') | |
637 | filename = info['filepath'] | |
638 | new_ext = self.format | |
639 | new_format = new_ext | |
640 | if new_format == 'vtt': | |
641 | new_format = 'webvtt' | |
642 | if subs is None: | |
643 | self._downloader.to_screen('[ffmpeg] There aren\'t any subtitles to convert') | |
592e97e8 | 644 | return [], info |
e9fade72 JMF |
645 | self._downloader.to_screen('[ffmpeg] Converting subtitles') |
646 | for lang, sub in subs.items(): | |
647 | ext = sub['ext'] | |
648 | if ext == new_ext: | |
649 | self._downloader.to_screen( | |
650 | '[ffmpeg] Subtitle file for %s is already in the requested' | |
651 | 'format' % new_ext) | |
652 | continue | |
653 | new_file = subtitles_filename(filename, lang, new_ext) | |
654 | self.run_ffmpeg( | |
655 | subtitles_filename(filename, lang, ext), | |
656 | new_file, ['-f', new_format]) | |
657 | ||
658 | with io.open(new_file, 'rt', encoding='utf-8') as f: | |
659 | subs[lang] = { | |
660 | 'ext': ext, | |
661 | 'data': f.read(), | |
662 | } | |
663 | ||
592e97e8 | 664 | return [], info |