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.

238 lines
10 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import absolute_import
  4. import os
  5. import subprocess
  6. import sys
  7. import time
  8. from .utils import *
  9. class PostProcessor(object):
  10. """Post Processor class.
  11. PostProcessor objects can be added to downloaders with their
  12. add_post_processor() method. When the downloader has finished a
  13. successful download, it will take its internal chain of PostProcessors
  14. and start calling the run() method on each one of them, first with
  15. an initial argument and then with the returned value of the previous
  16. PostProcessor.
  17. The chain will be stopped if one of them ever returns None or the end
  18. of the chain is reached.
  19. PostProcessor objects follow a "mutual registration" process similar
  20. to InfoExtractor objects.
  21. """
  22. _downloader = None
  23. def __init__(self, downloader=None):
  24. self._downloader = downloader
  25. def set_downloader(self, downloader):
  26. """Sets the downloader for this PP."""
  27. self._downloader = downloader
  28. def run(self, information):
  29. """Run the PostProcessor.
  30. The "information" argument is a dictionary like the ones
  31. composed by InfoExtractors. The only difference is that this
  32. one has an extra field called "filepath" that points to the
  33. downloaded file.
  34. This method returns a tuple, the first element of which describes
  35. whether the original file should be kept (i.e. not deleted - None for
  36. no preference), and the second of which is the updated information.
  37. In addition, this method may raise a PostProcessingError
  38. exception if post processing fails.
  39. """
  40. return None, information # by default, keep file and do nothing
  41. class FFmpegPostProcessorError(PostProcessingError):
  42. pass
  43. class AudioConversionError(PostProcessingError):
  44. pass
  45. class FFmpegPostProcessor(PostProcessor):
  46. def __init__(self,downloader=None):
  47. PostProcessor.__init__(self, downloader)
  48. self._exes = self.detect_executables()
  49. @staticmethod
  50. def detect_executables():
  51. def executable(exe):
  52. try:
  53. subprocess.Popen([exe, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  54. except OSError:
  55. return False
  56. return exe
  57. programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe']
  58. return dict((program, executable(program)) for program in programs)
  59. def run_ffmpeg(self, path, out_path, opts):
  60. if not self._exes['ffmpeg'] and not self._exes['avconv']:
  61. raise FFmpegPostProcessorError(u'ffmpeg or avconv not found. Please install one.')
  62. cmd = ([self._exes['avconv'] or self._exes['ffmpeg'], '-y', '-i', encodeFilename(path)]
  63. + opts +
  64. [encodeFilename(self._ffmpeg_filename_argument(out_path))])
  65. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  66. stdout,stderr = p.communicate()
  67. if p.returncode != 0:
  68. stderr = stderr.decode('utf-8', 'replace')
  69. msg = stderr.strip().split('\n')[-1]
  70. raise FFmpegPostProcessorError(msg)
  71. def _ffmpeg_filename_argument(self, fn):
  72. # ffmpeg broke --, see https://ffmpeg.org/trac/ffmpeg/ticket/2127 for details
  73. if fn.startswith(u'-'):
  74. return u'./' + fn
  75. return fn
  76. class FFmpegExtractAudioPP(FFmpegPostProcessor):
  77. def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, nopostoverwrites=False):
  78. FFmpegPostProcessor.__init__(self, downloader)
  79. if preferredcodec is None:
  80. preferredcodec = 'best'
  81. self._preferredcodec = preferredcodec
  82. self._preferredquality = preferredquality
  83. self._nopostoverwrites = nopostoverwrites
  84. def get_audio_codec(self, path):
  85. if not self._exes['ffprobe'] and not self._exes['avprobe']: return None
  86. try:
  87. cmd = [self._exes['avprobe'] or self._exes['ffprobe'], '-show_streams', encodeFilename(self._ffmpeg_filename_argument(path))]
  88. handle = subprocess.Popen(cmd, stderr=compat_subprocess_get_DEVNULL(), stdout=subprocess.PIPE)
  89. output = handle.communicate()[0]
  90. if handle.wait() != 0:
  91. return None
  92. except (IOError, OSError):
  93. return None
  94. audio_codec = None
  95. for line in output.decode('ascii', 'ignore').split('\n'):
  96. if line.startswith('codec_name='):
  97. audio_codec = line.split('=')[1].strip()
  98. elif line.strip() == 'codec_type=audio' and audio_codec is not None:
  99. return audio_codec
  100. return None
  101. def run_ffmpeg(self, path, out_path, codec, more_opts):
  102. if not self._exes['ffmpeg'] and not self._exes['avconv']:
  103. raise AudioConversionError('ffmpeg or avconv not found. Please install one.')
  104. if codec is None:
  105. acodec_opts = []
  106. else:
  107. acodec_opts = ['-acodec', codec]
  108. opts = ['-vn'] + acodec_opts + more_opts
  109. try:
  110. FFmpegPostProcessor.run_ffmpeg(self, path, out_path, opts)
  111. except FFmpegPostProcessorError as err:
  112. raise AudioConversionError(err.message)
  113. def run(self, information):
  114. path = information['filepath']
  115. filecodec = self.get_audio_codec(path)
  116. if filecodec is None:
  117. raise PostProcessingError(u'WARNING: unable to obtain file audio codec with ffprobe')
  118. more_opts = []
  119. if self._preferredcodec == 'best' or self._preferredcodec == filecodec or (self._preferredcodec == 'm4a' and filecodec == 'aac'):
  120. if filecodec == 'aac' and self._preferredcodec in ['m4a', 'best']:
  121. # Lossless, but in another container
  122. acodec = 'copy'
  123. extension = 'm4a'
  124. more_opts = [self._exes['avconv'] and '-bsf:a' or '-absf', 'aac_adtstoasc']
  125. elif filecodec in ['aac', 'mp3', 'vorbis', 'opus']:
  126. # Lossless if possible
  127. acodec = 'copy'
  128. extension = filecodec
  129. if filecodec == 'aac':
  130. more_opts = ['-f', 'adts']
  131. if filecodec == 'vorbis':
  132. extension = 'ogg'
  133. else:
  134. # MP3 otherwise.
  135. acodec = 'libmp3lame'
  136. extension = 'mp3'
  137. more_opts = []
  138. if self._preferredquality is not None:
  139. if int(self._preferredquality) < 10:
  140. more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
  141. else:
  142. more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
  143. else:
  144. # We convert the audio (lossy)
  145. acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'opus': 'opus', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec]
  146. extension = self._preferredcodec
  147. more_opts = []
  148. if self._preferredquality is not None:
  149. if int(self._preferredquality) < 10:
  150. more_opts += [self._exes['avconv'] and '-q:a' or '-aq', self._preferredquality]
  151. else:
  152. more_opts += [self._exes['avconv'] and '-b:a' or '-ab', self._preferredquality + 'k']
  153. if self._preferredcodec == 'aac':
  154. more_opts += ['-f', 'adts']
  155. if self._preferredcodec == 'm4a':
  156. more_opts += [self._exes['avconv'] and '-bsf:a' or '-absf', 'aac_adtstoasc']
  157. if self._preferredcodec == 'vorbis':
  158. extension = 'ogg'
  159. if self._preferredcodec == 'wav':
  160. extension = 'wav'
  161. more_opts += ['-f', 'wav']
  162. prefix, sep, ext = path.rpartition(u'.') # not os.path.splitext, since the latter does not work on unicode in all setups
  163. new_path = prefix + sep + extension
  164. # If we download foo.mp3 and convert it to... foo.mp3, then don't delete foo.mp3, silly.
  165. if new_path == path:
  166. self._nopostoverwrites = True
  167. try:
  168. if self._nopostoverwrites and os.path.exists(encodeFilename(new_path)):
  169. self._downloader.to_screen(u'[youtube] Post-process file %s exists, skipping' % new_path)
  170. else:
  171. self._downloader.to_screen(u'[' + (self._exes['avconv'] and 'avconv' or 'ffmpeg') + '] Destination: ' + new_path)
  172. self.run_ffmpeg(path, new_path, acodec, more_opts)
  173. except:
  174. etype,e,tb = sys.exc_info()
  175. if isinstance(e, AudioConversionError):
  176. msg = u'audio conversion failed: ' + e.message
  177. else:
  178. msg = u'error running ' + (self._exes['avconv'] and 'avconv' or 'ffmpeg')
  179. raise PostProcessingError(msg)
  180. # Try to update the date time for extracted audio file.
  181. if information.get('filetime') is not None:
  182. try:
  183. os.utime(encodeFilename(new_path), (time.time(), information['filetime']))
  184. except:
  185. self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file')
  186. information['filepath'] = new_path
  187. return self._nopostoverwrites,information
  188. class FFmpegVideoConvertor(FFmpegPostProcessor):
  189. def __init__(self, downloader=None,preferedformat=None):
  190. super(FFmpegVideoConvertor, self).__init__(downloader)
  191. self._preferedformat=preferedformat
  192. def run(self, information):
  193. path = information['filepath']
  194. prefix, sep, ext = path.rpartition(u'.')
  195. outpath = prefix + sep + self._preferedformat
  196. if information['ext'] == self._preferedformat:
  197. self._downloader.to_screen(u'[ffmpeg] Not converting video file %s - already is in target format %s' % (path, self._preferedformat))
  198. return True,information
  199. self._downloader.to_screen(u'['+'ffmpeg'+'] Converting video from %s to %s, Destination: ' % (information['ext'], self._preferedformat) +outpath)
  200. self.run_ffmpeg(path, outpath, [])
  201. information['filepath'] = outpath
  202. information['format'] = self._preferedformat
  203. information['ext'] = self._preferedformat
  204. return False,information