SOL: Extract the mp3 audio from a YouTube (or other) video file

This is part of my Simple One-Liners (SOL) series of useful commands. Today I would like to discuss an easy way, using the command line and the extremely powerful tool ffmpeg to extract the audio track from a YouTube or other video.

First, to actually get the video file from YouTube, I highly recommend the excellent Firefox add-on called Download YouTube Videos as MP4. You will find a number of add-ons available for Firefox which offer to enable you to download YouTube or other videos, however in my experience many of them are add-on spam which at the least try to upsell “features” and at worst may potentially be malicious. Download YouTube Videos as MP4 seems to be totally above-the-board however and is an Open Source project on GitHub.

There is a lot of music available on YouTube – in fact it kind of boggles the mind how much can be found there – but as you know each song is actually a video because that is YouTube’s format (even if the video consists of just one frame).

In order to extract the audio you need to get ffmpeg, the Windows version of which is available here. I recommend the 64-bit static version (use the 32 bit version if you are on a 32-bit system obviously).

I have a special way of setting up ffmpeg on my system. Once the .7z archive is downloaded, I put it in a subdirectory of my home called Apps. I don’t know if I mentioned before the role of the Apps directory in my Personal Information Logos directory schema, but it serves an important role. It is the directory in my home directory where I put what are called portable apps. For example there are a lot of useful little apps that run by themselves such as WinAeroTweaker, Flash Fullscreen Patcher, TCPView, Process Explorer, PatchMyPC, and others, which I put in there. Its highly convenient to keep all portable apps and utilities in there and also I can just copy the directory to any other machine where I want to have all the utilities available.

Then I link the ffmpeg binaries into my ~/bin directory. My Cygwin .bash_profile includes my ~/bin directory in my PATH environment variable. This is basic Unix stuff but essential to know about and extremely useful. All I do is:
cd ~/bin
ln -s ../Apps/ffmpeg-20160531-git-a1953d4-win64-static/bin/ffmpeg.exe .
ln -s ../Apps/ffmpeg-20160531-git-a1953d4-win64-static/bin/ffplay.exe .
ln -s ../Apps/ffmpeg-20160531-git-a1953d4-win64-static/bin/ffproble.exe .

Now these commands will be available to me from the command line in any directory.

Once you have a video file downloaded, you can use ffprobe to check it out. Its a good idea to check the bitrate of the audio track inside the video file. Unfortunately most YouTube videos use relatively low bitrates for their audio tracks. Always download the highest resolution video available to ensure you also get the highest possible bitrate for the audio track.

To extract the audio track you just do:
ffmpeg.exe -i file.mp4 -b 256 file.mp3

This example will encode with a bit rate of 256kbps which is usually higher than most audio tracks in YouTube videos are encoded at (check the output of ffprobe above if you want to be more exact).


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *