opengeodata.de

Convert youtube to audio

2013-04-20

So, you want to archive all that cool music these crazy people put on youtube? Be my guest. :]

First of all: check yourself before you wreck yourself. I will definitely not do this but some people trick you with code snippets. One can easily put some hidden characters via CSS into innocent commands that may look ok in the browser but do terrible stuff in your console. So before hitting the big enter button, read the code you are going to input into your terminal.

But let’s get to the fun. You need to get youtube-dl (readme); so on Ubuntu you may type:

sudo apt-get install youtube-dl

Chances are good that youtube changed its API mechanisms since the last repo update, so run the internal update function. You need root privileges since the update wants to alter some stuff in /usr/bin/youtube-dl:

sudo youtube-dl -U

Youtube-dl should work now just perfect. In my case youtube-dl needs to do some batch stuff. I don’t exactly want this (this gets one video and saves it to your hard drive):

youtube-dl 7yJMLArxPaA

I want this:

youtube-dl -a batch.txt

So put all these nice videos respectively their video ID in a text file and run the command like above. The program will fetch one video after another and do some processing (if indicated). Now, you may want to play around with the options of youtube-dl (see readme). For example the -t option will use the title as filename; very handy. But we are only halfway there because what you have now is either flv or mp4 which contains the audio and video track. youtube-dl has an internal audio converter which relies on ffmpeg, so you can easily go like this:

youtube-dl -a batch.txt -t -x --audio-format "wav"

This will get the IDs in batch.txt, fetch the title and put it as filename and directly convert the videos to wav. For mp3-conversion you will have to install the MP3-support for ffmpeg:

sudo apt-get install ffmpeg libavcodec-extra-53

The following command will give high quality mp3-files (which is by the way kind of unnecessary since you’ll never get perfect quality on youtube; expect some stuff that sounds good on your mobile audioplayer and home system, but terrible on anything close to Hi-Fi; so you may want to save some disk space by setting the quality around 4 or 5-ish):

youtube-dl -a batch.txt -t -x --audio-format "mp3" --audio-quality 0

But mp3 isn’t cool. You know what’s cool? 1 Billi… err, ogg/vorbis, I mean. ;] So just enter something like this and get some patent troll-free musical goodness.

youtube-dl -a batch.txt -t -x --audio-format "vorbis" --audio-quality 8

Be aware, the quality parameter is reversed for ogg/vorbis - 0 is low quality, 9 is high quality. Furthermore, you may want to check out hardware with ogg support to not rely on mp3 patents (I once had a SanDisk player). If you have any questions, feel free to comment.

Edit: To enhance your “productivity” you may install a clipboard manager like parcellite (or any other), copy the URI of the desired videos, find the history file (in case of parcellite it resides here: ~/.local/share/parcellite/history; same goes for glipper), clean that file up a bit and use it as your batch.txt - cool, eh?

Edit 2: Be careful with those playlist parameters (&list=xyz); youtube-dl tries to fetch the whole playlist. Playlists with hundreds or thousands of videos aren’t unusual, so better limit the number of items to be fetched:

youtube-dl yourURI --playlist-end NUMBER 

Also, when messing around with playlists make sure to set the -i parameter. Youtube-dl will ignore errors like “Sorry, not available in your country”.