Merge remote-tracking branch 'rmanola/master' (Closes: #124)

Add option to specify mp3 quality and prevent the video from being deleted
This commit is contained in:
Philipp Hagemeister 2011-09-15 12:43:27 +02:00
commit c99dcbd2d6

View File

@ -3293,11 +3293,13 @@ class PostProcessor(object):
class FFmpegExtractAudioPP(PostProcessor):
def __init__(self, downloader=None, preferredcodec=None):
def __init__(self, downloader=None, preferredcodec=None, preferredquality=None, keepvideo=False):
PostProcessor.__init__(self, downloader)
if preferredcodec is None:
preferredcodec = 'best'
self._preferredcodec = preferredcodec
self._preferredquality = preferredquality
self._keepvideo = keepvideo
@staticmethod
def get_audio_codec(path):
@ -3346,12 +3348,16 @@ class FFmpegExtractAudioPP(PostProcessor):
# MP3 otherwise.
acodec = 'libmp3lame'
extension = 'mp3'
more_opts = ['-ab', '128k']
more_opts = []
if self._preferredquality is not None:
more_opts += ['-ab', self._preferredquality]
else:
# We convert the audio (lossy)
acodec = {'mp3': 'libmp3lame', 'aac': 'aac'}[self._preferredcodec]
extension = self._preferredcodec
more_opts = ['-ab', '128k']
more_opts = []
if self._preferredquality is not None:
more_opts += ['-ab', self._preferredquality]
if self._preferredcodec == 'aac':
more_opts += ['-f', 'adts']
@ -3371,6 +3377,7 @@ class FFmpegExtractAudioPP(PostProcessor):
except:
self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file')
if not self._keepvideo:
try:
os.remove(path)
except (IOError, OSError):
@ -3573,6 +3580,10 @@ def parseOpts():
help='convert video files to audio-only files (requires ffmpeg and ffprobe)')
postproc.add_option('--audio-format', metavar='FORMAT', dest='audioformat', default='best',
help='"best", "aac" or "mp3"; best by default')
postproc.add_option('--audio-quality', metavar='QUALITY', dest='audioquality', default='128K',
help='ffmpeg audio bitrate specification, 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(general)
@ -3753,7 +3764,7 @@ def main():
# PostProcessors
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
if opts.update_self: