It’s a script-based alternative to online youtube-to-mp3 converters, but it’s much more faster, much more reliable and easy to customize. You don’t have to visit those spammy online converters anymore, and what’s more, you can run multiple instances of the same script so that you’ll be able to convert several youtube videos simultaneously.

I use this on my Ubuntu (Linux), but Windows and Mac users should be able to do the same by writing the equivalent shell script for their own command lines. Before you can use the script make sure you have “youtube-dl” and “ffmpeg” installed. We will use youtube-dl to download youtube videos, and ffmpeg to convert them into the mp3 format. Create a new file…

gedit youtube2mp3 

…and paste the following script:

x=~/.youtube-dl-$RANDOM-$RANDOM.flv youtube-dl --output=$x --format=18 "$1" ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$2" rm $x 

Save and close gedit. Now install the script somewhere easily accessible.

sudo install youtube2mp3 /usr/local/bin 

Now you can convert youtube videos into mp3 files by using the following command (including the double quotes):

youtube2mp3 "youtube-link" "mp3-file.mp3"


 
 

For this script to work, ffmpeg must be able to use the libmp3lame codec. As far as I know this is not provided with the ffmpeg on Ubuntu, but there are many tutorials on the internet that could help you do this. Also, the script is very verbose. Use the following command if you don’t want to see all the messages on your screen:

youtube2mp3 "youtube-link" "mp3-file.mp3" > /dev/null


You can also use the following command to make the script run in the background. This way you will be able to run multiple instances of the script at the same time.

youtube2mp3 "youtube-link" "mp3-file.mp3" > /dev/null & 

 

How it works

The way this script works is really simple. First it downloads the youtube video into a temporary file, converts the video to mp3 and then deletes the temporary file. Let’s go through this script step-by-step.

1. The first line of the script assigns a random .flv filename to the variable $x.

2. The second line downloads the youtube video into the temporary file named $x. It automatically downloads the HQ version of the video if it’s available.

3. The third line extracts the audio from the video and converts it into an mp3 file with the filename you specified.

4. The last line removes the temporary file created in step 2.

 
 
Advertisements