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.

511 lines
18 KiB

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