Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download the best quality audio file with youtube-dl [closed]

People also ask

Does YouTube-dl download the highest quality?

You can download 1080p using youtube-dl , but you need to do a little extra work. Usually it will only download 720p as its max even if you can see 1080p on youtube.com. and it will download the 1080p video and auto-merge it with the highest-quality audio.

Does YouTube use Vorbis?

YouTube also recommends uploading at 48KHz, but only their Opus encodes are in 48KHz - YouTube uses 44.1KHz for both AAC and Vorbis, so both of those encodes are likely to have been downsampled as well. In the end, Opus should still be higher quality.

What is M4A audio quality?

M4A files are encoded with the lossy Advanced Audio Coding (AAC) codec, which is able to provide the same bitrates as MP3s, yet achieve tighter compression. This results in smaller file sizes, all while delivering higher audio quality.


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.


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)

Download best audio:

youtube-dl -f bestaudio https://www.youtube.com/watch?v=3_y2jVPmPBw --output "out.%(ext)s"

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.


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   

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.


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)