Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

oliveiraev commented Jun 25, 2012

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

@oliveiraev

This comment has been minimized.

Copy link
Owner Author

oliveiraev commented Nov 7, 2012

Corrected to work with the signed mode.

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

@oliveiraev

This comment has been minimized.

Copy link
Owner Author

oliveiraev commented Mar 21, 2013

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

This comment has been minimized.

Copy link
Owner Author

oliveiraev commented Mar 22, 2013

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
You can’t perform that action at this time.