Linux Convert YouTube Flash Video (FLV) to MP3

Ever wanted to download music videos from YouTube, then extract the music component from the video and convert to MP3? Well here's how to do it!

Firstly, to download the videos from YouTube, you will need to install the Flashgot extension into your Firefox web browser. This will add a small icon at the bottom right of the browser, and when it detects video (such as when you go to YouTube), you will be able to click on the icon and the video is grabbed and (in my instance) saved to the /tmp directory.

To extract and convert to MP3 we need to use the Linux command line ffmpeg utility.

The example below shows that I have moved the target file to a directory for that artiste; in this instance it is the rather shouty Charice. Maybe I should have chosen a more uber trendy yet obscure band so I come over as hip and cool Puzzled

Quick Copy/Paste Solution for the Impatient

ffmpeg -i infile -acodec libmp3lame -ac 2 -ab 256k -vn -y outfile

Detailed Solution for the Relaxed

Ok - to run the ffmpeg utility, issue the following:

badzilla@laptop4:~/Music/Charice> ffmpeg  -i Charice_* -acodec libmp3lame -ac 2 -ab 256k -vn -y "Charice - Pyramid.mp3"
FFmpeg version SVN-r25320, Copyright (c) 2000-2010 the FFmpeg developers
  built on Oct 14 2010 17:24:48 with gcc 4.5.0 20100604 [gcc-4_5-branch revision 160292]
  configuration: --shlibdir=/usr/lib --prefix=/usr --mandir=/usr/share/man --libdir=/usr/lib --enable-shared --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libspeex --enable-libxvid --enable-postproc --enable-gpl --enable-x11grab --extra-cflags='-fomit-frame-pointer -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gsm' --enable-libschroedinger --enable-libdirac --enable-libgsm --enable-avfilter --enable-libvpx --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libdc1394 --enable-pthreads
  libavutil     50.32. 0 / 50.32. 0
  libavcore      0. 9. 0 /  0. 9. 0
  libavcodec    52.92. 0 / 52.92. 0
  libavformat   52.79. 0 / 52.79. 0
  libavdevice   52. 2. 2 / 52. 2. 2
  libavfilter    1.48. 0 /  1.48. 0
  libswscale     0.12. 0 /  0.12. 0
  libpostproc   51. 2. 0 / 51. 2. 0
[flv @ 0x807dc30] Estimating duration from bitrate, this may be inaccurate
                                                                                                                                                          
Seems stream 0 codec frame rate differs from container frame rate: 47.95 (5994/125) -> 24.00 (24/1)
Input #0, flv, from 'Charice___Pyramid__featuring_Iyaz___Video.flv':
  Metadata:
    duration        : 247
    starttime       : 0
    totalduration   : 247
    width           : 640
    height          : 360
    videodatarate   : 788
    audiodatarate   : 65
    totaldatarate   : 857
    framerate       : 24
    bytelength      : 26474918
    canseekontime   : true
    sourcedata      : B4A7D6941HH1287223136868392
    purl            :
    pmsg            :
  Duration: 00:04:06.66, start: 0.000000, bitrate: 873 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x360 [PAR 1:1 DAR 16:9], 806 kb/s, 24 tbr, 1k tbn, 47.95 tbc
    Stream #0.1: Audio: aac, 22050 Hz, stereo, s16, 66 kb/s
Output #0, mp3, to 'Charice - Pyramid.mp3':
  Metadata:
    TSSE            : Lavf52.79.0
    Stream #0.0: Audio: libmp3lame, 22050 Hz, stereo, s16, 256 kb/s
Stream mapping:
  Stream #0.1 -> #0.0
Press [q] to stop encoding
size=    4830kB time=247.30 bitrate= 160.0kbits/s   
video:0kB audio:4830kB global headers:0kB muxing overhead 0.000667%
badzilla@laptop4:~/Music/Charice> ls -lash
total 31M
4.0K drwxr-xr-x  2 badzilla users 4.0K 2010-10-16 12:01 .
4.0K drwxr-xr-x 55 badzilla users 4.0K 2010-10-16 11:23 ..
26M -rw-r--r--  1 badzilla users  26M 2010-04-14 22:05 Charice___Pyramid__featuring_Iyaz___Video.flv
4.8M -rw-r--r--  1 badzilla users 4.8M 2010-10-16 12:01 Charice - Pyramid.mp3
badzilla@laptop4:~/Music/Charice>

The original command may be lost in that little lot, so here we have it:
ffmpeg -i Charice_* -acodec libmp3lame -ac 2 -ab 256k -vn -y "Charice - Pyramid.mp3"

Or, more appropriately:
ffmpeg -i infile -acodec libmp3lame -ac 2 -ab 256k -vn -y outfile

Here we can see the MP3 file has been created and weighs in at 4.8MB. To check out everything is ok, use the mediainfo utility

badzilla@laptop4:~/Music/Charice> mediainfo "Charice - Pyramid.mp3"
General                                                                                                                                                   
Complete name                    : Charice - Pyramid.mp3                                                                                                  
Format                           : MPEG Audio                                                                                                             
File size                        : 4.72 MiB
Duration                         : 4mn 7s                                                                                                                 
Overall bit rate                 : 160 Kbps
Writing library                  : LAME3.98.4
Encoding settings                : Lavf52.79.0

Audio
Format                           : MPEG Audio
Format version                   : Version 2
Format profile                   : Layer 3
Format_Settings_Mode             : Joint stereo
Format_Settings_ModeExtension    : MS Stereo
Duration                         : 4mn 7s
Bit rate mode                    : Constant
Bit rate                         : 160 Kbps
Channel(s)                       : 2 channels
Sampling rate                    : 22.05 KHz
Stream size                      : 4.72 MiB (100%)
Writing library                  : LAME3.98.4


badzilla@laptop4:~/Music/Charice>

To finish off the job you may wish to use EasyTag to set the MP3 tags so your MP3 player can file the track correctly.

Had Problems?

Well, I certainly did. It is important that you ensure all the relevant libraries are on exactly the same version. If your attempt to convert coughs, have a look for:

WARNING: library configuration mismatch

and
ffmpeg: relocation error: ffmpeg: symbol av_strerror, version LIBAVUTIL_50 not defined in file libavutil.so.50 with link time reference

and
ffmpeg: relocation error: ffmpeg: symbol avcodec_get_context_defaults3, version LIBAVCODEC_52 not defined in file libavcodec.so.52 with link time reference

All these diagnostics mean you have libraries out of whack somewhere. Look at your installed libraries with respect to ffmpeg. On my system you will see everything matches ok.
ffmpeg