Added option to allow different audio encoding qualities and to allow specify whether erase or not the video when it's need to extract the audio.

This commit is contained in:
rmanola 2011-06-06 17:46:37 -07:00
parent 4b0d9eed45
commit 18b7f87409

31
youtube-dl Executable file → Normal file
View file

@ -2611,11 +2611,17 @@ class PostProcessor(object):
class FFmpegExtractAudioPP(PostProcessor): class FFmpegExtractAudioPP(PostProcessor):
def __init__(self, downloader=None, preferredcodec=None): def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, keepvideo=None):
PostProcessor.__init__(self, downloader) PostProcessor.__init__(self, downloader)
if preferredcodec is None: if preferredcodec is None:
preferredcodec = 'best' preferredcodec = 'best'
if preferredquality is None:
preferredquality = '128K'
if keepvideo is None:
keepvideo = False;
self._preferredcodec = preferredcodec self._preferredcodec = preferredcodec
self._preferredquality = preferredquality
self._keepvideo = keepvideo
@staticmethod @staticmethod
def get_audio_codec(path): def get_audio_codec(path):
@ -2653,6 +2659,8 @@ class FFmpegExtractAudioPP(PostProcessor):
return None return None
more_opts = [] more_opts = []
if (self._preferredquality != '128K') and (self._preferredquality != '160K') and (self._preferredquality != '192K'):
self._preferredquality = '128K'
if self._preferredcodec == 'best' or self._preferredcodec == filecodec: if self._preferredcodec == 'best' or self._preferredcodec == filecodec:
if filecodec == 'aac' or filecodec == 'mp3': if filecodec == 'aac' or filecodec == 'mp3':
# Lossless if possible # Lossless if possible
@ -2664,12 +2672,12 @@ class FFmpegExtractAudioPP(PostProcessor):
# MP3 otherwise. # MP3 otherwise.
acodec = 'libmp3lame' acodec = 'libmp3lame'
extension = 'mp3' extension = 'mp3'
more_opts = ['-ab', '128k'] more_opts = ['-ab', self._preferredquality]
else: else:
# We convert the audio (lossy) # We convert the audio (lossy)
acodec = {'mp3': 'libmp3lame', 'aac': 'aac'}[self._preferredcodec] acodec = {'mp3': 'libmp3lame', 'aac': 'aac'}[self._preferredcodec]
extension = self._preferredcodec extension = self._preferredcodec
more_opts = ['-ab', '128k'] more_opts = ['-ab', self._preferredquality]
if self._preferredcodec == 'aac': if self._preferredcodec == 'aac':
more_opts += ['-f', 'adts'] more_opts += ['-f', 'adts']
@ -2682,11 +2690,12 @@ class FFmpegExtractAudioPP(PostProcessor):
self._downloader.to_stderr(u'WARNING: error running ffmpeg') self._downloader.to_stderr(u'WARNING: error running ffmpeg')
return None return None
try: if not self._keepvideo:
os.remove(path) try:
except (IOError, OSError): os.remove(path)
self._downloader.to_stderr(u'WARNING: Unable to remove downloaded video file') except (IOError, OSError):
return None self._downloader.to_stderr(u'WARNING: Unable to remove downloaded video file')
return None
information['filepath'] = new_path information['filepath'] = new_path
return information return information
@ -2820,6 +2829,10 @@ if __name__ == '__main__':
help='convert video files to audio-only files (requires ffmpeg and ffprobe)') help='convert video files to audio-only files (requires ffmpeg and ffprobe)')
postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best', postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best',
help='"best", "aac" or "mp3"; best by default') help='"best", "aac" or "mp3"; best by default')
postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='128K',
help='128K, 160K or 192K; 128K by default')
postproc.add_option('-k', '--keep-video', action='store_true', dest='keepvideo', default=False,
help='keeps the video file on disk after the post-processing; the video is erased by default')
parser.add_option_group(postproc) parser.add_option_group(postproc)
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
@ -2970,7 +2983,7 @@ if __name__ == '__main__':
# PostProcessors # PostProcessors
if opts.extractaudio: if opts.extractaudio:
fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat)) fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat,preferredquality=opts.audioquality,keepvideo=opts.keepvideo))
# Update version # Update version
if opts.update_self: if opts.update_self: