Youtube to MP3 on Ubuntu Linux
83In 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.
PrintShare it! — Rate it: up down flag this hub
Hi, do you think this script can be used to build a website? Any suggestion on how to do it?
Thanks
works great! Thanks.
@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.
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!
works on gentoo too....
thanks











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?