Saturday, August 19, 2006

Bash Script for downloading and converting .flv files from YouTube to Mpeg

Make sure you have ffmpeg installed to be able to convert your .flv files to .mpg

Heres the script,

[root@localhost youtube]# cat yt.sh
#!/bin/bash

BASEURL="http://youtube.com/get_video.php?" ; read -p "YouTube URL? " ORIGURL ; read -p "Desired path/filename? " OUTFILE

wget -c -S -O ${OUTFILE}.flv ${BASEURL}`curl -s ${ORIGURL} | grep player2.swf | cut -f2 -d? | cut -f1 -d\"` && ffmpeg -i ${OUTFILE}.flv -ab 56 -ar 22050 -b 500 -s 320x240 ${OUTFILE}.mpg && exit 0
[root@localhost youtube]#

Heres the Output,

[root@localhost youtube]# sh yt.sh
YouTube URL? http://www.youtube.com/watch?v=hNa32mgaM-4
Desired path/filename? /ram/youtube/b
--02:14:55-- http://youtube.com/get_video.php?video_id=hNa32mgaM-4&l=27&t=OEgsToPD
skIPFtZ85feiZLVqeNUmcCat&nc=16763904
=> `/ram/youtube/b.flv'
Resolving youtube.com... 208.65.153.242, 208.65.153.245, 208.65.153.240, ...
Connecting to youtube.com|208.65.153.242|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 303 See Other
Date: Fri, 18 Aug 2006 20:22:36 GMT
Server: Apache
Set-Cookie: VISITOR_INFO1_LIVE=Cz8iYphwJaw; path=/; domain=.youtube.com; expires=Mon, 15-Aug-2016 20:22:36 GMT
Cache-Control: no-cache
Location: http://v47.youtube.com/get_video?video_id=hNa32mgaM-4
Connection: close
Content-Type: text/html; charset=utf-8
Location: http://v47.youtube.com/get_video?video_id=hNa32mgaM-4 [following]
--02:14:56-- http://v47.youtube.com/get_video?video_id=hNa32mgaM-4
=> `/ram/youtube/b.flv'
Resolving v47.youtube.com... 64.34.180.87
Connecting to v47.youtube.com|64.34.180.87|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.0 200 OK
Connection: close
Content-Length: 1139071
Last-Modified: Sun, 18 Dec 2005 03:59:00 GMT
ETag: 11617f-43a4de84
Cache-Control: no-cache
Content-Type: video/flv
Date: Fri, 18 Aug 2006 20:23:31 GMT
Server: lighttpd/1.4.8
Length: 1,139,071 (1.1M) [video/flv]

100%[====================================>] 1,139,071 11.63K/s ETA 00:00

02:16:35 (11.36 KB/s) - `/ram/youtube/b.flv' saved [1139071/1139071]

ffmpeg version 0.4.9-pre1, build 4718, Copyright (c) 2000-2004 Fabrice Bellard
built on Dec 16 2005 05:33:18, gcc: 4.0.1 (4.0.1-5mdk for Mandriva Linux release 2006.0)
Input #0, flv, from '/ram/youtube/b.flv':
Duration: N/A, bitrate: N/A
Stream #0.0: Audio: mp3, 22050 Hz, mono
Stream #0.1: Video: flv, 320x240, 6.00 fps
Output #0, mpeg, to '/ram/youtube/b.mpg':
Stream #0.0: Video: mpeg1video, 320x240, 5.00 fps, q=2-31, 500 kb/s
Stream #0.1: Audio: mp2, 22050 Hz, mono, 56 kb/s
Stream mapping:
Stream #0.1 -> #0.0
Stream #0.0 -> #0.1