Youtube to MP3 on Ubuntu Linux

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.

In web development

I use my youtube to mp3 converter script in Linux as a convenience. But some of the commenters here seem to have found a use for it in web development. They convert youtube to mp3 on their website where they have integrated this little script I wrote above. I think it's very creative.

Speaking of web development, I recently wrote a piece for hubpages on web development and how to build a website. If you're interested in building your own website, I suggest you check it out and leave a comment.

More by this Author


41 comments

Giovanni 7 years 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 7 years ago

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

Thanks


jason 7 years ago

works great! Thanks.


JeffreyV profile image

JeffreyV 7 years ago from Amsterdam Author

@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 7 years 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 7 years ago

works on gentoo too....

thanks


Steven E 6 years ago

Ya, use your package manager and make sure you have these packages:

libmp3lame

youtube-dl

mark them if not already marked, hit apply and you should be in business...

now if someone could write a dummy script "input youtube url" "input mp3 file name"... and poof... :D


KaoKao 6 years ago

On Ubuntu 9.10 you need this package: libavcodec-unstripped-52

Otherwise ffmpeg couldn't find libmp3lame :)


Joseph Schwenker 6 years ago

For some reason, it doesn't work, and my terminal outputs this:

joseph@joseph:/usr/local/bin$ youtube2mp3 http://www.youtube.com/watch?v=8e0qO01Hvfc&fmt=18 Cosmica.mp3

[1] 31269

Retrieving video webpage... bash: Cosmica.mp3: command not found

joseph@joseph:/usr/local/bin$ done.

Extracting URL "t" parameter... done.

Requesting video file... done.

Video data found at http://v1.cache2.c.youtube.com/videoplayback?ip=0....

Retrieving video data: 100.0% ( 9.92M of 9.92M) at 46.71k/s ETA 00:00 done.

Video data saved to /home/joseph/.youtube-dl-350-11620.flv

FFmpeg version git-91be88c, Copyright (c) 2000-2009 Fabrice Bellard, et al.

configuration: --enable-gpl --enable-nonfree --enable-shared --enable-pthreads --enable-libx264 --enable-libfaac --enable-libtheora

libavutil 50. 3. 0 / 50. 3. 0

libavcodec 52.32. 0 / 52.20. 0

libavformat 52.36. 0 / 52.31. 0

libavdevice 52. 2. 0 / 52. 1. 0

libswscale 0. 7. 1 / 0. 7. 1

built on Jul 24 2009 21:22:03, gcc: 4.3.3

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x93d3ac0]ISO: File Type Major Brand: mp42

st:0 removing common factor 4 from timebase

Seems stream 1 codec frame rate differs from container frame rate: 59.94 (60000/1001) -> 29.97 (30000/1001)

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/joseph/.youtube-dl-350-11620.flv':

Duration: 00:05:07.47, start: 0.000000, bitrate: 270 kb/s

Stream #0.0(und), 256/11025: Audio: aac, 44100 Hz, stereo, s16

Stream #0.1(und), 1001/30000: Video: h264, yuv420p, 480x360, 1001/60000, 29.97 tbr, 29.97 tbn, 59.94 tbc

Unable to find a suitable output format for ''

joseph@joseph:/usr/local/bin$

Any ideas?


Joseph Schwenker 6 years ago

Oops, sorry. I didn't put the quotes in! It should work fine now. I'll post again if it doesn't.


Joseph Schwenker 6 years ago

Oh dear, it is still not working. What am I doing wrong? This time, the terminal outputted:

joseph@joseph:/usr/local/bin$ youtube2mp3 "http://www.youtube.com/watch?v=8e0qO01Hvfc" "Cosmica.mp3"

Retrieving video webpage... done.

Extracting URL "t" parameter... done.

Requesting video file... done.

Video data found at http://v1.lscache4.c.youtube.com/videoplayback?ip=...

Retrieving video data: 100.0% ( 9.92M of 9.92M) at 46.86k/s ETA 00:00 done.

Video data saved to /home/joseph/.youtube-dl-9592-2530.flv

FFmpeg version git-91be88c, Copyright (c) 2000-2009 Fabrice Bellard, et al.

configuration: --enable-gpl --enable-nonfree --enable-shared --enable-pthreads --enable-libx264 --enable-libfaac --enable-libtheora

libavutil 50. 3. 0 / 50. 3. 0

libavcodec 52.32. 0 / 52.20. 0

libavformat 52.36. 0 / 52.31. 0

libavdevice 52. 2. 0 / 52. 1. 0

libswscale 0. 7. 1 / 0. 7. 1

built on Jul 24 2009 21:22:03, gcc: 4.3.3

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8dc4ac0]ISO: File Type Major Brand: mp42

st:0 removing common factor 4 from timebase

