Show HN: Sed-based YouTube dl
4 pointsby 1019140 comments
Substitute "learnenough.com/command-line" for "conqueringthecommandline.com/book"
Script will work just the same. Go to conqueringthecommandline.com/book
Save the page source
Insert the filename into the script below
Alternatively, omit filename and pipe the source of the page to the script
Works with BSD sed as well as GNU sed; first lesson: portability
a=$(printf '\004')
sed 's/s3_url/'"$a"'&/' \
|tr '\004' '\012' \
|exec sed '
s/\\u0026/\&/g;
s/.*s3_url.:.//g;
s/\".*//g;
/https:.*Amz/!d;
' filename
I do not recommend this "book". Look at the hoops they make readers jump through. Try grymoire.com; much better and no Javascript required # requirements:
# sh, sed (BSD ok), tr, openssl, ftp or some other httpclient
curl=ftp
file=${2-1.mp4} # default outfile
# itag #s are on the wikipedia page for youtube
case $# in
[12])
{
sed 's/http:/https:/' \
|while read a;
do
b=${a#*://};
c=${b%%/*};
{
printf "%b" "GET /${a#https://*/} HTTP/1.0\r\n";
printf "Host: $c\r\n";
printf "User-Agent: OK, Google.\r\n";
printf "Connection: close\r\n\r\n";
} \
|openssl s_client -ign_eof -connect $c:443 -verify 9
done \
|sed '
s,http,\
&,g;
' \
|sed '
/%3A%2F/!d;
/videoplayback/!d;
s,%3D,=,g;
s,%3A,:,g;
s,%2F,/,g;
s,%3F,?,g;
s/^M//g;
' \
|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 \
|sed '
s,%26,\
,g;
s,&,\
,g;
' \
|sed 's/%25/%/g' \
|tr '\012' '&' \
|sed 's/&$//';
echo
} \
|sed '
s,%3D,=,g;
s,%3A,:,g;
s,%2F,/,g;
s,%3F,?,g;
s/^M//g;
' \
|sed '
s/&https/\
\
https/g;' \
|sed 's/\\u0026.*//' \
|sed '/itag='"${1-.}"'/!d;'|{
read a;
exec $curl -4o $file $a;}
;;
*)
exec echo \
"usage: $0 itagno [outfile]
outfile: $file"
esac a=$(printf '\xb%-47.s' \
|nc -uw1 ntp.metas.ch 123 \
|exec xxd -s40 -l4 -p)
b=$(printf %d\\n 0x"$a")
c=$((b-2208988800))
date -r$c
There is a way to do the xxd step using original netcat.
Included with nc was a short program called nc-data.
It converts from btoa and atob for shoveling data to nc. # proof of concept: video retrieval
# requirements:
# sh, sed, tr, openssl, ftp
# Adobe Flash not required
# HTML5 not required
# Python not required
# Awk not required
# web browser not required
curl=ftp
file=1.mp4 # default outfile
url=www.youtube.com # example
# itag #s are on the wikipedia page for youtube
f061(){
sed '
s,%3D,=,g;
s,%3A,:,g;
s,%2F,/,g;
s,%3F,?,g;
s/
//g;
'
} f060(){
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
}
f062(){
sed '
s,http,\
&,g'
}
f063(){
sed '
/%3A%2F/!d;
/videoplayback/!d'
}
f064(){
sed '
s,%26,\
,g;
s,&,\
,g;
'
}
f065(){
sed 's/&https/\
\
https/g;'
}
f066(){
sed 's/\\u0026.*//'
}
f067(){
sed '/itag='"${1-.}"'/!d;'
}
f068(){
sed 's/%25/%/g'
}
f069(){
tr '\012' '&'
}
f070(){
sed 's/&$//'; echo
}
f071(){
local a061 a062 a063;
while read a; do
case $a in
https://*)a061=${a#https://*/} ;;
http://*)a061=${a#http://*/} ;;
*)a061=${a#*/} ;;
esac;
a062=${a#*://};
a063=${a062%%/*};
printf "%b" "${1-GET} /${a061} HTTP/1.0\r\n"
printf "Host: ${a063}\r\n";
printf "User-Agent: GoogleAnalytics 1.5.1\r\n";
printf "Connection: Close\r\n";
printf "\r\n";
done;
}
f072(){
openssl s_client -ign_eof -connect $1:${2-443} -verify 9
}
case $# in
[12])
{
f071 \
|f072 $url \
|f062 \
|f063 \
|f061 \
|f060 \
|f064 \
|f068 \
|f069 \
|f070
} \
|f061 \
|f065 \
|f066 \
|f067 $1 \
|{
read a;
exec $curl -4o ${2-$file} $a ;
}
;;
*)
exec echo \
"usage: $0 itagno [outfile]
outfile: $file"
esac