Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Last active July 20, 2022 08:42
Show Gist options
  • Save oliveiraev/2992029 to your computer and use it in GitHub Desktop.
Save oliveiraev/2992029 to your computer and use it in GitHub Desktop.
Download youtube videos
#!/bin/bash
decode() {
to_decode='s:%([0-9A-Fa-f][0-9A-Fa-f]):\\x\1:g'
printf "%b" `echo $1 | sed 's:&:\n:g' | grep "^$2" | cut -f2 -d'=' | sed -r $to_decode`
}
data=`wget http://www.youtube.com/get_video_info?video_id=$1\&hl=pt_BR -q -O-`
url_encoded_fmt_stream_map=`decode $data 'url_encoded_fmt_stream_map' | cut -f1 -d','`
signature=`decode $url_encoded_fmt_stream_map 'sig'`
url=`decode $url_encoded_fmt_stream_map 'url'`
test $2 && name=$2 || name=`decode $data 'title' | sed 's:+: :g;s:/:-:g'`
test "$name" = "-" && name=/dev/stdout || name="$name.vid"
wget "${url}&signature=${signature}" -O "$name"
#!/usr/bin/env /bin/bash
function youtube-video-url {
local field=
local data=
local split="s:&:\n:g"
local decode_str='s:%([0-9A-Fa-f][0-9A-Fa-f]):\\x\1:g'
local yt_url="http://www.youtube.com/get_video_info?video_id=$1"
local grabber=`command -v curl`
local args="-sL"
if [ ! "$grabber" ]; then
grabber=`command -v wget`
args="-qO-"
fi
if [ ! "$grabber" ]; then
echo 'No downloader available.' >&2
test x"${BASH_SOURCE[0]}" = x"$0" && exit 1 || return 1
fi
function decode {
data="`echo $1`"
field="$2"
if [ ! "$field" ]; then
field="$1"
data="`cat /dev/stdin`"
fi
data=`echo $data | sed $split | grep "^$field" | cut -f2 -d'=' | sed -r $decode_str`
printf "%b" $data
}
local map=`$grabber $args $yt_url | decode 'url_encoded_fmt_stream_map' | cut -f1 -d','`
echo `decode $map 'url'`\&signature=`decode $map 'sig'`
}
[ $SHLVL != 1 ] && export -f youtube-video-url
@oliveiraev
Copy link
Author

usage '$ ./youtube-downloader {video_id}'
output: {video_title}.vid

@oliveiraev
Copy link
Author

Corrected to work with the signed mode.

Note: It'll always download the highest quality available.

@oliveiraev
Copy link
Author

Added ability to choose output name youtube-downloader.sh ${YOUTUBE_ID} ${OUTPUT_NAME}.vid and even toss out to stdout youtube-downloader.sh ${YOUTUBE_ID} -

@oliveiraev
Copy link
Author

Created youtube-video-url. To be sourced insides shells

% source ./youtube-video-url.sh
% youtube-video-url ${YOUTUBE_ID}
http://...

% wget `youtube-video-url ${YOUTUBE_ID}` -bO my-video.vid

% curl `youtube-video-url ${YOUTUBE_ID}` -Ls | mplayer -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment