From 8544f1a915ff689453105eacfd44c6f6e9b8ca75 Mon Sep 17 00:00:00 2001 From: Istvan Nagy Date: Sun, 15 Mar 2015 18:13:58 +0100 Subject: [PATCH 1/3] libfdkaac transcode support added. --- youtube_dl/__init__.py | 2 +- youtube_dl/postprocessor/ffmpeg.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 852b2fc3d..41969e980 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -162,7 +162,7 @@ def _real_main(argv=None): if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart: raise ValueError('Playlist end must be greater than playlist start') if opts.extractaudio: - if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']: + if opts.audioformat not in ['best', 'aac', 'libfdkaac', 'libfdkaac-he', 'libfdkaac-hev2', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']: parser.error('invalid audio format specified') if opts.audioquality: opts.audioquality = opts.audioquality.strip('k').strip('K') diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index b6f51cfd5..e264ecb11 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -237,7 +237,7 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): more_opts += ['-b:a', self._preferredquality + 'k'] else: # We convert the audio (lossy) - acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'opus': 'opus', 'vorbis': 'libvorbis', 'wav': None}[self._preferredcodec] + acodec = {'mp3': 'libmp3lame', 'aac': 'aac', 'm4a': 'aac', 'opus': 'opus', 'vorbis': 'libvorbis', 'wav': None, 'libfdkaac': None, 'libfdkaac-he': None, 'libfdkaac-hev2': None}[self._preferredcodec] extension = self._preferredcodec more_opts = [] if self._preferredquality is not None: @@ -255,6 +255,21 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): if self._preferredcodec == 'wav': extension = 'wav' more_opts += ['-f', 'wav'] + if self._preferredcodec == 'libfdkaac' or self._preferredcodec == 'libfdkaac-he' or self._preferredcodec == 'libfdkaac-hev2': + more_opts = [] + acodec = 'libfdk_aac' + prefix, sep, ext = path.rpartition('.') + if (ext == 'm4a'): + extension = 'libfdkaac.m4a' + if self._preferredquality is not None: + if int(self._preferredquality) < 6: + more_opts += ['-vbr', self._preferredquality] + else: + more_opts += ['-b:a', self._preferredquality + 'k'] + if self._preferredcodec == 'libfdkaac-he': + more_opts += ['-profile:a', 'aac_he'] + elif self._preferredcodec == 'libfdkaac-hev2': + more_opts += ['-profile:a', 'aac_he_v2'] prefix, sep, ext = path.rpartition('.') # not os.path.splitext, since the latter does not work on unicode in all setups new_path = prefix + sep + extension From a66cb19c693822124c40665a6e8cad4d787dbc5f Mon Sep 17 00:00:00 2001 From: Istvan Nagy Date: Mon, 16 Mar 2015 22:02:07 +0100 Subject: [PATCH 2/3] Added fdkaac sample rate table for CBR and VBR modes --- youtube_dl/postprocessor/ffmpeg.py | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index e264ecb11..82fa59349 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -23,6 +23,50 @@ from ..utils import ( subtitles_filename, ) +FFMPEG_CBR_SAMPLERATE_TABLE = { + "libfdkaac": [ + [16, 23, 11025], + [24, 31, 16000], + [32, 39, 24000], + [40, 95, 32000], + [96, 320, 44100], + [320, 576, 48000] + ], + "libfdkaac-he": [ + [16, 28, 32000], + [28, 63, 44100], + [64, 128, 48000] + ], + "libfdkaac-hev2": [ + [8, 11, 24000], + [12, 17, 32000], + [18, 39, 44100], + [40, 56, 48000] + ] +} + +FFMPEG_VBR_SAMPLERATE_TABLE = { + "libfdkaac": [ + [1, 32000], #40kbps + [2, 32000], #64kbps + [3, 44100], #96kbps + [4, 44100], #128kbps + [5, 44100], #192kbps + ], + "libfdkaac-he": [ + [1, 44100], + [2, 48000], + [3, 48000], + [4, 48000], + [5, 48000] + ], + "libfdkaac-hev2": [ + [1, 48000], + [2, 48000], + [3, 48000], + [4, 48000] + ] +} class FFmpegPostProcessorError(PostProcessingError): pass @@ -203,6 +247,24 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): except FFmpegPostProcessorError as err: raise AudioConversionError(err.msg) + def get_fdkaac_cbr_samplerate(self, codec, bitrate): + table = FFMPEG_CBR_SAMPLERATE_TABLE[codec]; + int_bitrate = int(bitrate) + for configuration in table: + min_rate, max_rate, sample_rate = configuration[0], configuration[1], configuration[2] + if int_bitrate>=min_rate and int_bitrate<=max_rate: + return sample_rate + raise PostProcessingError('WARNING: Can''t find fdkaac samplerate configuration for bitrate.') + + def get_fdkaac_vbr_samplerate(self, codec, vbr_value): + table = FFMPEG_VBR_SAMPLERATE_TABLE[codec]; + int_vbr_level = int(vbr_value) + for configuration in table: + vbr_level, sample_rate = configuration[0], configuration[1] + if int_vbr_level == vbr_level: + return sample_rate + raise PostProcessingError('WARNING: Can''t find fdkaac samplerate configuration for VBR level.') + def run(self, information): path = information['filepath'] @@ -259,13 +321,18 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): more_opts = [] acodec = 'libfdk_aac' prefix, sep, ext = path.rpartition('.') + if (ext == 'm4a'): extension = 'libfdkaac.m4a' if self._preferredquality is not None: if int(self._preferredquality) < 6: more_opts += ['-vbr', self._preferredquality] + sample_rate = self.get_fdkaac_vbr_samplerate(self._preferredcodec, self._preferredquality) + more_opts += ['-ar', str(sample_rate)] else: more_opts += ['-b:a', self._preferredquality + 'k'] + sample_rate = self.get_fdkaac_cbr_samplerate(self._preferredcodec, self._preferredquality) + more_opts += ['-ar', str(sample_rate)] if self._preferredcodec == 'libfdkaac-he': more_opts += ['-profile:a', 'aac_he'] elif self._preferredcodec == 'libfdkaac-hev2': From 49384d9775721306b9d47c8d6f388387a181dc6d Mon Sep 17 00:00:00 2001 From: Istvan Nagy Date: Mon, 16 Mar 2015 22:06:05 +0100 Subject: [PATCH 3/3] Naming fix. --- youtube_dl/postprocessor/ffmpeg.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 82fa59349..532bcd565 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -23,7 +23,7 @@ from ..utils import ( subtitles_filename, ) -FFMPEG_CBR_SAMPLERATE_TABLE = { +LIBFDKAAC_CBR_SAMPLERATE_TABLE = { "libfdkaac": [ [16, 23, 11025], [24, 31, 16000], @@ -45,7 +45,7 @@ FFMPEG_CBR_SAMPLERATE_TABLE = { ] } -FFMPEG_VBR_SAMPLERATE_TABLE = { +LIBFDKAAC_VBR_SAMPLERATE_TABLE = { "libfdkaac": [ [1, 32000], #40kbps [2, 32000], #64kbps @@ -248,7 +248,7 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): raise AudioConversionError(err.msg) def get_fdkaac_cbr_samplerate(self, codec, bitrate): - table = FFMPEG_CBR_SAMPLERATE_TABLE[codec]; + table = LIBFDKAAC_CBR_SAMPLERATE_TABLE[codec]; int_bitrate = int(bitrate) for configuration in table: min_rate, max_rate, sample_rate = configuration[0], configuration[1], configuration[2] @@ -257,7 +257,7 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor): raise PostProcessingError('WARNING: Can''t find fdkaac samplerate configuration for bitrate.') def get_fdkaac_vbr_samplerate(self, codec, vbr_value): - table = FFMPEG_VBR_SAMPLERATE_TABLE[codec]; + table = LIBFDKAAC_VBR_SAMPLERATE_TABLE[codec]; int_vbr_level = int(vbr_value) for configuration in table: vbr_level, sample_rate = configuration[0], configuration[1]