Changeset 8
- Timestamp:
- 10/13/06 04:10:47
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plagger/trunk/my-plugins/Filter-FetchEnclosure-Mplayer/Mplayer.pm
r7 r8 22 22 my($self, $context, $args) = @_; 23 23 24 unless (defined $self->conf->{type}) { 25 $context->log(error => q{config 'type' is not set.}); 26 } 27 24 28 for my $enclosure ($args->{entry}->enclosures) { 29 30 unless ($enclosure->type =~ $self->conf->{type}) { 31 $enclosure->local_path('dummy') unless $enclosure->local_path; 32 next; 33 } 25 34 26 my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 35 my $feed_dir 36 = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 27 37 unless (-e $feed_dir && -d _) { 28 38 $context->log(info => "mkdir $feed_dir"); … … 30 40 } 31 41 32 my $path_base = File::Spec->catfile($feed_dir, $args->{entry}->digest); 33 my $path = "$path_base.wav"; 34 my $fetched = $self->conf->{consider_fetched} || 'mp3'; 35 my $extpath = "$path_base." . $fetched; 36 37 if (-e $extpath || -e $path) { 42 my $path_base = File::Spec->catfile($feed_dir, $args->{entry}->digest); 43 my $output_path = "$path_base.wav"; 44 45 # Check existence of fetched data 46 my $fetched_extension = $self->conf->{consider_fetched} || 'mp3'; 47 my $fetched_data_path = "$path_base.$fetched_extension"; 48 if (-e $fetched_data_path || -e $output_path) { 38 49 my $length = -s _; 39 50 $enclosure->length($length); 40 51 $enclosure->type('audio/x-wav'); 41 $enclosure->local_path($ path);42 $context->log(debug => $enclosure->url . "is already stored in $ path");52 $enclosure->local_path($output_path); 53 $context->log(debug => $enclosure->url . "is already stored in $output_path"); 43 54 next; 44 55 } 45 56 46 my $ new_enclosure_url = $self->rewrite_enclosure_url($enclosure->url);57 my $stream_url = $self->_get_stream_url_for($enclosure); 47 58 48 $context->log(info => "fetch " . $new_enclosure_url . " to " . $path);59 $context->log(info => "fetch $stream_url to $output_path"); 49 60 50 61 my @options = split(/ /,$self->conf->{option}) or (); 51 my @command = ('mplayer', $new_enclosure_url, 52 '-vc', 'dummy', 53 '-vo', 'null', 54 '-ao', "pcm:file=$path", 55 '-quiet', 56 @options,); 62 my @command = ( 63 'mplayer', 64 $stream_url, 65 '-vc', 'dummy', 66 '-vo', 'null', 67 '-ao', "pcm:file=$output_path", 68 '-quiet', 69 @options, 70 ); 57 71 58 72 system (@command); 59 73 60 if (!$? && -s $ path ) {74 if (!$? && -s $output_path ) { 61 75 my $length = -s _; 62 $enclosure->url($ new_enclosure_url);76 $enclosure->url($stream_url); 63 77 $enclosure->length($length); 64 78 $enclosure->type('audio/x-wav'); 65 $enclosure->local_path($ path);66 $context->log(info => "Dumping to $ path is done [$length]");79 $enclosure->local_path($output_path); 80 $context->log(info => "Dumping to $output_path is done [$length]"); 67 81 } else { 68 $context->log( error => "Error dumping to $path");69 $context->log(info => "Deleting $ path");70 unlink $ path or $context->log(error => "Cannot delete $path.");82 $context->log(warn => "Can not dump to $output_path"); 83 $context->log(info => "Deleting $output_path"); 84 unlink $output_path or $context->log(warn => "Can not delete $output_path."); 71 85 } 72 86 } 73 87 } 74 88 75 sub rewrite_enclosure_url{76 my ($self, $ url) = @_;89 sub _get_stream_url_for { 90 my ($self, $enclosure) = @_; 77 91 78 $url =~ m!.*\.(.+)!; 79 my $ext = $1; 80 Plagger->context->log(error => "Cannot determine file type.") unless $ext; 81 82 my $new_url; 92 my $stream_url; 93 83 94 # TODO support multi stream addresses 84 if ($e xt =~ /^asx$/) {85 my $res = $self->{ua}->fetch($ url)95 if ($enclosure->type =~ /asf/) { 96 my $res = $self->{ua}->fetch($enclosure->url) 86 97 or Plagger->context->log; 87 98 $res->content =~ m!((?:http|mms)://.*?\.(?:wsx|wma|asf))!; 88 $ new_url = $1;99 $stream_url = $1; 89 100 } 90 elsif ($e xt =~ /^ram$/) {91 my $res = $self->{ua}->fetch($ url)101 elsif ($enclosure->type =~ /realaudio/) { 102 my $res = $self->{ua}->fetch($enclosure->url) 92 103 or Plagger->context->log; 93 104 $res->content =~ m!(rtsp://.*?\.(?:rm|smi))!; 94 $ new_url = $1;105 $stream_url = $1; 95 106 } 96 107 97 Plagger->context->log(debug => "Rewrite url from $url to $new_url"); 108 Plagger->context->log(warn => "Cannot get stream url for " . $enclosure->url) 109 unless $stream_url; 98 110 99 return $ new_url or $url;111 return $stream_url; 100 112 } 101 102 113 103 114 1;
