Ubuntu. Convert youtube video to mp3 on-line with a command line script.

Ubuntu command line script (source: http://jeffreyv.hubpages.com/hub/Youtube-to-MP3-on-Ubuntu-Linux). With this 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.

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”

avconv -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y “$2”

rm $x

————————-

(avconv is used in DEBIAN and UBUNTU instead of ffmpeg)

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,  avconv OR ffmpeg must be able to use the libmp3lame code (libavcodec-extra-53 and libavcodec-extra-52)

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.

youtube-dl install from Ubuntu Software Center

youtube-dl is a small command-line program to download videos from YouTube.com and other sites that don’t provide direct links to the videos served.

youtube-dl allows the user, among other things, to choose a specific video quality to download (if available) or let the program automatically determine the best (or worst) quality video to grab. It supports downloading entire playlists and all videos from a given user.

Currently supported sites are: CollegeHumor, Comedy Central, Dailymotion, Facebook, Metacafe, MyVideo, Photobucket, The Escapist, Vimeo, Yahoo!, YouTube, blip.tv, depositfiles.com, video.google.com, xvideos, Soundcloud, InfoQ, Mixcloud, OpenClassRoom.