Seems stream 1 codec frame rate differs from container frame rate: 59.94 (60000/1001) -> 29.97 (30000/1001)

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/joseph/.youtube-dl-9592-2530.flv':

Duration: 00:05:07.47, start: 0.000000, bitrate: 270 kb/s

Stream #0.0(und), 256/11025: Audio: aac, 44100 Hz, stereo, s16

Stream #0.1(und), 1001/30000: Video: h264, yuv420p, 480x360, 1001/60000, 29.97 tbr, 29.97 tbn, 59.94 tbc

ffmpeg: symbol lookup error: ffmpeg: undefined symbol: avcodec_channel_layout_num_channels

[1]+ Done youtube2mp3 http://www.youtube.com/watch?v=8e0qO01Hvfc

joseph@joseph:/usr/local/bin$

I used Nautilus's search function to search my entire hard drive for Cosmica.mp3, but it could not find anything. I am using Ubuntu 9.04, if that helps. I have ffmpeg and youtube-dl installed. Thanks!


123 6 years ago

perfect, thx!


Pétur Ingi 6 years ago

Hello, I wrote a script which makes use of your commands..

It can be found on http://www.petur.eu/

Install instructions are in the source.


Socan 6 years ago

Thanks, good tip! It works on Fedora of course but youtube-dl and ffmpeg package is needed.


Nathan Broadbent 6 years ago

I modified the script slightly so that I could download, convert and play youtube videos in real-time, as they buffer. Plays the audio in vlc. Not the most streamlined solution, but the best I could come up with, since mpg321 doesn't seem to be able to play a file as it downloads.

I used the '-re' flag for ffmpeg, so that it only encodes the file as fast as it would be played.

call it like this:

./youtube2mp3_realtime $video_url

#!/bin/bash

# Script downloads a youtube video, converts it and plays it as it buffers.

download_buffer=5

conversion_buffer=3

output_file=outputfile.mp3

download_file=youtube-dl-$RANDOM-$RANDOM.flv

youtube-dl --output=$download_file --format=18 "$1" &

sleep $download_buffer

ffmpeg -re -i "$download_file" -acodec libmp3lame -ac 2 -ab 128k -vn -y "$output_file" &

sleep $conversion_buffer

vlc $output_file

# clear the files when they have finished playing.

rm $download_file $output_file

Your post was excellent, thanks very much!


dreamland 6 years ago

Just tested on Ubuntu 10.04 and works great and increased the bitrate to 192k for a bit more fidelity.

Thx.


Sergio Arroyos 6 years ago

excellent contribution!


chris 6 years ago

[youtube] Setting language

[youtube] 6yjyGH80ggQ: Downloading video info webpage

[youtube] 6yjyGH80ggQ: Extracting video information

ERROR: format not available for video

FFmpeg version SVN-r0.5.1-4:0.5.1-1ubuntu1, Copyright (c) 2000-2009 Fabrice Bellard, et al.

configuration: --extra-version=4:0.5.1-1ubuntu1 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static

libavutil 49.15. 0 / 49.15. 0

libavcodec 52.20. 1 / 52.20. 1

libavformat 52.31. 0 / 52.31. 0

libavdevice 52. 1. 0 / 52. 1. 0

libavfilter 0. 4. 0 / 0. 4. 0

libswscale 0. 7. 1 / 0. 7. 1

libpostproc 51. 2. 0 / 51. 2. 0

built on Mar 4 2010 12:35:30, gcc: 4.4.3

/home/chris/.youtube-dl-10099-4248.flv: no such file or directory

rm: ???????? ????????? «/home/chris/.youtube-dl-10099-4248.flv»: No such file or directory

i can find any song so it must not work...

any ideas why?


dan 6 years ago

Ubuntu 9.10 does not have the latest youtube-dl script version, that is why you have the "format not available for video" error

You need to get the latest version of script from

http://freshmeat.net/projects/youtube-dl


andrew 5 years ago

Hi guys, I just wanted to share my experience getting this script running on Ubuntu 10.10. I am relatively tech un-savvy, just to put it out there. Anyway, as poster Dan mentioned, you need to get the latest youtube-dl from freshmeat (or wherever). Then, if you are like me and used the package manager to unstall an unworking youtube-dl, make sure you use the package manager to remove it. Then download the latest one (save link as), then install it from your download directory like: sudo install youtube-dl /usr/local/bin. Also, I didn't see a way to get ffmpeg from the package manager so I googled it and found some instructions here: http://pasindudps.blogspot.com/2010/12/compiling-f... I just followed all the instructions (cut and past into a terminal) exactly as the guy said. As I said, I'm not very tech-savvy, so I have no idea if I also installed a root kit on myself as I was following his instructions, but the end result was I have a working ffmpeg now (so no "ffmpeg: command not found" error). I imagine that periodically I will have to keep replacing the youtube-dl as Youtube keeps making changes that break it, but other than that I think I am all set. Thanks to JeffreyV for making this webpage, and thanks to Dan for pointing out the need for a current youtube-dl. I hope I was able to help another noob like myself. Happy converting!


