| 1 |
package Plagger::Plugin::Filter::FetchEnclosure::Mplayer; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw(Plagger::Plugin::Filter::FetchEnclosure); |
|---|
| 4 |
use File::Path; |
|---|
| 5 |
|
|---|
| 6 |
sub register { |
|---|
| 7 |
my($self, $context) = @_; |
|---|
| 8 |
$context->register_hook( |
|---|
| 9 |
$self, |
|---|
| 10 |
'update.entry.fixup' => \&fetch, |
|---|
| 11 |
); |
|---|
| 12 |
} |
|---|
| 13 |
|
|---|
| 14 |
sub init { |
|---|
| 15 |
my $self = shift; |
|---|
| 16 |
$self->SUPER::init(@_); |
|---|
| 17 |
|
|---|
| 18 |
$self->{ua} = Plagger::UserAgent->new; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
sub fetch { |
|---|
| 22 |
my($self, $context, $args) = @_; |
|---|
| 23 |
|
|---|
| 24 |
unless (defined $self->conf->{type}) { |
|---|
| 25 |
$context->log(error => q{config 'type' is not set.}); |
|---|
| 26 |
} |
|---|
| 27 |
my $file_name_base = $args->{entry}->date->strftime('%Y%m%d-%H%M'); |
|---|
| 28 |
|
|---|
| 29 |
for my $enclosure ($args->{entry}->enclosures) { |
|---|
| 30 |
|
|---|
| 31 |
unless ($enclosure->type =~ $self->conf->{type}) { |
|---|
| 32 |
$enclosure->local_path('dummy') unless $enclosure->local_path; |
|---|
| 33 |
next; |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
my $dir_name = $self->conf->{sub_dir_name} || $args->{feed}->id_safe; |
|---|
| 37 |
my $feed_dir |
|---|
| 38 |
= File::Spec->catfile($self->conf->{dir}, $dir_name); |
|---|
| 39 |
unless (-e $feed_dir && -d _) { |
|---|
| 40 |
$context->log(info => "mkdir $feed_dir"); |
|---|
| 41 |
mkdir $feed_dir, 0777; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
my $stream_url = ($self->_get_stream_urls_for($enclosure))[0]; |
|---|
| 45 |
my $enclosure_num = 0; |
|---|
| 46 |
my $file_name = "$file_name_base-$enclosure_num"; |
|---|
| 47 |
my $file_path_base = File::Spec->catfile($feed_dir, "$file_name"); |
|---|
| 48 |
my $output_path = "$file_path_base.wav"; |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
my $fetched_extension = $self->conf->{consider_fetched} || 'mp3'; |
|---|
| 52 |
my $fetched_data_path = "$file_path_base.$fetched_extension"; |
|---|
| 53 |
if (-e $fetched_data_path || -e $output_path) { |
|---|
| 54 |
my $length = -s _; |
|---|
| 55 |
$ enclosure->url($enclosure->url); |
|---|
| 56 |
$enclosure->length($length); |
|---|
| 57 |
$enclosure->type('audio/x-wav'); |
|---|
| 58 |
$enclosure->local_path($output_path); |
|---|
| 59 |
$context->log(debug => $enclosure->url . "is already stored in $output_path"); |
|---|
| 60 |
next; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
$context->log(info => "fetch $stream_url to $output_path"); |
|---|
| 64 |
|
|---|
| 65 |
my @options = split(/ /,$self->conf->{option}) or (); |
|---|
| 66 |
my @command = ( |
|---|
| 67 |
'mplayer', |
|---|
| 68 |
$stream_url, |
|---|
| 69 |
'-vc', 'dummy', |
|---|
| 70 |
'-vo', 'null', |
|---|
| 71 |
'-ao', "pcm:file=$output_path", |
|---|
| 72 |
'-quiet', |
|---|
| 73 |
@options, |
|---|
| 74 |
); |
|---|
| 75 |
|
|---|
| 76 |
system (@command); |
|---|
| 77 |
|
|---|
| 78 |
if (!$? && -s $output_path ) { |
|---|
| 79 |
my $length = -s _; |
|---|
| 80 |
$enclosure->url($stream_url); |
|---|
| 81 |
$enclosure->length($length); |
|---|
| 82 |
$enclosure->type('audio/x-wav'); |
|---|
| 83 |
$enclosure->local_path($output_path); |
|---|
| 84 |
|
|---|
| 85 |
$context->log(info => "Dumping to $output_path is done."); |
|---|
| 86 |
} |
|---|
| 87 |
else { |
|---|
| 88 |
$context->log(warn => "Can not dump to $output_path"); |
|---|
| 89 |
$context->log(info => "Deleting $output_path"); |
|---|
| 90 |
unlink $output_path or $context->log(warn => "Can not delete $output_path."); |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
sub _get_stream_urls_for { |
|---|
| 96 |
my ($self, $enclosure) = @_; |
|---|
| 97 |
|
|---|
| 98 |
my @stream_urls; |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
if ($enclosure->type =~ /asf/) { |
|---|
| 102 |
my $res = $self->{ua}->fetch($enclosure->url) |
|---|
| 103 |
or Plagger->context->log; |
|---|
| 104 |
@stream_urls |
|---|
| 105 |
= $res->content =~ m!((?:http|mms)://.*?\.(?:wmv|wsx|wma|asf))!g; |
|---|
| 106 |
} |
|---|
| 107 |
elsif ($enclosure->type =~ /realaudio/) { |
|---|
| 108 |
my $res = $self->{ua}->fetch($enclosure->url) |
|---|
| 109 |
or Plagger->context->log; |
|---|
| 110 |
@stream_urls |
|---|
| 111 |
= $res->content =~ m!(rtsp://.*?\.(?:rm|smi))!g; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
Plagger->context->log(warn => "Cannot get stream url for " . $enclosure->url) |
|---|
| 115 |
unless @stream_urls; |
|---|
| 116 |
|
|---|
| 117 |
return @stream_urls; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
1; |
|---|
| 121 |
|
|---|
| 122 |
__END__ |
|---|
| 123 |
|
|---|
| 124 |
=head1 NAME |
|---|
| 125 |
|
|---|
| 126 |
Plagger::Plugin::Filter::FetchEnclosure::Mplayer - Fetch enclosures using mplayer |
|---|
| 127 |
|
|---|
| 128 |
=head1 SYNOPSIS |
|---|
| 129 |
|
|---|
| 130 |
- module: Filter::FetchEnclosure::Mplayer |
|---|
| 131 |
config: |
|---|
| 132 |
dir: /path/to/download |
|---|
| 133 |
sub_dir_name: sub_directory_name |
|---|
| 134 |
type: asf |
|---|
| 135 |
|
|---|
| 136 |
=head1 DESCRIPTION |
|---|
| 137 |
|
|---|
| 138 |
This plugin dumps audio/video stream to a wav file. |
|---|
| 139 |
|
|---|
| 140 |
=head1 AUTHOR |
|---|
| 141 |
|
|---|
| 142 |
Yohei Fushii |
|---|
| 143 |
|
|---|
| 144 |
=head1 SEE ALSO |
|---|
| 145 |
|
|---|
| 146 |
L<Plagger> |
|---|
| 147 |
|
|---|
| 148 |
=cut |
|---|