Comment

Comment by 101914

Remarkably, YouTube makes scripting downloads very easy. The script below needs only sed and some http client and it has worked for years. I have only had to change it once when there was a change at YouTube; the change was very small.

   # this script uses sh, sed, awk, tr and some http client
   # here, some http client = tnftp
   # awk and tr are optional
   
   
   # wrapper for tnftp to accept urls from stdin
   ftp1(){
   while read a;do 
   ftp ${@--4vdo-} "$a" 
   done;}
   
   
   # uniq
   awk1(){ awk '!($0 in a){a[$0];print}' ;}
   
   
   # some url decoding
   f1(){
   sed '
   s,%3D,=,g;
   s,%3A,:,g;
   s,%2F,/,g;
   s,%3F,?,g;
   s/^M      
   //g;
   #  ^ thats Ctrl-V then Ctrl-M in vi   
   ' 
   }
   
   # remove redundant itags
   f0(){
   sed -e '
   s/&itag=5//;t1
   s/&itag=1[78]//;t1
   s/&itag=22//;t1
   s/&itag=3[4-8]//;t1
   s/&itag=4[3-6]//;t1
   s/&itag=1[346][0-9]//;t1
   ' -e :1
   }
   
   # separate urls 
   f2(){
   sed '
   s,http,\
   &,g' 
   }
   
   # remove unneeded lines
   f3(){
   sed '
   #/^http%3A%2F.*c.youtube.com/!d;
   /^http%3A%2F.*googlevideo.com/!d;
   /crossdomain.xml/d;
   s/%25/%/g;
   s,sig=,\&signature=,;
   s,\\u0026,\&,g;
   /&author=.*/d;
   ' 
   }
   
   
   
   # separate cgi arguments for debugging
   f4(){
   sed '
   s,%26,\
   ,g;
   s,&,\
   ,g;
   ' 
   }
   
   # remove more unneeded lines
   f5(){
   sed '
   /./!d;
   /quality=/d;
   /type=/d;
   /fallback_host=/d;
   /url=/d;
   /^http:/!s/^/\&/
   /^[^h].*:/d;
   /^http:.*doubleclick.net/d;
   /itag.*,/d;
   '
   }
   
   # print urls 
   f6(){
   sed 's/^http:/\
   &/' | tr -d '\012' \
   |sed '
   s/http:/\
   &/g;
   ' 
   }
   
   f8(){
   sed 's/https:/http:/'
   }
   
   FTPUSERAGENT="like OSX"
   
   case $# in
   0) 
   echo|$0 -h 
    ;;
   [12345])
   case $1 in
   
   -h|--h)
   echo "url=http[s]://www.youtube.com/watch?v=..........."
   echo usage1: echo url\|$0 -F \(get itag-no\'s\)
   echo usage2: echo url\|$0 -g \(get download urls\)
   echo usage3: echo url\|$0 -fitag-no -4o video-file
   echo N.B. no space permitted after -f
   
    ;;
   -F)
   $0 -g \
   |tr '&' '\012' \
   |sed '
   /,/d;
   /itag=[0-9]/!d;
   s/itag=//;
   /^17$/s/$/ 3GP/;
   /^36$/s/$/ 3GP/;
   /^[56]$/s/$/ FLV/;
   /^3[45]$/s/$/ FLV/;
   /^18$/s/$/ MP4/;
   /^22$/s/$/ MP4/;
   /^3[78]$/s/$/ MP4/;
   /^8[2-5]$/s/$/ MP4/;
   s/.*?//;
   '|awk1
    ;;

   -g)
   while read a;do
   n=1
   while [ $n -le 10 ];do
   echo $a|f8|ftp1||
   echo $a|f8|ftp1 &&
   break
   n=$((n+1))
   done \
   |f2|f3|f1|f0|f4|f5|f6|f1|sed '/itag='"$2"'/!d'
   done
    ;;

   -f*)
   while read a;do
   n=1
   while [ $n -le 10 ];do
   echo $a|$0 -g ${1#-f} |ftp1 $2 $3 $4 $5 ||
   echo $a|$0 -g ${1#-f} |ftp1 $2 $3 $4 $5  && 
   break
   n=$((n+1))
   done
   done
    ;;

   esac
   esac

There are separate scripts for extracting www.youtube.com/watch?v=........... urls from web pages to feed to this script.
parent 101914 12 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton