1
0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-12-23 16:36:48 +00:00

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): 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) PostProcessor.__init__(self, downloader)
if preferredcodec is None: if preferredcodec is None:
preferredcodec = 'best' preferredcodec = 'best'
self._preferredcodec = preferredcodec self._preferredcodec = preferredcodec
self._preferredquality = preferredquality
self._keepvideo = keepvideo
@staticmethod @staticmethod
def get_audio_codec(path): def get_audio_codec(path):
@ -3346,12 +3348,16 @@ class FFmpegExtractAudioPP(PostProcessor):
# MP3 otherwise. # MP3 otherwise.
acodec = 'libmp3lame' acodec = 'libmp3lame'
extension = 'mp3' extension = 'mp3'
more_opts = ['-ab', '128k'] more_opts = []
if self._preferredquality is not None:
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 = []
if self._preferredquality is not None:
more_opts += ['-ab', self._preferredquality]
if self._preferredcodec == 'aac': if self._preferredcodec == 'aac':
more_opts += ['-f', 'adts'] more_opts += ['-f', 'adts']
@ -3371,11 +3377,12 @@ class FFmpegExtractAudioPP(PostProcessor):
except: except:
self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file') self._downloader.to_stderr(u'WARNING: Cannot update utime of audio file')
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
@ -3573,6 +3580,10 @@ def parseOpts():
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='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) parser.add_option_group(general)
@ -3753,7 +3764,7 @@ def 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: