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