Follow me on Twitter for my latest adventures!
I've seen tools available to download just the audio from a youtube video, in various formats; but as per your explanation it seems, that the audio is integrated with the video in the .swf file. How can we extract only the audio part and have it converted to a format like mp3?
As I have written a few articles before on how to download YouTube videos with Perl, gawk and VBScript, and how to convert the downloaded flash video files (flv) to divx or xvid, or any other format with ffmpeg, it was very easy to help this guy.
This is a guide that explains how to extract audio tracks from any videos, not just YouTube.
First, lets download the ffmpeg tool (that's for Windows Operating System. If you are using linux operating system, you can get the ffmpeg tool as a package distribution) and open the ffmpeg documentation in another window.
Lets choose a sample video which we will extract the audio track from. I found some music video clip "My Chemical Romance - Famous Last Words" (http://www.youtube.com/watch?v=8bbTtPL1jRs).
Now, lets download the music video. If you are on a windows machine, you may use my VBScript program to download the video (download vbscript youtube video downloader, read how to use it here), or if you are on linux, you may use gawk program to download the video (download gawk youtube video downloader, read how to use it here).
After downloading the video, I ended up with a file named My_Chemical_Romance_-_Famous_Last_Words.flv.
Once you have downloaded the video, just for the sake of interest, lets find out the audio quality of this You Tube audio video.
The ffmpeg documentation does not tell us about a switch which would just output the audio parameters of the input file. After experimenting a little with the ffmpeg tool, it can be found that by just specifying '-i' switch and the input video file, the ffmpeg will output input streams information and quit.
Here is an example of how it looks:
c:\> ffmpeg.exe -i My_Chemical_Romance_-_Famous_Last_Words.flv Seems that stream 1 comes from film source: 1000.00 (1000/1) -> 24.00 (24/1) Input #0, flv, from 'My_Chemical_Romance_-_Famous_Last_Words.flv': Duration: 00:04:27.4, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Audio: mp3, 22050 Hz, mono, 64 kb/s Stream #0.1: Video: flv, yuv420p, 320x240, 24.00 fps(r) Must supply at least one output file
From this information (2nd line in bold) we can read that the audio bitrate of a YouTube video is 64kbit/s, sampling rate is 22050Hz, the encoding is mp3, and it's a mono audio.
You will be surprised how easy it is to extract the audio part as it is in the video. By just typing:
c:\> ffmpeg.exe -i My_Chemical_Romance_-_Famous_Last_Words.flv famous_last_word.mp3
the ffmpeg tool will extract it to an mp3 audio file!
That's it! After running this command you should have 'famous_last_words.mp3' file in the same folder/directory where the downloaded video file was!
We can go a little further and look up various audio switches on the documentation of ffmpeg. For example, if we had some fancy alarm clock which can be stuffed an mp3, you might not need the whole 64kbit/s of bitrate. You might want to convert the audio to a lower bitrate, say 32kbit/s.
The Section 3.5 - Audio Options of the ffmpeg documentation says:
`-ab bitrate' - Set the audio bitrate in bit/s (default = 64k).
So, by specifying a command line switch '-ab 32k' the audio will be converted to a lower bitrate of 32kbit/s.
Here is the example of running this command:
c:\> ffmpeg.exe -i My_Chemical_Romance_-_Famous_Last_Words.flv -ab 32k famous_last_word.32kbit.mp3 [...] Seems that stream 1 comes from film source: 1000.00 (1000/1) -> 24.00 (24/1) Input #0, flv, from 'My_Chemical_Romance_-_Famous_Last_Words.flv': Duration: 00:04:27.4, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Audio: mp3, 22050 Hz, mono, 64 kb/s Stream #0.1: Video: flv, yuv420p, 320x240, 24.00 fps(r) Output #0, mp3, to 'famous_last_word.32kbit.mp3': Stream #0.0: Audio: mp3, 22050 Hz, mono, 32 kb/s Stream mapping: Stream #0.0 -> #0.0 size= 1045kB time=267.6 bitrate= 32.0kbits/s video:0kB audio:1045kB global headers:0kB muxing overhead 0.000000%
The line in bold indicates that the output audio indeed was at a bitrate of 32kbit/s.
Some other things you can do are - changing the codec of the audio (-acodec option (find all codecs with -formats option)) or cut out a part of the audio (-t and -ss options) you are interested in.
This technique actually involved re-encoding the audio which was already in the movie file. If you read closely the audio option documentation, you will find that the -acodec option says:
`-acodec codec' - Force audio codec to codec. Use the copy special value to specify that the raw codec data must be copied as is.
If the input video file was from YouTube or it already had mp3 audio stream, then using the following command line, the audio will be extracted much, much faster:
c:\> ffmpeg.exe -i My_Chemical_Romance_-_Famous_Last_Words.flv -acodec copy famous_last_words.mp3
Have fun ripping your favorite music off YouTube!
ps. Do you have something cool and useful you would like to accompish but do not have the necessary computer skills? Let me know in the comments and I will see if I can write an article about it!