Grabbing Your Music from YouTube: Do It Your Way

A few months ago my father-in-law said that his company was renewing their computers. When I heard that some second-hand PCs were about to be available, I decided to take some of them, thinking that a few old PCs would not hurt when it comes to enlarging my home network and doing experiments with GNU/Linux. When my father-in-law asked if it would be possible to reformat one of those computers so that he could use it at his home, I jumped at the opportunity to bring another user to the world of GNU/Linux. A few days passed and he was a happy user of his new computer running Ubuntu, and he was enjoying his Firefox while he explored the web. I don’t know what other people think or usability studies say, but he said that he had no problem using the system and he was surprised that I did not have to install an anti-virus.

After a few weeks he said that he discovered the wonderful YouTube system, and being an avid jazz listener he liked to explore lots of classical pieces from 1950s and 60s. He wondered if it would be possible to grab the music out of those YouTube videos and put them on a CD so that he could listen to them while he drove his car. First I thought that some Firefox add- on probably existed for that, but I was too lazy to search for it. Instead, I decided to see if I could hack my way through my favorite development environment, Bash, and come up with something that can handle the task.

The first step was to think about which components I needed so that I could integrate them using Bash, the perfect glue:

  • A utility that can download the video files from YouTube (or other video services)
  • A utility to extract the sound from the video file
  • And a utility that can convert the sound file to the formats I wanted such as MP3

Remembering that one of the key aspects of UNIX and GNU/Linux utilities is “do one thing and do it very well”, I came up with the following utilities that did one thing and did it very well:

  • youtube-dl: a nice Python script that can download videos from YouTube and other sites
  • ffmpeg: the powerful utility to process and convert any kind of video or sound file
  • lame: the perfect choice for encoding sound data into MP3

The first component, youtube-dl, is a very simple-to-use command line utility to download the video files from YouTube. For example in order to download Frank Sinatra’s “My Way” from http://www.youtube.com/watch?v=6E2hYDIFDIU all you have to do is issue the command:

$ youtube-dl http://www.youtube.com/watch?v=6E2hYDIFDIU

to have the video file stored at the some location. In this particular case you should be able to see 6E2hYDIFDIU.flv at the directory you ran the youtube-dl command. You also have the capability of getting the title of the video automatically with the help of youtube-dl:


$ youtube-dl --get-title http://www.youtube.com/watch?v=6E2hYDIFDIU 

Frank Sinatra, My Way, With Lyrics

Once you have video file, the second step is to extract the audio data from it which can be easily achieved with a simple ffmpeg command:

$ ffmpeg -i 6E2hYDIFDIU.flv 6E2hYDIFDIU.wav

This will immediately extract the audio information from the already downloaded file into a WAV encoded file. And once you have audio data in that file, it is ready to be converted into and MP3 file which can be done using the lame encoder utility. As with the previous utilities the basic usage of lame is very simple:

$ lame 6E2hYDIFDIU.wav 6E2hYDIFDIU.mp3

Voilà! You have your MP3 file ready for your listening pleasure. You can delete the big .wav file and burn your .mp3 file to a CD to listen to it on your car stereo.

Once we have all the components ready and tested we can create a very simple Bash script that can take the input as the YouTube web address and produce the MP3 file as the output:

1 #!/bin/bash 
2	# A very simple Bash script to download a YouTube video 
3	# and extract the music file from it. 
4 address=$1 
5 regex='v=(.*)' 
6	if [[ $address =~ $regex ]]; then 
7	video_id=${BASH_REMATCH[1]}
8	video_id=$(echo $video_id | cut -d'&' -f1) 
9	video_title="$(youtube-dl --get-title $address)" 
10	youtube-dl $address 
11	ext="flv" 
12	ffmpeg -i $video_id.$ext "$video_title".wav 
13	lame "$video_title".wav "$video_title".mp3 
14	rm $video_id.$ext "$video_title".wav 
15 else 
16	echo "Sorry but the system encountered a problem." 
17 fi

If you save this script as youtube2mp3.sh and turn it into an executable by issuing the

chmod +x youtube2mp3.sh

You can run run it to download “My Way” using the following command:

./youtube2mp3.sh http://www.youtube.com/watch?v=6E2hYDIFDIU

The fourth line of the script is where we get the YouTube video address from the command line as the first parameter. Lines 5, 6, 7 and 8 rely on the use of regular expressions in Bash and the cut utility. We want the part which is starting after ‘v=’ and we don’t need the parameters that may follow it such as ‘&feature=related’. Once they are processed we get the unique code that YouTube uses to identify the videos and store it in the video_id variable. Line 9 is where we retrieve the title of the video file with the help of youtube-dl and store it in the video_title variable. For reasons of simplicity I assumed that the extension of the video file will be ‘.flv’ and stored it in the variable ext. Lines 12 and 13 finish the task of extracting of sound data and storing it in an MP3 file and finally at line 14 we do the house cleaning by deleting the video file as well as the .wav file that is no longer required. If the script encounters any problem with with the YouTube address format that was provided line 16 reports a very simple error message.

The simple Bash script above is far from perfect. Its purpose was to be as simple as possible without taking lots of important things into account such as realistic error handling, different YouTube video formats and a simple help system to report the usage in case the parameters were not entered correctly or missing. A slightly more complicated Bash script which also makes use of a simple graphical user interface with the help of wonderful xenity utility is available at https://github.com/emres/youtube2mp3 (feel free to hack the code and send pull requests to the author ;-) As usual I had a lot of fun exploring those utilities and combining them to create a solution that can be useful for other people, too. Of course that would be almost impossible without the flexibility of GNU/Linux and the free software world in which we live. I hope you’ll enjoy your music while you drive your car as much as I and my father-in-law do.

Happy hacking.

Emre Sevinç currently works as a software developer and researcher. He's been involved with GNU/Linux since 1994 when he first met it at the math department of Istanbul Technical University.

Load Disqus comments