23

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?

kenorb
  • 24,736
  • 27
  • 129
  • 199
user365967
  • 451
  • 1
  • 5
  • 11

3 Answers3

31

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
  • 223,558
  • 70
  • 607
  • 592
  • 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. Mar 15 '16 at 23:02
  • @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 Mar 16 '16 at 10:14
  • 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. Mar 16 '16 at 10:46
  • 1
    I see now. First, I clarified my answer—it was a little more complex than needed. Also, maybe [this answer](http://superuser.com/questions/300897/) 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 Mar 16 '16 at 11:07
  • 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](https://www.reddit.com/r/software/comments/4aewl0/download_youtube_audio_without_quality_loss/). – H.v.M. Mar 16 '16 at 12:04
  • 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 Mar 16 '16 at 15:07
  • I'll take your word for it, but [here](https://www.youtube.com/watch?v=afJm3rbZjgw) [are](https://www.youtube.com/watch?v=sOOebk_dKFo) [some](https://www.youtube.com/watch?v=MIdLS0OC2zk) videos of varying bitrate if you wanna mess around with it. – H.v.M. Mar 16 '16 at 15:36
  • 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 `). All other audio components of the other audiovisual representations have lower bitrates. – slhck Mar 16 '16 at 17:57
  • Hey, it's me again. I found a [video](https://www.youtube.com/watch?v=J9bjJEjK2dQ) 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) – H.v.M. Apr 20 '16 at 17:40
  • (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. – H.v.M. Apr 20 '16 at 17:44
  • 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 – H.v.M. Apr 26 '16 at 04:27
  • @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 Apr 26 '16 at 17:45
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>
Frank Breitling
  • 635
  • 1
  • 11
  • 25
  • 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? – Hashim Aziz Nov 12 '18 at 03: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. – Frank Breitling Nov 12 '18 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? – Hashim Aziz Mar 24 '19 at 00: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. – Frank Breitling Mar 24 '19 at 08:04
  • That first syntax is no longer valid. It results in: "zsh: no matches found: bestaudio[ext=m4a]" – Oscar Goldman Apr 05 '21 at 19:44
  • @OscarGoldman: you need to quote the square brackets in zsh, otherwise zsh thinks its a glob and tries to expand it to the matching files. youtube-dl -f 'bestaudio[ext=m4a]' ... should work fine in zsh as well. – agostonbarna Jan 07 '23 at 11:22
  • @agostonbarna Thanks for the info. I'll give that a try. – Oscar Goldman Jan 08 '23 at 19:38
  • could not make this '-f ba[ext=m4a]' work with yt-dlp – Ewoks May 13 '23 at 13:28
2

Use the command:

youtube-dl -f bestaudio "url"
user198350
  • 3,769
  • 15
  • 50
  • 89