Skip to content

Instantly share code, notes, and snippets.

@fuba
Created July 13, 2010 18:31
Show Gist options
  • Save fuba/474294 to your computer and use it in GitHub Desktop.
Save fuba/474294 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use MozRepl;
use Net::Pcap::Easy;
my ($ustream_url, $file, $stop, $device) = @ARGV;
my $usage = <<USAGE;
usage: sudo ./ustream_download.pl USTREAM_URL TARGET_DIRECTORY [rtmpdump stop option] [network device]
e.g. sudo ./ustream_download.pl http://www.ustream.tv/channel/dommune ~/Video 600 eth0
This program must be used with Firefox+MozRepl.
USAGE
die $usage unless ($ustream_url && $ustream_url =~ /^http\:\/\/(?:www\.)?ustream\.tv/i);
die $usage unless $file;
$ustream_url =~ /([^\/]+)$/;
$file .= '/'.($1 || 'stream').'.flv';
$stop ||= 600;
$device ||= 'eth0';
my $repl = MozRepl->new;
$repl->setup({
client => {
timeout => 300,
}
});
my $change_url = sub {
my $url = shift;
return <<SCRIPT;
(function(){
window.getBrowser().contentDocument.location.href = "$url";
})();
SCRIPT
};
$repl->execute($change_url->($ustream_url));
my $rtmp_url = '';
my $npe = Net::Pcap::Easy->new(
dev => $device,
filter => "tcp",
packets_per_loop => 10,
bytes_to_capture => 1024,
timeout_in_ms => 60*1000, # 0ms means forever
promiscuous => 0, # true or false
tcp_callback => sub {
my ($npe, $ether, $ip, $tcp) = @_;
if ($ether->{data} =~ m|(rtmp://flash\d+.ustream.tv:\d+/ustreamVideo/\d+)|) {
print $1."\n";
$rtmp_url = $1;
die;
}
},
);
1 while (eval{$npe->loop});
$repl->execute($change_url->('about:blank'));
$rtmp_url =~ m|ustreamVideo/(\d+)|;
my $command = 'rtmpdump -q -v -r "'.$rtmp_url.'" -a "ustreamVideo/'.$1.'" -f "LNX 10,0,45,2" -y "streams/live" --stop '.$stop.' -o "'.$file.'"';
warn $command;
system($command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment