Comment goes here
You should log in and post some comments! The link is up there in the toolbar. Go nuts!
 

Converting YouTube to MP3 in Linux

« Back to X org / Desktop Articles

Everyone's been in this situation at least one time. You find a song on YouTube love, want to download it to keep it for future uses only to find there's no real way. There's those websites that convert videos for you, but out of the few that actually work they place restrictions on you too. Plus, what's the fun in letting someone else do the work for you? I mean, we are using Linux after all.

Here in this article I'll show you how to do this yourself using only two programs: youtube-dl and ffmpeg. While I know youtube-dl can convert to MP3, I prefer to use ffmpeg...plus, I don't think youtube-dl can do it on its own anyways, so we'll do it the long and painful way, with a script to automate the process in the end.

Software Required
As said, we only need two pieces of software to do this: youtube-dl and ffmpeg. Youtube-dl can be found at http://rg3.github.com/youtube-dl/ while ffmpeg can be found at http://ffmpeg.org/. You can also find these in your repo most likely (ffmpeg is a pretty popular multimedia library).

Getting the YouTube Video
Since we're going for audio-only, and I want to make this article kid-friendly, we'll use this dubstep song: http://www.youtube.com/watch?v=30amRuYU7dw

The first thing we're going to discuss about this program here is the format of the video we are going to download (see "-f" switch in the man page). The available options for YouTube videos are:
WebM video at 480p: 43
WebM video at 720p: 45
H264 video in MP4 container at 480p: 18
H264 video in MP4 container at 720p: 22
H264 video in MP4 container at 1080p: 37
H264 video in FLV container at 360p: 34
H264 video in FLV container at 480p: 35
H263 video at 240p: 5
3GP video: 17


Personally I'm a fan of 720p, and while ultimately it doesn't matter for audio (the quality is going to be the same regardless), if you want to keep the video downloaded for future use may as well make it a format you enjoy.

Next, we'll want to keep it fancy-ish looking so why not use the title of the video as the filename? For that we'll be using the "-t" switch. The only caveat to this is that it includes the video ID in the name as well...and since I'm not a fan of hacking of sed lines all that well, I'll leave that up to you for homework.

Time To Download
Running the youtube-dl command is pretty straight forward, with the output looking like this:
$ youtube-dl -f 22 -t http://www.youtube.com/watch?v=30amRuYU7dw
[youtube] Setting language
[youtube] 30amRuYU7dw: Downloading video webpage
[youtube] 30amRuYU7dw: Downloading video info webpage
[youtube] 30amRuYU7dw: Extracting video information
[download] Destination: X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4
[download] 100.0% of 29.51M at 853.20k/s ETA 00:00
$


The "Destination" line will tell you the name of your video file.

Time To Convert
For ffmpeg we are going to get a little more advanced. We'll be using the "-i" switch for the input file (the above .mp4 destination file). Then we tell ffmpeg we ant an MP3 file with "-f mp3". Since 192 bitrate is about good for most people (which you can change if you want), we'll use an adjustable bitrate of 192k (-ab 192000). We don't want to record any video so we use "-vn" (ffmpeg's switch to disable video recording), and then give the output filename.

Side note If you're using Ubuntu (or a flavor of it), you'll need to install libavcodec-extra-53 to get MP3 support. You can use ogg if you want (should be built into ffmpeg), but I'm not a fan of the format personally.

The output of ffmpeg is big, so I'm going to truncate it down to the most important parts:
ffmpeg -i X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4 -f mp3 -ab 192000 -vn X_Sentinel_The_Unmaker_Dance_Electro.mp3
...
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2011-10-19 18:20:28
Duration: 00:03:30.00, start: 0.000000, bitrate: 1178 kb/s
Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720, 1020 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 151 kb/s
Metadata:
creation_time : 2011-10-19 18:20:28
Output #0, mp3, to 'X_Sentinel_The_Unmaker_Dance_Electro.mp3':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
TDEN : 2011-10-19 18:20:28
TSSE : Lavf53.3.0
Stream #0.0(und): Audio: libmp3lame, 44100 Hz, stereo, s16, 192 kb/s
Metadata:
creation_time : 2011-10-19 18:20:28
Stream mapping:
Stream #0.1 -> #0.0
Press ctrl-c to stop encoding
size= 4922kB time=210.00 bitrate= 192.0kbits/s
video:0kB audio:4922kB global headers:0kB muxing overhead 0.003095%
$



Script
I started you off here on how to strip out the video ID and such.

#!/bin/sh

ID="$1"

if [ -z "$ID" ]; then
echo "$0 <YouTube video ID>"
echo "tExample: $0 5V5V5V-4V"

exit 1
fi

YT="http://www.youtube.com/watch?v=$ID"

MP4=`youtube-dl -f 22 -t $YT | grep "Destination:" | awk '{print $3}'`
#MP3=`echo "$MP4" | sed 's/.{$ID}//g'`

ffmpeg -i "$MP4" -f mp3 -ab 192000 -vn "$MP4.mp3"

echo "YouTube Link: $YT"
echo "Downloaded video to file: $MP4"
#echo "MP3 file: $MP3"


Outro
I'll be writing up some more security articles soon. I wanted to pass this out there though for a breath of fresh air, and because I got annoyed with all of those converter websites that didn't do anything.