Skip to content

Instantly share code, notes, and snippets.

@fuba
Created October 19, 2010 16:07
Show Gist options
  • Save fuba/634461 to your computer and use it in GitHub Desktop.
Save fuba/634461 to your computer and use it in GitHub Desktop.
download live video from ustream using rtmpdump.
#!/usr/bin/perl
# this script is an implementation of http://textt.net/mapi/20101018201937
use strict;
use warnings;
use LWP::Simple;
my ($ustream_url, $file, $stop) = @ARGV;
my $usage = <<USAGE;
usage: sudo ./ustream_download.pl USTREAM_URL TARGET_DIRECTORY [rtmpdump stop option]
e.g. sudo ./ustream_download.pl http://www.ustream.tv/channel/naf-libe-events 600
USAGE
die $usage unless $ustream_url;
die $usage unless $file;
my ($amf_url, $title) = get_amf_data($ustream_url);
$file .= '/'.($title || 'stream').'_'.time.'.flv';
$stop ||= 600;
my ($video_url, $video_id) = get_video_url($amf_url);
if ($video_url) {
my $command = 'rtmpdump -q -v -r "'.$video_url.'" -a "ustreamVideo/'.$video_id.'" -f "LNX 10,0,45,2" -y "streams/live" --stop '.$stop.' -o "'.$file.'"';
print $command."\n";
system($command);
}
else {
die "no video url";
}
sub get_video_url {
my $url = shift;
my $amf_bin = get($url);
if ($amf_bin =~ m|(rtmp://[^/]+/ustreamVideo/(\d+))|m) {
return ($1, $2);
}
return;
}
sub get_amf_data {
my $url = shift;
my $text = get($url);
my $channel_id;
if ($text =~ /Channel\s+ID\:\s+(\d+)/m) {
$channel_id = $1;
}
else {
return;
}
my $title = $channel_id;
if ($text =~ /property\=\"og\:url\"\s+content\=\"http\:\/\/www.ustream.tv\/channel\/([^\"]+)\"/m) {
$title = $1;
}
return (
'http://cdngw.ustream.tv/Viewer/getStream/1/'.$channel_id.'.amf',
$title
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment