Friday, 29 October 2010

Youtube videos to MP3 (or mpeg)

Today I wanted to download the music of a YouTube video, in order to show it to some friends off-line. It turns out that this is quite easy (for reference, this is using Ubuntu 10.04 LTS). (Information consulted: http://hubpages.com/hub/Youtube-to-MP3-on-Ubuntu-Linux and http://lookherefirst.wordpress.com/2007/12/15/how-to-convert-flv-files-to-mpg-or-mp3/).

The first step is to get the aptly named script youtube-dl. The version available in Ubuntu 10.04 is 2010.04.04, but this does not work properly anymore, and it gives the following error when trying to download a video:

angelv~$ youtube-dl http://www.youtube.com/watch?v=BTwhEPiv0U8
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
ERROR: format not available for video
angelv~$


If we download the latest version (2010.10.24) from http://bitbucket.org/rg3/youtube-dl/wiki/Home, and just copy the file to /usr/bin/youtube-dl, then we have no trouble and the video (in .flv format) is saved:

angelv~$ youtube-dl http://www.youtube.com/watch?v=BTwhEPiv0U8
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video webpage
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
[download] Destination: BTwhEPiv0U8.flv
[download] 100.0% of 7.73M at   59.74k/s ETA 00:00

angelv~$ ls -lt *flv
-rw-r--r-- 1 angelv dialout 8100631 2010-10-29 10:01 BTwhEPiv0U8.flv
angelv~$


Then, in order to convert it to an MP3 you will need ffmpeg and the libmp3lame0 library (and in Ubuntu 10.04, perhaps also in others, the package libavcodec-unstripped-52, so that ffmpeg cand find libmp3lame). With this in place, a small script will take care of everything:

#!/bin/bash
x=$RANDOM.flv
youtube-dl --output=$x "$1"
ffmpeg -i $x -ar 44100 -ab 160k -ac 2 "$2"
rm $x


As an example:

angelv~/temp$ yt2mp3 http://www.youtube.com/watch?v=BTwhEPiv0U8 chiclana.mp3
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video webpage
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
[download] Destination: 17660.flv
[download] 100.0% of 7.73M at   59.97k/s ETA 00:00
FFmpeg version SVN-r0.5.1-4:0.5.1-1ubuntu1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.1-1ubuntu1 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --e\
nable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc \
--enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Mar  4 2010 12:41:55, gcc: 4.4.3

Seems stream 0 codec frame rate differs from container frame rate: 49.99 (4999/100) -> 25.00 (25/1)
Input #0, flv, from '17660.flv':
  Duration: 00:03:24.44, start: 0.000000, bitrate: 253 kb/s
    Stream #0.0: Video: h264, yuv420p, 320x240 [PAR 1:1 DAR 4:3], 253 kb/s, 25 tbr, 1k tbn, 49.99 tbc
    Stream #0.1: Audio: aac, 22050 Hz, stereo, s16
Output #0, mp3, to 'chiclana.mp3':
    Stream #0.0: Audio: libmp3lame, 44100 Hz, stereo, s16, 160 kb/s
Stream mapping:
  Stream #0.1 > #0.0
Press [q] to stop encoding
size=    4003kB time=204.93 bitrate= 160.0kbits/s
video:0kB audio:4003kB global headers:0kB muxing overhead 0.000781%
 

angelv~/temp$ ls -lt *mp3
-rw-r--r-- 1 angelv dialout 4098644 2010-10-29 10:34 chiclana.mp3
angelv~/temp$ 


Despite its name, we can also use the script as-is to convert Youtube videos for example to mpeg format:

angelv@vaso:~$ yt2mp3 http://www.youtube.com/watch?v=McdD9Ng4VnY funny.mpg


UPDATE (10-Apr-2013): With Ubuntu 12.10, youtube-dl works perfect right now to download YouTube videos, and it even has an option to extract automatically the audio, so what I was achieving with this post is now a no-brainer:


angelv@pilas:~$ youtube-dl --extract-audio --audio-format=mp3 http://www.youtube.com/watch?v=suTKOmmoBtw
[youtube] Setting language
[youtube] suTKOmmoBtw: Downloading video webpage
[youtube] suTKOmmoBtw: Downloading video info webpage
[youtube] suTKOmmoBtw: Extracting video information
[download] Destination: suTKOmmoBtw.flv
[download] 100.0% of 13.89M at   80.60k/s ETA 00:00
[avconv] Destination: suTKOmmoBtw.mp3
angelv@pilas:~$ 


3 comments:

jack said...

Now we can convert YouTube To Mp3 format without any downloader.

kiraniluri said...

really good piece of information, I had come to know about your site from my friend shubodh, kolkatta,i have read atleast nine posts of yours by now, and let me tell you, your site gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a lot once again, Regards, youtube to mp3

angelv said...

Thanks kiraniluri. The information here is mostly for myself, of stuff that I think it will be useful in the future, but I post it here in case it can be used by others as well. So I'm really happy that you find it useful.
Cheers,Ángel