#!/bin/bash
# yt-download.sh v2.35 2013-04-21
# Arif HS http://arif.suparlan.com/2011/02/23/download-youtube-video-dengan-shell-script
# Big thanks to: Trio & Dougal http://www.murga-linux.com/puppy/viewtopic.php?p=326803#326803
# Usage: bash/sh yt-download.sh [youtube_video_id] [save_directory]
#
echo ""
echo "YT-DOWNLOAD.SH v2.35"
echo "----------------------------------"
if [ $2 ]
then
if [ ! -d "$2" ]
then
echo "Can not find directory. Please check [save_directory]"
sleep 1
exit 0
fi
DIR=$2"/"
else
DIR=""
fi
if [ $1 ]
then
#downloading html file
echo "Downloading HTML file and getting video formats, please wait..."
tmpfn=$1-$(date +"%s").tmp
wget -O $tmpfn "http://www.youtube.com/watch?v="$1 -q
#curl -s "http://www.youtube.com/watch?v="$1 > $tmpfn
#get title
title=$(grep -o "" $tmpfn)
title=`expr match "$title" ''`
title=${title//\//"-"}
titlefn=${title//" "/"_"}
#check value
function chkval {
if [ ! $1 ]
then
echo "Uh oh, something went wrong..."
exit 0
fi
}
chkval $titlefn
echo ""
echo "Download site(s) found!"
echo "----------------------------------"
echo "Title: "$title
#get player_config part
#Tmp=$(grep -o "'PLAYER_CONFIG': {.*}" $tmpfn)
Tmp=$(grep -o "ytplayer.config = {.*}" $tmpfn)
declare -a Url_array Geom_array Fmt_array
UrlMap=${Tmp#*\"url_encoded_fmt_stream_map\": \"}
UrlMap=${UrlMap%%\"*}
UrlMap=`echo ${UrlMap//,/ } | tac -s' '`
GeomMap=${Tmp#*\"fmt_list\": \"}
GeomMap=${GeomMap%%\"*}
GeomMap=`echo ${GeomMap//,/ } | tac -s' '`
#fmt_list
#url_encoded_fmt_stream_map
j=0
for i in ${UrlMap} ; do
#Fmt=${i##*=}
Url=${i#*url=}
Url=${Url%%\\u0026*}
Url="$(echo $Url | echo -e $(sed 's/+/ /g; s/%/\\x/g'))"
Url=${Url//\\\///}
Url=${Url//\\\u0026/&}
Url=${Url//\%252C/,}
#get signature
Sig=${i#*sig=}
Sig=${Sig%%\\u0026*}
#Url=${Url%%&type*}
#Url="$(echo $Url | sed -e s/\\http:[^\]]*\\.v/http:\\/\\/v/)"
Url=$Url"&signature="$Sig"&title="$titlefn
#itag
itag=${i#*0026itag=}
itag=${itag%%\\*}
Url_array[$j]=$Url
Fmt_array[$j]=$itag
let j++
done
j=0
for i in ${GeomMap} ; do
Fmt=${i%%\\*}
Geom=${i#*/}
Geom=${Geom%%\\*}
Geom_array[$j]="$Geom"
let j++
done
#format list
fmt[0]="flv"
fmt[5]="flv"
fmt[6]="flv"
fmt[34]="flv"
fmt[35]="flv"
fmt[13]="3gp"
fmt[17]="3gp"
fmt[18]="mp4"
fmt[22]="mp4"
fmt[37]="mp4"
fmt[38]="mp4"
fmt[43]="webm"
fmt[44]="webm"
fmt[45]="webm"
echo "----------------------------------"
# List all the elements in the array.
arr_count=${#Geom_array[@]}
i=0
while [ "$i" -lt "$arr_count" ]
do
echo [$i] ${Geom_array[$i]} ${fmt[${Fmt_array[$i]}]} #${Fmt_array[$i]}
let i++
done
rm $tmpfn
echo -n 'Select video format and press enter or just press enter to abort: '
read infmt
if [ ! "$infmt" ]
then
echo "Aborted."
sleep 1
exit 0
else
#check if input integer
if [ $infmt -ne 0 -o $infmt -eq 0 2>/dev/null ]
then
arr_in=${#Fmt_array[@]}
if [ $infmt -lt $arr_in ] && [ $infmt -ge "0" ]
then
echo "Downloading video..."
echo "----------------------------------"
#set file name
fn=$titlefn.${Geom_array[$infmt]}.${fmt[${Fmt_array[$infmt]}]}
chkval ${Url_array[$infmt]}
wget -O - -t 7 -w 5 --waitretry=14 --random-wait '--user-agent=Mozilla/5.0' -e robots=off "${Url_array[$infmt]}" > "$DIR""$fn"
#echo "File:" $fn ${Url_array[$infmt]}
#rm wget-log*
fi
fi
fi
echo "----------------------------------"
echo ""
else
echo "Usage: bash/sh yt-download.sh [youtube_video_id] [save_directory] "
echo "Example: sh yt-download.sh jNQXAC9IVRw"
exit
fi