create your own

Youtube to MP3 on Ubuntu Linux

83
rate or flag this page

By JeffreyV


In this hubpage I'd like to share with you a nifty (and geeky) little trick I use to directly convert youtube videos into mp3 files, and store them on my harddrive. 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.

Print   —   Rate it:  up  down  flag this hub

RSS for comments on this Hub

Giovanni  says:
3 months ago

>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.

Where? I have searched for a tutorial to find a way of doing this but with no success

Any ideas?

Marco  says:
5 weeks ago

Hi, do you think this script can be used to build a website? Any suggestion on how to do it?

Thanks

jason  says:
5 weeks ago

works great! Thanks.

JeffreyV profile image

JeffreyV  says:
4 weeks ago

@Giovanni:

It's just a matter of installing the right Ubuntu packages. I've totally forgotten which ones those are, so searching through the packages might help.

@Marco:

As long as you have the necessary tools, you should be able to do it. For example, you can run an equivalent script in PHP or ASP.

Erik T  says:
3 weeks ago

Worked great. Just use the syn package manager to get youtube-dl and ffmpeg. (You can also find the libmp3lame there too.) Thanks a bunch!

franco  says:
2 weeks ago

works on gentoo too....

thanks

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working