I'm using the youtube_dl
library to download some videos as mp3 files. Here are the options I'm using:
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': music_directory + '/YouTube/' + '%(title)s.mp3',
'noplaylist': True,
'continue_dl': True,
'quiet': True,
'progress_hooks': [self.progress_hook],
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192', }]
}
I would have thought that the postprocessors
should convert the downloaded file to mp3. However, the downloaded file isn't correctly encoded as an mp3. See:
When I run ffmpeg -i [file]
, it outputs this:
Note the matroska,webm
and Audio: opus
.
Whereas with normal mp3 files it specifies the audio is mp3:
Note the mp3
and Audio: mp3
.
I'm having trouble playing the file in programs such as iTunes, and I believe this encoding error is the cause of the problem. How can I ensure youtube_dl
encodes the mp3 file correctly?
Update:
After doing some more testing, it seems that youtube_dl
is only downloading in mpeg and not converting to mp3 from that.
When I use these options, it downloads as an mpeg file. Testing with ffmpeg
, the postprocessors don't make any difference on the audio encoding.
ydl_opts = {
'format': 'bestaudio',
'outtmpl': music_directory + '/YouTube/' + '%(title)s.%(ext)s',
'noplaylist': True,
'continue_dl': True,
'quiet': True,
'progress_hooks': [self.progress_hook],
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192', }]
}
So... I guess the problem is that I can't get youtube_dl
to download anything other than an mpeg file.