dtg 5 years ago

works. thanks.

if you need libm3lame on ubuntu

http://ubuntuforums.org/showthread.php?t=1117283


dan 5 years ago

If you get the error:

Unknown encoder 'libmp3lame'

even if you have libmp3lame0 and libmp3lame-dev packages installed; you actually need to do:

#sudo apt-get install ffmpeg libavcodec-extra-52

Tested on Ubuntu 10.10

thanks dtg


yktoo 5 years ago

It would be more sensible to download to /tmp rather than to ~/ directory.


Vasja 5 years ago

For the problem: ERROR: format not available....

just type in the terminal:

%sudo youtube-dl -U

(will upgrade youtube-dl)


Juliana 5 years ago

As a new user of Ubuntu 10.10, this was pretty easy to figure out, so thanks! Thanks also to Dan and Andrew for the advice about youtube-dl.


Jason 5 years ago

Hey Jeffrey. Thanks for the useful script. Is there any way to generalize this script so that it converts all youtube videos in a playlist? Perhaps assigning the final MP3 the same name as the video? This last part I assume to be hard, since I'm not sure whether youtube_dl provides the video's title anywhere.

Thanks,

J


Rytis 5 years ago

Thanks for this! Works great!

Had to do what dan suggested, installing the lame packages, and replacing the contents of /usr/bin/youtube-dl with the latest from http://freshmeat.net/projects/youtube-dl

Ubuntu 10.10 here


Lilith 5 years ago

Thanks! Works like a charm!


Faustino 5 years ago

I get the folowing error:

ERROR: unable to download video (format may not be available)

Any ideas on how to solve this?? I have already upgraded youtube-dl


telesyk 5 years ago

I am getting the same error as faustino :(


Sugmeg 5 years ago

I got this working in a shell script, when I try to prompt it using PHP, it returns the result, but no action is taken. I'm using Ubunuty, I have given the apache user root-priviliges (just for test, ofc), still doesn't work.

Any experience with this?


mememe 5 years ago

ERROR: no fmt_url_map or conn information found in video info


Pierpa 5 years ago

Great script.

Modified a little, in order you can avoid supplying output file name - it can be got by youtube itself.

x=~/.youtube-dl-$RANDOM-$RANDOM.flv

if [[ $2 = "" ]]

then

OUTF=`youtube-dl -e $1`

else

OUTF=$2

fi

youtube-dl --output=$x --format=18 "$1"

ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$OUTF".mp3

rm $x

To get libmp3lame working I had to install ubuntu-restricted-extras


Donn 5 years ago

A small improvement to set the mp3 filename automatically from the YouTube song title:

x=/tmp/youtube-dl-$RANDOM-$RANDOM.flv

TITLE=`youtube-dl -e "$1"`

echo $TITLE

youtube-dl --output=$x --format=18 "$1"

ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$TITLE.mp3"



ArturBV 4 years ago

ERROR:

# youtube http://www.youtube.com/watch?v=6F2t9E2tSPk shamanic-theta-binaural-waves.mp3

Usage: youtube-dl [options] video_url

youtube-dl: error: no such option: --format

convert flv to mp3

ffmpeg: error while loading shared libraries: libavfilter.so.0: cannot open shared object file: No such file or directory

rm: imposível remover `/tmp/youtube-dl-6662-4744.flv': Arquivo ou diretório inexistente

done


yoda 4 years ago

thanks, this was awesome. i've been using these converter websites that always lag out, and this script does the trick. :-)


Frankie 4 years ago

Jeffrey, excellent post this is great!

Hey Dan, thanks for the tip regarding installing ffmpeg libavcodec-extra-52; that worked perfectly on my Ubuntu Server box. Good stuff indeed!


Meyman 4 years ago

Super. Thanks

For ubuntu 11.10 install:

sudo apt-get install ffmpeg libavcodec-extra-53


richard 4 years ago

Thanks! It works great. Only glitch was in Mint 11 where I'm running this, there was no libmp3lame to be linked, so it bombed on first try. After scratching around a bit on the forums, I did find a library to install that includes libmp3lame. There seems to be some copyright and other issues so it is not routinely distributed with the Mint system. This could make it difficult for some to get it working...


Dan 4 years ago

I wrote a quick "how too" for newbies like me for Ubuntu if the moderator will post it. (.odt format) I didn't know how to get to terminal mode.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

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


    Click to Rate This Article
    working