You're viewing a comment by quandary and its responses.

July 09, 2008, 06:55

Why Perl? You can even do it with bash!
And you also get to download the mp4 format and a download o-meter for free.

#!/bin/bash
#
# getvideo.sh
# Copyright (C) 2008 by Quandary
# Contact: http://www.nixcoders.org/forum/index.php?showuser=215
# Released under the GPL 3.O
#
# Download youtube videos with bash and wget;-)
#
# run: chmod +x getvideo.sh
# run:  ./getvideo.sh YOUTUBE_URL
# e.g.  ./getvideo.sh http://www.youtube.com/watch?v=00KYPsQSG10
#
# Last modified: Jul 09, 2008


#youtubeURL="http://www.youtube.com/watch?v=43sUEFcD4Ro" # only has flv
#youtubeURL="http://www.youtube.com/watch?v=00KYPsQSG10" # has mp4 & flv
youtubeURL=$1; # get command line argument 1
youtubeURL=$(echo "$youtubeURL") # argh, damn bash! Remove invisible double quote

reset
echo "Downloading video: $youtubeURL"
echo ""
youtubeHTML=$(wget -O - "$youtubeURL" 2> /dev/null)
#youtubeHTML=$(cat "$(echo ~)/Desktop/teletubbie.htm") # cat it from an existing file

#echo "HTML Page:"
#echo "$youtubeHTML"
#echo "$youtubeHTML" >> youtube.htm # For review, if the keyword changes


#flvURL=$(echo "$youtubeHTML" | grep -o -E 'player2\.swf?[^\"]+' | head -n 1 | sed 's/^player2\.swf?//g')
flvURL=$(echo "$youtubeHTML" | grep 'fullscreenUrl' | sed -re 's/^.+video_id=//;s/&hl=.+//') # sed -re enables regex search syntax extensions
echo "Encrypted URL = $flvURL"
echo ""

VideoTitle=$(echo "$youtubeHTML" | grep "" | sed 's///;s///;s/YouTube - //;s/^\s*//g')
FLVfile=$(echo "$youtubeHTML" | grep "" | sed 's///;s//.flv/;s/YouTube - //;s/^\s*//g')
MP4file=$(echo "$youtubeHTML" | grep "" | sed 's///;s//.mp4/;s/YouTube - //;s/^\s*//g')
#sed 's/^\s*//g' # finally, trim trailing whitespaces
echo "Video title: $VideoTitle"
echo ""

echo "Starting wget: "
FLVdownloadURL="http://www.youtube.com/get_video.php?hl=en&video_id=$flvURL"
MP4downloadURL="http://www.youtube.com/get_video.php?hl=en&video_id=$flvURL&fmt=18"
# append &fmt=6    FLV
# append &fmt=18   MP4
echo "FLV download URL: $FLVdownloadURL"
echo "MP4 download URL: $MP4downloadURL"
echo ""

wget -O "$FLVfile" "$FLVdownloadURL"
wget -O "$MP4file" "$MP4downloadURL"
reset

echo ""
echo "VoilĂ , download of \"$VideoTitle\" completed."

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type first 3 letters of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.