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).
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] 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:
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.