Ledona Tech

March 23, 2010

CLI Converting YouTube video to audio/mp3s

Filed under: apple,bash,linux,ubuntu — ledona @ 1:24 pm

Problem:

I recently (like yesterday) wanted to convert some YouTube videos to audio tracks that I could listen to on my iPod.  A quick google search gives tons of possible solutions, some like vixy.net take a URL and convert it in the cloud and give you a link for download.  This would work fine if I had only a couple videos and they were kinda short.  However I wanted to convert a bunch of videos each of which were over an hour in length.  So I decided to search for a command-line solution so I could write a shell script to do the conversions.

Solution:

So, basically I needed to download the YouTube video, and then convert that video to mp3.  For the first step there is a script called youtube-dl.  It downloads YouTube videos and saves them as flash video files.  It works PERFECTLY.  For converting flash video files to mp3s I used ffmpeg.  Again, it works PERFECTLY!

I did my Youtube downloading on Ubuntu where there is a youtube-dl package, and I did my ffmpeg audio converting on a mac where there is a macports package available.  But both tools are cross platform and should work on whatever you have.

Usage:

Once both tools are installed, the following is all you need to do to first download from youtube and then convert to mp3.  Lets say the link to the youtube video is http://www.youtube.com/watch?v=aFIPeFvPICs , and you want an mp3 file named audio.mp3 .  All you need do is on your command line (or in your shell script) run the following:

> youtube-dl -o video.flv http://www.youtube.com/watch?v=aFIPeFvPICs
> ffmpeg -i video.flv -ar 44100 audio.mp3

Both tools have a bunch of options.  The only additional options that I had to use were to include a username and password for Youtube because they were required to download the video I wanted.  In that case you run youtube-dl as follows:

> youtube-dl -u USERNAME -p PASSWORD -o video.flv http://www.youtube.com/watch?v=aFIPeFvPICs

2 Comments »

  1. Isn’t it easier to use a site like http://www.youtube-mp3.org/ ? There you only have one step: paste your url :-)

    Comment by ludac — March 24, 2010 @ 12:40 pm

  2. ludac,

    Yes, it is much easier if you are doing short videos or if you only have a couple to convert. My problem was that I had a bunch (20+) videos to convert. And each video was about an hour in length. My experience is that websites like youtube-mp2 take approximately half the video play time to do the conversion. This (I’m guessing) is because it takes that long to download the video from youtube.

    If there is a site where I can paste 10 or 20 Youtube URLs in and eventually get all of their associated MP3s that would be ideal. I couldn’t find that so this seemed like the best solution.

    Comment by ledona — March 24, 2010 @ 1:06 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress