aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNigel Taylor <njtaylor0101@gmail.com>2013-04-15 16:12:18 -0700
committerNigel Taylor <njtaylor0101@gmail.com>2013-04-15 16:12:18 -0700
commita84dd286c622bb9819d1619456bbca9158bc1830 (patch)
treeb2c5ac1f59d4b39fe4dc67add41a58117bbae011
parenta38c0c403ac503f2a875d84c901021454d2ef8ee (diff)
parent1e9d3e630ba30c707a27989788ed2aa6b08fdc4e (diff)
downloadget-flash-videos-a84dd286c622bb9819d1619456bbca9158bc1830.tar.gz
Merge pull request #104 from karjonas/rtmpcheck
Adding a try_download function for RTMPDownloader
-rw-r--r--lib/FlashVideo/RTMPDownloader.pm50
-rw-r--r--lib/FlashVideo/Site/Kanal5play.pm32
2 files changed, 71 insertions, 11 deletions
diff --git a/lib/FlashVideo/RTMPDownloader.pm b/lib/FlashVideo/RTMPDownloader.pm
index 7ab2ec0..cb37540 100644
--- a/lib/FlashVideo/RTMPDownloader.pm
+++ b/lib/FlashVideo/RTMPDownloader.pm
@@ -6,6 +6,8 @@ use base 'FlashVideo::Downloader';
use IPC::Open3;
use Fcntl ();
use Symbol qw(gensym);
+use File::Temp qw(tempfile tempdir);
+use Storable qw(dclone);
use FlashVideo::Utils;
use constant LATEST_RTMPDUMP => 2.2;
@@ -91,6 +93,54 @@ sub download {
return -s $file;
}
+# Check if a stream is active by downloading a sample
+sub try_download {
+
+ my ($self, $rtmp_data_orig) = @_;
+ my $rtmp_data = dclone($rtmp_data_orig);
+
+ # Create a temporary file for the test
+ my ($fh, $filename) = tempfile();
+ $rtmp_data->{flv} = $filename;
+
+ # Just download a second of video
+ $rtmp_data->{stop} = "1";
+
+ if(my $socks = FlashVideo::Mechanize->new->get_socks_proxy) {
+ $rtmp_data->{socks} = $socks;
+ }
+
+ my $prog = $self->get_rtmp_program;
+
+ if($prog eq 'flvstreamer' && ($rtmp_data->{rtmp} =~ /^rtmpe:/ || $rtmp_data->{swfhash})) {
+ error "FLVStreamer does not support "
+ . ($rtmp_data->{swfhash} ? "SWF hashing" : "RTMPE streams")
+ . ", please install rtmpdump.";
+ exit 1;
+ }
+
+ if($self->debug) {
+ $rtmp_data->{verbose} = undef;
+ }
+
+ my($return, @errors) = $self->run($prog, $rtmp_data);
+
+ if($return != 0 && "@errors" =~ /failed to connect/i) {
+ # Try port 443 as an alternative
+ info "Couldn't connect on RTMP port, trying port 443 instead";
+ $rtmp_data->{port} = 443;
+ ($return, @errors) = $self->run($prog, $rtmp_data);
+ }
+
+ # If we got an unrecoverable error return false
+ if($return == 1) {
+ info "\n Tested stream failed.";
+ return 0;
+ }
+
+ return 1;
+}
+
sub get_rtmp_program {
if(is_program_on_path("rtmpdump")) {
return "rtmpdump";
diff --git a/lib/FlashVideo/Site/Kanal5play.pm b/lib/FlashVideo/Site/Kanal5play.pm
index 862c2b9..125a627 100644
--- a/lib/FlashVideo/Site/Kanal5play.pm
+++ b/lib/FlashVideo/Site/Kanal5play.pm
@@ -6,15 +6,9 @@ use warnings;
use FlashVideo::Utils;
use FlashVideo::JSON;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
sub Version() { $VERSION;}
-my $bitrates = {
- low => 250000,
- medium => 450000,
- high => 900000
-};
-
sub find_video {
my ($self, $browser, $embed_url, $prefs) = @_;
if (!($browser->uri->as_string =~ m/video\/([0-9]*)/)) {
@@ -37,20 +31,36 @@ sub find_video {
my $filename = sprintf "%s - S%02dE%02d", $name, $season, $episode;
my $rtmp = "rtmp://fl1.c00608.cdn.qbrick.com:1935/00608";
my $playpath = $json->{streams}[0]->{source};
+ my $max_rate = 0;
+ # Always take the highest bitrate stream
foreach my $stream (@{$json->{streams}}) {
my $rate = int($stream->{bitrate});
- if ($bitrates->{$prefs->{quality}} == $rate) {
+ if ($rate > $max_rate) {
$playpath = $stream->{source};
- last;
+ $max_rate = $rate;
}
}
- return {
+ # Check if the maximum quality stream is available.
+ # The stream is not present in the json object even if it exists,
+ # so we have to try the playpath manually.
+ my $downloader = FlashVideo::RTMPDownloader->new;
+ $playpath =~ m/(.*)_([0-9]*)\Z/;
+ my $playpath_max = $1 . "_1600";
+
+ my $args = {
flv => title_to_filename($filename, "flv"),
rtmp => $rtmp,
- playpath => $playpath,
+ playpath => $playpath_max,
swfVfy => "http://www.kanal5play.se/flash/K5StandardPlayer.swf"
};
+
+ # If the stream was not found we revert the playpath
+ if (!$downloader->try_download($args)) {
+ $args->{playpath} = $playpath;
+ }
+
+ return $args;
}
1;