As I’ve mentioned in previous posts, I’m an audiophile. This means the quality of sound is important to me. I prefer to buy music on CDs, or Vinyl, than suffer the lossy compression on MP3s, or other lossy compression formats. But sometimes, the only place you can find something is online.
Lets say, for example, you know someone who’s recorded something, and uploaded it to YouTube. It sounds good online, but of course, re-encoding it for your iPod is going to introduce some quality loss as you’re converting from one lossy format to another! Some people try to work around this by encoding the audio to FLAC (Free Lossless Audio Codec), but this ends up with an unnecessarily large file that has the same quality as the youtube video!
What is needed then, is a way to strip the audio out of the original video, and repack it, without converting the audio itself.
A forewarning: This technique requires use of command line tools, but don’t worry, it’s not as complicated as it sounds!
The first thing I did was to look at the different formats YouTube uses:
Way back in March 2008, a popular YouTuber at the time, Mr Safety (smpfilms) posted a video publicising the &fmt=18 technique, and whilst this doesn’t provide the best definition any more, we can see, from the table above, that MP4 is the default container for this particular format. As most videos are available in this format, this is the one we’re going to exploit!
So firstly, we’re going to need the following tools installed on your computer:
- faad2
- MP4Box (part of GPAC) – Windows binaries available here
- mp4creator
- EasyTag (optional)
The download links are for Windows, for Linux users, these tools are available from your distro’s repository! I’ve tried this on Ubuntu, but the tools should act exactly the same under Windows.
You will also need something to download the YouTube videos in the first place. A plug in for FireFox called DownloadHelper can extract the format we need, but there are a few others.
We first need a video we’re going to extract the sound file from, I’m going to use this one: http://www.youtube.com/watch?v=uyuzwdCt0GQ as it’s royalty free, so I won’t get sued for using it!
As I said before, fmt=18 provides a video in the MP4 format, which crucially, has AAC encoded audio. So what we need to do is add &fmt=18 to the end of the URL of our chosen video: http://www.youtube.com/watch?v=uyuzwdCt0GQ&fmt=18
We then use our downloading plugin to save the video, make sure you’re downloading the version that ends in “.mp4”, I’ve saved mine as “music.mp4”
You then need to open Command Prompt (or a Terminal application if you’re using Linux) and move to the directory you have saved your file in. For example, if you have saved your video in “Documents” on Windows 7, then you’ll need to type into Command Prompt:
cd "C:\users\username\documents"
once you’re in the directory you’ve saved your video, you can make sure you’re in the right place by typing:
dir
or
ls
if you’re on Linux.
Next, we need to extract the raw AAC audio out of the video we’ve just downloaded:
faad -a music.aac music.mp4
This will make a new file which contains the audio only in the AAC file format. This is the raw sound from the video. Some media players (Like VLC) will play this, but there are limitations, as there is no way to add meta data to this file, meaning the Track names can’t be added and the media player can’t easily work out how long the track lasts for!)
To make this file more useful, we need to put it back into an MP4 container, for this, we use MP4Box:
MP4Box -add music.aac music.m4a
This will create another new file called “music.m4a”
Next we should add the Track name and Artist name, as this makes the song easier to find on an iPod.
I used EasyTag to label the new m4a file.
Finally, some players are very sensitive about how the m4a file is laid out, for example, Rockbox won’t play these files in a playlist until the files have been optimised. To do this use mp4creator:
mp4creator -optimize music.m4a
This file can now be imported to iTunes and synced to your iPod
To prove that this technique doesn’t degrade the original music at all, you can extract the audio from both the MP4 and the M4A using FAAD into a Wav file, load them both side by side in audacity, and invert one of the files, when played back, there should to nothing but total silence, as the audio from one file cancels out the audio in the other when inverted.
Cheers