Youtube video downloader shell script
Recently I were looking for youtube downloaders over the web. I found many websites filled with lot of ads. Few websites where there which gave some neat interface to download videos. I got interested and decided to write my own shell script to parse and download videos. By looking into HTTP requests, I came through how downloaders work.
The logic is pretty simple. By passing the video_id which is received as v=videoid from youtube video URLs, to? http://www.youtube.com/get_video_info?video_id=videoid, we obtain a metadata file which contain metadata about the video we need to download. We extract a parameter called tokenid. Again pass the video_id and token to the same URL to obtain the video. We can also specify formats in which it is to be downloaded such as mp4,flv or 3gp in different video qualities. fmt=id parameter is passed to specify file format. By carefully watching the HTTP requests from youtube page, I collected variety of fmt arguments for different formats and quality. I have compiled all these info to write a youtube video downloader shell script. Download it and have fun.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
#Description: Youtube video downloader script
#Author: Sarath Lakshman
#url: http://sarathlakshman.com
if [ $# -ne 3 ];
then
echo -e "Usage: $0 URL -format FORMAT\nFormats of different video qualities:\n1080 (mp4) - highest\n720 (mp4) - higher\n360 (flv) - high\n480 (flv) - low\n240 (flv) - lower\n3gp (3gp) - least\n\nEg: $0 http://www.youtube.com//watch?v=yZPSx2r3TiY -format 1080\n"
exit 0
fi
url=$1
declare -A formats;
declare -A extension;
formats[1080]=37;
formats[720]=22;
formats[480]=35;
formats[360]=34;
formats[240]=5;
formats[3gp]=13;
extension[1080]=mp4;
extension[720]=mp4;
extension[480]=flv;
extension[360]=flv;
extension[240]=flv;
extension[3gp]=3gp;
vid=`echo $1 | cut -d= -f2`
wget -O /tmp/meta.data "http://www.youtube.com/get_video_info?video_id=$vid&el=vevo" &> /dev/null
token=`sed 's/.*token=\([^&=]\+\).*/\1/g' /tmp/meta.data
title=`sed 's/.*title=\([a-zA-Z0-9%+-]\+\).*/\1/g; s/#-//g; s/[%+0-9]\+/_/g' /tmp/meta.data
echo "Downloading..."
wget -o /tmp/log.$$ -O "$title.${extension[$3]}" "http://www.youtube.com/get_video?video_id=$vid&t=$token&fmt=${formats[$3]}"
if grep -q "Not Found" /tmp/log.$$ ; then
echo "Unsupported format. Please try again with lower video quality format"
rm $title.${extension[$3]} ;
else
echo Download complete. Play $title.${extension[$3]} and enjoy \).
fi
Download the scrpt from here : youtube_dl.sh
1
2
3
4
5
6
7
8
9
10
11
slynux@slynux-laptop:~/scripts$ ./youtube_dl.sh
Usage: ./youtube_dl.sh URL -format FORMAT
Formats for different video qualities:
1080 (mp4) - highest
720 (mp4) - higher
360 (flv) - high
480 (flv) - low
240 (flv) - lower
3gp (3gp) - least
Eg: ./youtube_dl.sh http://www.youtube.com/watch?v=yZPSx2r3TiY -format 1080