How to extract audio from YouTube

The easy way for Linux users

[2010-08-19]

For this you will need:

  1. Linux

  2. youtube-dl

  3. MPlayer or VLC + vorbis-tools

  4. the YouTube URL of the video whose audio you want

I found the latest version of youtube-dl worked better than the version packaged for my installation of Linux. It's a single Python script, so I saved in it ~/bin/ and made it executable with "chmod +x ~/bin/youtube-dl". The other stuff you'll find in the standard repositories for your distribution, if it isn't installed already.

Download the video:

 $ youtube-dl --all-formats 'http://www.youtube.com/watch?v=xyz'

"--all-formats" allows you to choose the quality you want.

Extract audio:

 $ cvlc --no-video --aout=file "xyz-18.mp4" vlc://quit 
$ oggenc -o "decent_filename.ogg" audiofile.wav

Or pull out the MP3 from an FLV with:

 $ mplayer -dumpaudio "xyz-5.flv" -dumpfile "decent_filename.mp3"

That's all.

VLC can extract the MP3 or AAC audio stream, but I found the methods above attractively simple. Extracting an MP3 from an FLV with VLC can be done with:

 $ cvlc "xyz-5.flv" vlc://quit --sout '#transcode{acodec=mp3}:duplicate{dst=std{access=file,mux=raw,dst="decent_filename.mp3"},select="novideo"}'

Note that a standard Ubuntu installation lacks the necessary codecs. In Lucid, install libavcodec-extra-52. To transcode mp4a streams on Ubuntu, you could try building FFMPEG and VLC from source, but I just don't have that kind of enthusiasm.

I found some MP3 files extracted using MPlayer gave the incorrect duration, and those files caused odd playback issues with Rhythmbox. And I could not play AAC files extracted with the same method.

You can find a table of the codecs YouTube uses at Wikipedia. The "fmt" value at the top of the table corresponds to the end of the filename saved by youtube-dl, so you can see what codec and quality your files are.