youtube-dl is a versatile command line tool for downloading videos from YouTube and many other websites. I use it for making back up of my own YouTube videos.
By default, you use youtube-dl for downloading videos. How about extracting only the audio with youtube-dl? Thatβs very simple actually. Let me show you the steps.
Install youtube-dl first
Please make sure that you have installed youtube-dl on your Linux distribution first.
Since 2021, the usual snap and default package manager installations of youtube-dl give a broken utility. So, you need to install the Nightly version of youtube-dl to make it work.
The Nightly release contains recent changes and fixes so that you can run it without any error. I will describe the easiest way to set it up. First go to the Nightly release GitHub page.
Here, you can get a python file, named youtube-dl
. Just download it. Once downloaded, copy that file to /usr/local/bin/
. For this, open a terminal where you downloaded the file and then run:
sudo cp youtube-dl /usr/local/bin/
Now, give the file execution permission using the chmod
command.
sudo chmod a+rx /usr/local/bin/youtube-dl
You cannot just run youtube-dl
now, because of issue with python
naming. It uses python
while in Ubuntu-based systems, it is python3
.
So, you can create an alias for youtube-dl in your .bashrc
.
alias youtube-dl='python3 /usr/local/bin/youtube-dl'
Thatβs it. You have set up youtube-dl in your system.
In order to download audio using youtube-dl, you should have FFMPEG installed on your system.
Download only audio with youtube-dl
If you only want to download audio from a YouTube video, you can use the -x
option with youtube-dl. This extract-audio option converts the video files to audio-only files.
youtube-dl -x video_URL
The file is saved in the same directory from where you ran the youtube-dl command.
Hereβs an example where I downloaded the voice-over of our Install Google Chrome on Ubuntu 24.04 video.
Did you notice the audio format? It is in .m4a format. You may specify the audio format to something of your choice.
Say you want to extract the audio in MP3 format. You can use it like this:
youtube-dl -x --audio-format mp3 video_URL
Hereβs the same example I showed previously. You can see that it uses ffmpeg to convert the m4a file into MP3.
-k
option to preserve the old file along with the new one.Download entire YouTube playlist in MP3 format
Yes, you can totally do that. The main thing is to get the URL of the playlist here. It is typically in the following format:
https://www.youtube.com/playlist?list=XXXXXXXXXXXXXXXXXXX
To get the URL of a playlist, click on its name when the playlist is being displayed in the right sidebar.
It will take you to the playlist page, and you can copy the URL here.
Now that you have the playlist URL, you can use it to download the audio files in MP3 format in the following fashion:
youtube-dl --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" playlist_URL
That scary looking -o "%(title)s.%(ext)s"
specifies the output file (with option -o) and instructs it to use the title of the video and the extension (MP3 in this case) for naming the audio files.
I hope you find this quick tip helpful. Enjoy the audio files π