Skip to content

Instantly share code, notes, and snippets.

@takano32
Forked from fuba/gist:981256
Created June 18, 2011 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takano32/1032901 to your computer and use it in GitHub Desktop.
Save takano32/1032901 to your computer and use it in GitHub Desktop.
#!/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 = 3600;
}
$stop =~ tr|0123456789|0123456789|;
my $time;
if ($stop =~ /(\d+)/) {
$time = $1;
$stop = $time;
}
else {
$stop = 5;
}
$stop ||= 10;
$stop *= $unit;
if ($stop > 1800) {
$stop = 1800;
}
};
my ($video_url, $video_id) = 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 {
$command = 'rtmpdump -q -v -r "'.$video_url.'" -a "ustreamVideo/'.$video_id.'" -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) {
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;
$video_url .= '/'.$video_id;
$video_id =~ s/\@/_/g;
return ($video_url, $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