Download MP3 Tracks from a YouTube Video Using YouTube-DL

We all love music, be it gym, during work, or just chilling out, I do use amazon prime and spotify but there are times when we face internet trouble and when we do get grumpy when not able to listen to some good numbers, for such times I keep some music downloaded and play them from my Bluetooth speakers, that way I don’t have to do a lot of pairing from multiple devices with internet.

I use youtube-dl which is a freely available tool based on python, I try to keep things simple, and on my android phone downloaded termux which helps me run Linux commands and provides me with a development environment.

The next step is to allow termux permission to your storage where you would keep your music which can then be accessed by your music player.

I won’t go into details on how to set up termux and allow termux access to your phone storage, you can google search and can get a lot of articles to do this.

To convert music to mp3 you may need to install two packages such as avconv and ffmpeg you can install any of these or both, youtube-dl defaults to converting using avconv.

$ sudo apt install ffmpeg avconv

I try to keep my work env clean so I create a virtual env.

$ python -m venv youtube-dl
$ cd youtube-dl/
$ source bin/activate
$ pip install youtube-dl

Once you have youtube-dl installed move to the directory where you want to download your mp3’s

$ cd /path/to/download/music
$ youtube-dl -x --audio-format mp3

The command will download the music you want, you can also give the playlist which you want to download, just ensure to add it in " " otherwise, this downloads only one file as there is ‘&’ in the URL which sends your command in the background and downloading only one file.

If you are a fan of cover art, you may want to try and download the album cover as well.

$ youtube-dl -x --embed-thumbnail --audio-format mp3

To download the entire playlist

youtube-dl -x --embed-thumbnail --audio-format mp3 ""

It is possible that you may not like all the songs in other people’s playlists. So what if you want to download many songs from different playlists? Well a workaround on that matter is to get a list of URLs in a single file.

Write the URLs in a file called videos.txt and make sure to keep one URL at a line. Then you can use the following "for" loop to download the songs:

$ for i in $(<videos.txt); do youtube-dl -x --audio-format mp3 $i; done

%d bloggers like this: