How to get best quality audio using youtube-dl?

16

3

I am using youtube-dl on Linux to download videos from YouTube and extract the audio. This is my way of building a small music collection.

Anyways, I know that there is an option --audio-quality= with 0 being best and 9 being worst. Is this all I need to do to get the best quality audio?

Also, does anyone know if downloading from soundcloud could get higher quality?

user365967

Posted 2014-11-30T20:14:41.800

Reputation: 351

Answers

21

The --audio-quality does not affect the audio quality of the source. It's a post processing option and will re-encode the audio.

Since any kind of (lossy) re-encoding will actually deteriorate the quality of the stream—or at least make it unnecessarily larger in size—I would recommend not to post-process the audio at all.

To get the best audio quality possible you simply need to select a source format of high quality. Actually, youtube-dl will do that by default, but you can explicitly set it with --audio-format best. YouTube (and other providers) store different audio codecs with different bitrates, and youtube-dl will choose the best one from those.

If you have ffmpeg installed on your system, then youtube-dl can extract the audio automatically:

youtube-dl --audio-format best -x <url>

Otherwise, you will get a video file from which you have to extract the audio component.

slhck

Posted 2014-11-30T20:14:41.800

Reputation: 182 472

If ffmpeg doesn't reencode then why am I selecting the audio format? If I enter that line but with aac instead, I don't get the same file but with a different extension. I get a different file with slightly different size and bitrate. – Blrp – 2016-03-15T23:02:01.477

@Blrp YouTube stores the audio and video bitstreams in different formats (different codecs, different bitrates). youtube-dl allows you to choose that format. ffmpeg really only multiplexes video and audio streams together into a single file. – slhck – 2016-03-16T10:14:21.120

1Ok, but that doesn't really answer my question. I'm asking specifically about the video to audio conversion. If there's a command that takes the audio from an MP4 and saves it as an audio file without reencoding, there should only be one possible outcome, right? And yet, the ffmpeg command in your post yields a different result depending on which extension I select for the audio. So is there always one correct audio extension, depending on the video extension, for which I get the audio without reencoding? Or how else do I do it? – Blrp – 2016-03-16T10:46:20.830

1

I see now. First, I clarified my answer—it was a little more complex than needed. Also, maybe this answer is interesting to read. Even if the audio codec bitstream is not touched (i.e., copied), the extension determines how it is wrapped in a format. There is no 1:1 mapping here. Some formats require more space than others. You can use ffmpeg -i in.mp4 -c:a copy -vn out.m4a and end up with a different file than ffmpeg -i in.mp4 -c:a copy -vn out.mkv, although both contain the exact same audio data (and therefore have the same audio quality).

– slhck – 2016-03-16T11:07:17.057

Thanks! Another question though - are you certain that youtube-dl doesn't re-encode? All videos I've tried in 240p, 480p and 1080p get the same quality of 253kbps, which seems kinda weird. Also, other services give 125kbps with what they claim is the best available quality and no re-encoding. For more details see here.

– Blrp – 2016-03-16T12:04:38.827

1I'm certain it doesn't re-encode. It doesn't have any ability to do so. The reason you're getting the same bitrates is that YouTube has very limited variation in the audio bitrates / codecs they use, in contrast to the video representations. Maybe YouTube is a bit erratic when it assigns video and audio bitrates, and you may see a higher audio bitrate for a lower video quality—in that case, I think youtube-dl still selects the highest quality video format. Do you have a specific video example? – slhck – 2016-03-16T15:07:17.970

I'll take your word for it, but here are some videos of varying bitrate if you wanna mess around with it.

– Blrp – 2016-03-16T15:36:29.907

1Seems fine to me. For example in the second one, when you use -x as an option, it'll download the audio-only variant with the highest bitrate (256k, format code 141, check out with youtube-dl -F <url>). All other audio components of the other audiovisual representations have lower bitrates. – slhck – 2016-03-16T17:57:46.150

Hey, it's me again. I found a video that doesn't yield the best quality with your solution. youtube-dl --audio-format best -x https://www.youtube.com/watch?v=J9bjJEjK2dQ yields 125kbps m4a, and youtube-dl --format best -x https://www.youtube.com/watch?v=J9bjJEjK2dQ yields 192kbps m4a. I guess it's because the format with highest audio quality isn't audio-only, and --audio-format ignores formats with video? But if I replace --audio-format with --format, maybe the format counted as 'best' has really high video quality but lower audio (1/2)

– Blrp – 2016-04-20T17:40:42.790

(2/2) quality than some other format, so --format best also doesn't seem like a guarantee to have the best format. In fact, maybe it's possible that neither gives the best quality. What if one format has high video quality but medium audio quality, and one has high audio quality but low video quality, so the first is counted as best overall, and the second is not audio-only. – Blrp – 2016-04-20T17:44:23.937

I went to the project's support site and apparently there is no option for best sound quality: https://github.com/rg3/youtube-dl/issues/9302

– Blrp – 2016-04-26T04:27:05.310

@Blrp Hm, interesting. I didn't assume there would be a case where the best video/audio does not correspond to the best audio option. – slhck – 2016-04-26T17:45:34.977

5

Stay with Youtube's native music formats to avoid re-encoding:

youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <Video-URL>

resulting in an m4a file or

youtube-dl -f bestaudio --extract-audio --embed-thumbnail --add-metadata <Video-URL>

The latter can also resul in an ogg file if the highest audio quality format is OPUS rather than AAC.

You can list the available format with

youtube-dl -F <Video-URL>

Frank Breitling

Posted 2014-11-30T20:14:41.800

Reputation: 441

What's the difference between bestaudio[ext=m4a] and --extract-audio? Why would the latter likely result in an ogg but not the former? – Hashim – 2018-11-12T03:04:48.530

[ext=m4a] constrains your selection to the available m4a formats and without --extract-audio stores it in the same m4a container format. With --extract-audio audio data is extracted and stored in an ogg container. – Frank Breitling – 2018-11-12T20:19:25.063

Sorry this is late, but that doesn't really explain why the latter would result in an .ogg file specifically. Why .ogg when .ogg hasn't been specified? – Hashim – 2019-03-24T00:21:36.090

My understanding is that the best available audio quality is in OPUS format rather than AAC then it is saved in an ogg container. I have updated my answer accordingly. – Frank Breitling – 2019-03-24T08:04:53.550

2

Use the command:

youtube-dl -f bestaudio "url"

user598527

Posted 2014-11-30T20:14:41.800

Reputation: 2 399