Skip to content

Instantly share code, notes, and snippets.

@fuba
Created May 19, 2011 17:13
Show Gist options
  • Save fuba/981256 to your computer and use it in GitHub Desktop.
Save fuba/981256 to your computer and use it in GitHub Desktop.
ustream download library
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Encode;
use File::Slurp;
use URI::Escape qw(uri_unescape);
use LWP::Simple;
sub mp4_download {
my ($html, $dir) = @_;
my $url;
if ($html =~ m|ustream\.vars\.liveHttpUrl\=\"([^\"]+)\"|im) { #"
$url = $1;
$url =~ s|\\/|\/|g;
}
else {
return;
}
my $title;
if ($html =~ m|<h2 id=\"VideoTitle\">([^<]+)</h2>|im) { #"
$title = $1;
$title =~ s/\////g;
$title =~ s/\:/:/g;
$title =~ s/\"/”/g;
$title =~ s/\'/’/g;
$title =~ s/\s/ /g;
}
die if ($title =~ /mala/);
my $filename = $dir.'/'.$title.'.'.'mp4';
warn $url;
mirror($url, $filename);
return ($filename, $title);
}
sub ustream_download {
my ($ustream_url, $dir, $stop) = @_;
my $flashver = '10,0,42,34';
mkpath $dir unless (-e $dir);
my ($amf_url, $title) = get_amf_data($ustream_url);
return unless ($amf_url);
if ($amf_url eq 'mp4') {
return mp4_download($title, $dir);
}
$title = ($title =~ /\%/) ? uri_unescape(encode_utf8($title)) : $title;
my $file = $dir.'/'.($title || 'stream').'_'.time.'.flv';
{
my $unit = 60;
if ($stop =~ /秒/) {
$unit = 1;
}
elsif ($stop =~ /分/) {
$unit = 60;
}
elsif ($stop =~ /時間/) {
$unit = 3600;
}
$stop =~ tr|0123456789|0123456789|;
my $time;
if ($stop =~ /(\d+)/) {
$time = $1;
$stop = $time;
}
else {
$stop = 5;
}
$stop ||= 10;
$stop *= $unit;
if ($stop > 3600) {
$stop = 3600;
}
};
my ($video_url, $video_id, $video_id_raw) = get_video_url($amf_url);
if ($video_url) {
my $command;
if ($video_url =~ /\@/) {
$command = 'rtmpdump -vr "'.$video_url.'" -W http://cdn1.ustream.tv/swf/4/viewer.296.swf --stop '.$stop.' -o "'.$file.'"';
}
else {
$video_id_raw =~ s/^.+\@//;
my $video_name = ($video_url =~ /edgefcs/)
? '"'.$video_url.'/ustream@'.$video_id_raw.'.flv"'
: '"'.$video_url.'" -a "ustreamVideo/'.$video_id.'"';
$command = 'rtmpdump -q -v -r '.$video_name.' -f "LNX '.$flashver.'" -y "streams/live" --stop '.$stop.' -o "'.$file.'"';
}
print $command."\n";
system($command);
$file = decode_utf8($file);
$title = decode_utf8($title);
return ($file, $title);
}
else {
return;
}
}
sub get_video_url {
my $url = shift;
my $amf_bin = get($url);
if ($amf_bin =~ m|(rtmp://[^/]+/ustreamVideo/(\d+))|m) {
warn 'oops';
return ($1, $2);
}
if ($amf_bin =~ m|cdnUrl.+?(rtmp[\w\.\:\/]+)|m) {
my $video_url = $1;
if ($amf_bin =~ m|streamName.+?(ustream[\w\_\@\-]+)|m) {
my $video_id = $1;
my $video_id_ = $video_id;
$video_url .= '/'.$video_id;
$video_id =~ s/\@/_/g;
return ($video_url, $video_id, $video_id_);
}
}
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 {
if ($text =~ /liveHttpUrl/m) {
warn 'recorded';
return ('mp4', $text);
}
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
);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment