55

I have just download youtube-dl so I can download video and audio files from youtube.

I want to download the best audio from the following video: https://www.youtube.com/watch?v=uWusmdmc0to

When I do a search for all formats with youtube-dl I get the following results:

format code  extension  resolution note
249          webm       audio only DASH audio   58k , opus @ 50k, 18.99MiB
250          webm       audio only DASH audio   75k , opus @ 70k, 25.20MiB
140          m4a        audio only DASH audio  131k , m4a_dash container, mp4a.40.2@128k, 52.40MiB
251          webm       audio only DASH audio  147k , opus @160k, 50.95MiB
171          webm       audio only DASH audio  149k , vorbis@128k, 52.64MiB
278          webm       256x144    144p  109k , webm container, vp9, 25fps, video only, 34.62MiB
160          mp4        256x144    144p  117k , avc1.4d400c, 25fps, video only, 37.86MiB
242          webm       426x240    240p  245k , vp9, 25fps, video only, 75.13MiB
133          mp4        426x240    240p  258k , avc1.4d4015, 25fps, video only, 81.39MiB
243          webm       640x360    360p  492k , vp9, 25fps, video only, 142.99MiB
134          mp4        640x360    360p  673k , avc1.4d401e, 25fps, video only, 215.29MiB
244          webm       854x480    480p  828k , vp9, 25fps, video only, 256.58MiB
135          mp4        854x480    480p 1516k , avc1.4d401e, 25fps, video only, 408.56MiB
247          webm       1280x720   720p 1882k , vp9, 25fps, video only, 526.18MiB
136          mp4        1280x720   720p 3012k , avc1.4d401f, 25fps, video only, 803.36MiB
248          webm       1920x1080  1080p 3622k , vp9, 25fps, video only, 938.81MiB
137          mp4        1920x1080  1080p 4724k , avc1.640028, 25fps, video only, 1.44GiB
271          webm       2560x1440  1440p 9253k , vp9, 25fps, video only, 2.86GiB
313          webm       3840x2160  2160p 18685k , vp9, 25fps, video only, 6.33GiB
17           3gp        176x144    small , mp4v.20.3, mp4a.40.2@ 24k
36           3gp        320x180    small , mp4v.20.3, mp4a.40.2
43           webm       640x360    medium , vp8.0, vorbis@128k
18           mp4        640x360    medium , avc1.42001E, mp4a.40.2@ 96k
22           mp4        1280x720   hd720 , avc1.64001F, mp4a.40.2@192k (best)

What is the best choice to get the best audio file? The first five are audio only. Do I need to pick one here? Or is the last one MP4 HD720 the best option and then convert it to MP3?

Thanks!

nnyby
  • 4,452
  • 9
  • 48
  • 102
WJ496582
  • 659
  • 1
  • 5
  • 9
  • The option `171` vorbis@128k is the best audio available for this video. It can be different for others. Rencoding will loose more data, no matter in what or at what birate! Why not make your very own test, compare outputs, in an headphone, in your car, etc. See what suits your needs – NVRM Apr 12 '18 at 20:00
  • 2
    The format code `251` "opus@160k" is the best audio quality in this situation. The Opus codec is a decade newer than Vorbis and is more efficient at representing audible sounds. – Nayuki Feb 26 '21 at 14:36

10 Answers10

77

If you want mp3, just tell youtube-dl that:

youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=uWusmdmc0to

will get you an audio version (-x, short for --extract-audio) in or converted to mp3 (that's the --audio-format option). youtube-dl will automatically pick the best quality and most appropriate format.

Note that the listed qualities are just guesses. In practice, opus is superior to anything else, but vorbis is picked for compatibility (refer to this related answer of mine for more details), so that will be picked.

While you can use -f to select a particular format, this is intended for people who want lower quality because of limited bandwidth or storage space, or for debugging. By default, youtube-dl already downloads the highest quality.

  • Thanks for your reply. I read on more sites that Opus is the best. But why does youtube-dl download the Vorbis file when I use: youtube-dl -f bestaudio https://www.youtube.com/watch?v=uWusmdmc0to ?? It's not the best one available. – WJ496582 Apr 13 '18 at 18:49
  • As laid out in the linked answer, that's because of compatibility concerns. If you can play/convert opus, you can pass in `-f 'bestaudio[format="opus"]/bestaudio/best'`. –  Apr 13 '18 at 21:27
  • @phihag this gave me the error `ValueError: Invalid filter specification u'format="opus"'` – Colin D Dec 22 '18 at 04:46
  • @ColinD Please [ask a separate question instead](https://stackoverflow.com/questions/ask), or maybe file a [youtube-dl bug](https://yt-dl.org/bug) . You can [notify me of the question via email or so](https://phihag.de/). Don't forget to include the *entire* output of youtube-dl when called with the `-v` option (plus all of the options you have). Also make sure you are using a recent version of youtube-dl, at least 2018.12.17. –  Dec 22 '18 at 08:59
  • I wonder why `--audio-format` don't work with `-f bestaudio`? – mrgloom Jul 23 '19 at 15:39
  • @mrgloom Works fine for me. Both options are unrelated. What error message do you get? Can you post your complete command, maybe in a new question or better at https://yt-dl.org/bug ? –  Jul 23 '19 at 16:03
  • "By default, youtube-dl already downloads the highest quality." Not at all. It downloads best quality format that requires no further manipulation. Best audio and best video streams are usually separate, require ffmpeg to merge, and are not chosen as "best quality". – polkovnikov.ph May 06 '21 at 06:55
  • 1
    It seems that this method downloads both the audio and video and then extracts the audio. Is it possible to save bandwidth by downloading only the audio? – Simon East May 31 '21 at 06:08
30

To perform this command you need ffmpeg installed (audio and video converter which youtube-dl uses for convertion)

To download audio from a single movie:

youtube-dl -f "bestaudio/best" -ciw -o "%(title)s.%(ext)s" -v --extract-audio --audio-quality 0 --audio-format mp3  https://www.youtube.com/watch?v=c29tZVZpZGVvUGxheWxpc3RQYXJ0

It downloads only audio (without video) and converts it to mp3.

Option --audio-quality 0 is very important there!
Without this option you lose some sound quality during mp3 compression.

--audio-quality 0 tells youtube-dl to save audio file in the best quality (when converting to mp3).
Without this option mp3 audio-quality is set by default to 5 in 0-9 scale where 0 is the best quality and 9 the worst quality. So by default quality is worse. Youtube streams for nonpremium users with variable bitrate up to 160kbps in opus format. Opus format is newer than mp3 and has better compression than mp3 preserving the same quality. So 160kbps opus = ~256kbps mp3.
When audio-quality is default (5 in 0-9 scale) mp3 bitrate is limited to 160kbps which means that some sound quality is lost during compression. When audio-quality is set to 0 mp3 goes up to 300kbps preserving original quality.

To download audio from all movies from a channel

Command is the same, but you should put link to the channel instead of link to a single video:

youtube-dl -f "bestaudio/best" -ciw -o "%(title)s.%(ext)s" -v --extract-audio --audio-quality 0 --audio-format mp3 https://www.youtube.com/c/someChannelName1232143/videos

To download audio from a playlist

You have to add --yes-playlist option.
You can put a link to a playlist (link with playlist word):

youtube-dl -f "bestaudio/best" -ciw -o "%(title)s.%(ext)s" -v --extract-audio --audio-format mp3 --audio-quality 0  --yes-playlist https://www.youtube.com/playlist?list=c29tZVZpZGVvVVJMUGFy

Or the link to one of the songs from playlist while playing playlist (link with list word):

youtube-dl -f "bestaudio/best" -ciw -o "%(title)s.%(ext)s" -v --extract-audio --audio-format mp3 --audio-quality 0  --yes-playlist "https://www.youtube.com/watch?v=c29tZVZpZGVvUGxheWxpc3RQYXJ0&list=c29tZVZpZGVvTGlzdFBhcnRzc29tZVZpZGVvTGlzdFBhcnRz&index=4"

Command options explanation:

-f "bestaudio/best" <- Choose the best audio format.
As there is only audio format listed only the audio is downloaded.

-c <- (--continue) Force resume of partially downloaded files.
By default, youtube-dl will resume downloads if possible.
As docs state maybe it is default, but I put it to make sure it is set.

-i <- (--ignore-errors) Continue on download errors,
for example to skip unavailable videos in a playlist.

-w <- (--no-overtwrites) Do not overwrite files
(If something was already downloaded
and is present in the directory then continue with the next video)

-o "%(title)s.%(ext)s" <- (--output) Output filename template,
in this case it gives you file named movieTitle.mp3
where movieTitle is the title of the video on youtube.

-v <- (--verbose) Print various debugging information

--extract-audio <- (-x) Convert video files to audio-only files
(requires ffmpeg or avconv and ffprobe or avprobe)

--audio-quality 0 <- Specify ffmpeg/avconv audio quality,
insert a value between 0 (better) and 9 (worse) for VBR or a specific
bitrate like 128K (default 5).
Youtube streams for nonpremium users with variable bitrate up to 160kbps in opus format.
Opus format is newer than mp3 and has better compression than mp3
preserving the same quality. So 160kbps opus = ~ 256kbps mp3.  
When audio-quality is default (5 in 0-9 scale) mp3 bitrate
is limited to 160kbps which means that some sound quality is lost during compression.
When audio-quality is set to 0 mp3 goes up to 300kbps preserving original quality.

--audio-format mp3 <-  Specify audio format: "best", "aac", "flac", "mp3", "m4a",
"opus", "vorbis", or "wav"; "best" by default; No effect without -x (--extract-audio).
In this case we choose mp3.
Alternatively you could choose for example opus which
is oryginally provided by youtube and is newer and better than mp3
or flac which is loseless codec.

All links that I provided there are fake. I just put some random words encoded by base64 in them. So you have to replace them by your own links to make it work.

Hint:

Youtube-dl gives you opportunity to use your own youtube account to download stuff.
If your account is a premium account you can get
higher 320kbps opus bitrate which is equivalent of ~512kbps mp3.
Using your own account might be possible by setting --username and --pasword
(See Authentication Options in --help)
luke
  • 2,375
  • 28
  • 39
  • Wouldn't `--audio-quality 0` re-encode the audio, thus degrading the quality? We want the original stream (even though it's lossy), not a lossy-to-lossy transcode, right? – gargoylebident Feb 24 '21 at 23:52
  • @stackexchange_account1111 To be honest I don't know how it works internally, but for sure if you compare file downloaded without setting --audio-quality at all with another file that was downloaded with `--audio-quality` parameter set to `0` (`--audio-quality 0`). The second one has not only higher bitrate, but you can also hear that it has much better quality of sound. Just download the same audio file two times, with and without this parameter set and compare it on your own using good quality audio equipment. – luke Feb 26 '21 at 12:38
  • @stackexchange_account1111 Please note that even when you don't set `--audio-quality` parameter on your own it is being set to default value which is `--audio-quality 5`. It specified in `youtube-dl` help: `"--audio-quality QUALITY Specify ffmpeg/avconv audio quality, insert a value between 0(better) and 9 (worse) for VBR or a specific bitrate like 128K(default 5)"`. – luke Feb 26 '21 at 12:42
  • @stackexchange_account1111 What might be lossy is converting from video format to audio only format. We are doing it there by using `--extract-audio ` option. As help states:`"-x, --extract-audio Convert video files to audio-only files (requires ffmpeg or avconvand ffprobe or avprobe)"`. For sure it might be a little bit lossy, but I don't know if it really is lossy, and if it is lossy I don't know how much, because I don't know how ffmpeg (or other converting tool that might be used) works internally. – luke Feb 26 '21 at 12:53
  • @stackexchange_account1111 So If you want to be sure nothing is lost you can download it as a movie, but it is less convenient to listen to the movie :) You can do it on youtube as well, so why even bother to download it. When we assume that we still want to convert It to audio there is one more thing that might be lossy: converting from `opus` format to `mp3` format, but you can always download it in a original format using `--audio-format opus` option instead of `--audio-format mp3` to reduce the risk that something is lost. – luke Feb 26 '21 at 12:53
  • @stackexchange_account1111 But in general if you will list using `youtube-dl` all possible formats in which selected movie could be downloaded you will see that audio in opus format is separate from video. If you will download video only you will get video without sound. So I assume that what `youtube-dl` does by default when downloading movie from youtube is that it downloads movie and audio separately and then combines it together. It looks like youtube sends video and audio separately probably to improve compression and thus save bandwith. – luke Feb 26 '21 at 13:09
  • @stackexchange_account1111 If written above is true it means that downloading audio using `--extract-audio --audio-quality 0 --audio-format opus` might be less lossy than downloading whole movie. It is because in the second case audio and video is combined together which might be lossy. But I am not sure, it is only my hypotesis. It might be a good idea to ask one of `youtube-dl` developers how it works or analyze the source code. – luke Feb 26 '21 at 13:11
  • @stackexchange_account1111 It is still possible that even if you use `--extract-audio --audio-format opus` it is done in that way: `(separate video, separate audio) -> combine together to movie (eg. mp4) -> convert audio from movie to opus`. If it works this way it disproves my hypotesis :) But in fact, I just don't know how It really works. In my opinion the most important thing is that setting `--audio-quality 0` gives you better quality than not setting `--audio-quality` at all no matter if you download a movie or an audio. And you can easily hear the difference. – luke Feb 26 '21 at 13:23
  • `--audio-quality 5` is only used when you specify an incompatible output format (e.g. you want the output to be .mp3, but there are only .opus streams available). If you don't do that, it will not use that flag. You're saying that a lossy-to-lossy transcode sounds better than an untampered lossy file. That makes no sense. It will be lossy no matter what (it's already lossy on the YouTube/Soundcloud/etc servers), you're only making things worse by re-encoding them all over again. It also has absolutely nothing to do with combining audio and video. In short: `--audio-quality 0` degrades quality. – gargoylebident Feb 26 '21 at 17:18
13

Download best audio:

youtube-dl -f bestaudio https://www.youtube.com/watch?v=3_y2jVPmPBw --output "out.%(ext)s"
mrgloom
  • 17,637
  • 28
  • 146
  • 263
7

This should give you the best sound quality:

youtube-dl --extract-audio "https://www.youtube.com/watch?v=uWusmdmc0to"

I'd recommend not to specify any audio format. If you specify an audio format then that probably is different from the original encoding and you will lose sound quality.

Wolfgang
  • 1,078
  • 2
  • 12
  • 17
4

I use a bat file in windows, which passes the youtube url through to a preset list of arguments to download the highest quality audio stream and save it as MP3.

batch file contains:

youtube-dl -f bestaudio -x --audio-format mp3 %*

save the batch file in the same dir as youtube-dl (I save it as youtube-mp3.bat) in the same directory have ffmpeg installed

Whenever I want to download audio (Usually for me a youtube DJ Mix) I just pass the (youtube) URL to the batch file. Saves having to remember what arguments to use each time.

youtube-mp3 https://www.youtube.com/?v=xxxxxx   
Suraj Rao
  • 28,850
  • 10
  • 94
  • 99
2

You can try to download audio with desired format by

youtube-dl -f m4a https://www.youtube.com/watch\?v\=ZtrEWtk9kbo 

command.

Sometimes ffmpeg is required.

FunkyKat
  • 3,183
  • 1
  • 21
  • 19
1

Check whether you have ffmpeg package installed from your side by below methods.

sudo apt install ffmpeg
sudo pip install ffmpeg

After above i got like below:

youtube-dl https://www.youtube.com/watch?v=6Qmnh5C4Pmo
[youtube] 6Qmnh5C4Pmo: Downloading webpage
[download] Destination: Pipenv Crash Course-6Qmnh5C4Pmo.f248.webm
[download] 100% of 33.19MiB in 00:26
[download] Destination: Pipenv Crash Course-6Qmnh5C4Pmo.f251.webm
[download] 100% of 15.15MiB in 00:09
[ffmpeg] Merging formats into &quot;Pipenv Crash Course-6Qmnh5C4Pmo.webm&quot;
Deleting original file Pipenv Crash Course-6Qmnh5C4Pmo.f248.webm (pass -k to keep)
Deleting original file Pipenv Crash Course-6Qmnh5C4Pmo.f251.webm (pass -k to keep)
ananthb
  • 11
  • 4
0

This works for me for sites providing audio in the M4A (MPEG-4 audio) format (e.g. YouTube, Facebook video):

youtube-dl -f "bestaudio[ext=m4a]/best[ext=mp4]" -x "https://..."

Since it doesn't specify --audio-format, youtube-dl won't do any reencoding, thus it doesn't lose audio quality. However, it may happen the video is available in a better quality (with a different ext=...).

pts
  • 71,941
  • 19
  • 102
  • 171
0

I personally currently use

youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0

which is a nice combination of all the other answers. I created the alias youtube-dl-music by typing

alias youtube-dl-music='youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0'

Hopefully this helps those in a need for a quick command to copy and paste.

Peter Csala
  • 10,331
  • 15
  • 20
  • 47
chrome
  • 1
0

Although you can get the best audio like this:

$ youtube-dl -f bestaudio https://www.youtube.com/watch?v=7E-cwdnsiow

You can also get the worst video with the best audio like this:

$ youtube-dl -f worstvideo+bestaudio https://www.youtube.com/watch?v=7E-cwdnsiow

Or you can separate the best audio and best video into two separate files like this:

$ youtube-dl -f bestvideo,bestaudio https://www.youtube.com/watch?v=7E-cwdnsiow

Better yet, you can seek out the best video within what you would consider reasonable and combine it with the audio:

$ youtube-dl -f 'bestvideo[height<=480]+bestaudio/best[height<=480]' https://www.youtube.com/watch?v=7E-cwdnsiow
Wolfpack'08
  • 3,642
  • 9
  • 43
  • 75