Download audio in mp3 format from Youtube

Got your favorite youtube video and yet you don’t have it in an audio format such as mp3 to play it offline? With open source tools, you can grab that video and convert it to mp3 at no cost.

Prepare a directory for downloading mp4 format files from youtube.

mkdir /home/youtube

Tools you need

1. youtube-dl: A python script to download videos from youtube – http://rg3.github.io/youtube-dl/download.html

Using curl –

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

Using wget –

sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

Using pip –

sudo pip install --upgrade youtube_dl

2. ffmpeg: an audio/video conversion tool.

 apt-get install ffmpeg libavcodec-extra-53

Procedure
1. Make the youtube link ready for download and then download the mp4 using youtube-pl script
2. Use ffmpeg tools to convert mp4 to mp3

Sample download

Let us download the following youtube link

youtube-dl -f 18 -t http://www.youtube.com/watch?v=dAG2qxvYwsY

options: -f is for file format of the youtube video (check youtube-dl documentation for the whole list)

Next, convert it to mp3

ffmpeg -i Freselam_Mussie_s_Tsinih_Zeytibli-dAG2qxvYwsY.mp4 -f mp3 -ab 192000 -vn Freselam_Mussie_s_Tsinih_Zeytibli-dAG2qxvYwsY.mp3

options: -i for input
         -f for output file format
         -ab for bit rate
         -vn for Disable video recording.

References –

http://rg3.github.com/youtube-dl/

http://rg3.github.io/youtube-dl/download.html