You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

539 lines
18 KiB

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