24

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?

3 Answers 3

32

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.

12
  • 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.
    – H.v.M.
    Commented Mar 15, 2016 at 23:02
  • 1
    Ok, 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?
    – H.v.M.
    Commented Mar 16, 2016 at 10:46
  • 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
    Commented Mar 16, 2016 at 11:07
  • 1
    I'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
    Commented Mar 16, 2016 at 15:07
  • 1
    Seems 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
    Commented Mar 16, 2016 at 17:57
9

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>
8
  • 1
    What's the difference between bestaudio[ext=m4a] and --extract-audio? Why would the latter likely result in an ogg but not the former? Commented Nov 12, 2018 at 3:04
  • [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. Commented Nov 12, 2018 at 20:19
  • 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? Commented Mar 24, 2019 at 0:21
  • 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. Commented Mar 24, 2019 at 8:04
  • That first syntax is no longer valid. It results in: "zsh: no matches found: bestaudio[ext=m4a]" Commented Apr 5, 2021 at 19:44
3

Use the command:

youtube-dl -f bestaudio "url"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .