source: gs2-extensions/video-and-audio/trunk/src/perllib/plugins/AudioPlugin.pm@ 26532

Last change on this file since 26532 was 26532, checked in by davidb, 12 years ago

enable-streaming-list added to allow 'mp3' as a streamable target. Updated call to audio_convertto_cmd to be consistent with the newer cached_cmd API.

File size: 11.6 KB
Line 
1######################################################################
2#
3# AudioPlugin.pm -- plugin for processing video largely based on ImagePlug
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package AudioPlugin;
27
28use strict;
29no strict 'refs'; # allow filehandles to be variables and viceversa
30no strict 'subs';
31
32use gsprintf;
33
34use MultimediaPlugin;
35use AudioConverter;
36
37sub BEGIN {
38 @AudioPlugin::ISA = ('MultimediaPlugin', 'AudioConverter');
39}
40
41
42my $enable_streaming_list =
43 [{'name' => "disabled", 'desc' => "Do not create any audio file optimised for streaming over the Internet."},
44 {'name' => "flv", 'desc' => "Uses the FLV format for streaming media. Better to target old computers."},
45 {'name' => "mp4", 'desc' => "Uses the MP4 container with H264 and AAC codecs. Better quality at very low bitrates but more ressources intensive."},
46 {'name' => "mp3", 'desc' => "(audio only) Uses MP3 for psuedo streaming."}
47 ];
48
49my $streaming_mime_types = { "flv" => "video/flv",
50 "mp4" => "video/mp4",
51 "mp3" => "audio/mpeg" };
52
53my $arguments =
54 [ { 'name' => "process_exp",
55 'desc' => "{BasePlugin.process_exp}",
56 'type' => "regexp",
57 'deft' => &get_default_process_exp(),
58 'reqd' => "no" },
59 { 'name' => "converttotype",
60 'desc' => "{AudioPlugin.converttotype}",
61 'type' => "string",
62 'deft' => "",
63 'reqd' => "no" },
64 { 'name' => "converttobitrate",
65 'desc' => "{AudioPlugin.converttobitrate}",
66 'type' => "string",
67 'deft' => "128k",
68 'reqd' => "no" },
69 { 'name' => "streamingbitrate",
70 'desc' => "{AudioPlugin.streamingbitrate}",
71 'type' => "string",
72 'deft' => "200k",
73 'reqd' => "no" },
74
75 { 'name' => "enable_streaming",
76 'desc' => "{MultimediaPlug.enable_streaming}",
77 'type' => "enum",
78 'list' => $enable_streaming_list,
79 'deft' => "disabled",
80 'reqd' => "no" }
81
82];
83
84my $options = { 'name' => "AudioPlugin",
85 'desc' => "{AudioPlugin.desc}",
86 'abstract' => "no",
87 'inherits' => "yes",
88 'args' => $arguments };
89
90
91sub new {
92 my ($class) = shift (@_);
93 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
94 push(@$pluginlist, $class);
95
96 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
97 push(@{$hashArgOptLists->{"OptList"}},$options);
98
99 new AudioConverter($pluginlist, $inputargs, $hashArgOptLists);
100 my $self = new MultimediaPlugin($pluginlist, $inputargs, $hashArgOptLists);
101
102
103 return bless $self, $class;
104}
105
106
107sub begin {
108 my $self = shift (@_);
109 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
110
111 $self->SUPER::begin(@_);
112 $self->AudioConverter::begin(@_);
113}
114
115
116sub init {
117 my $self = shift (@_);
118 my ($verbosity, $outhandle, $failhandle) = @_;
119
120 $self->SUPER::init(@_);
121 $self->AudioConverter::init(@_);
122}
123
124
125sub get_default_process_exp {
126 my $self = shift (@_);
127
128 return q^(?i)\.(mp3|au|aiff?|aifc|wav|ogg|flac|shn)$^;
129}
130
131
132
133# Create the keyframes, thumbnail and screenview images, and discover
134# the Video's size, width, and height using the ffmpeg utility.
135
136sub run_convert {
137 my $self = shift (@_);
138 my $base_dir = shift (@_);
139 my $filename = shift (@_); # filename with full path
140 my $file = shift (@_); # filename without path
141 my $doc_obj = shift (@_);
142
143 my $section = $doc_obj->get_top_section();
144
145 my $verbosity = $self->{'verbosity'};
146 my $outhandle = $self->{'outhandle'};
147
148 # check the filename is okay
149 return 0 if ($file eq "" || $filename eq "");
150
151 my $minimumsize = $self->{'minimumsize'};
152 if (defined $minimumsize && (-s $filename < $minimumsize)) {
153 print $outhandle "AudioPlugin: \"$filename\" too small, skipping\n"
154 if ($verbosity > 1);
155 }
156
157 my ($aduration,$asize,$atype,$afreq,$achan,$arate)
158 = &AudioConverter::identify($filename, $outhandle, $verbosity);
159
160 if ($aduration eq "N/A") {
161 print $outhandle "Unable to determine duration of $file\n";
162 $aduration = undef;
163 }
164
165 my ($dur_hour,$dur_min,$dur_sec)
166 = ($aduration =~ m/(\d+):(\d+):(\d+\.\d+)/);
167
168 my $total_dur_secs = undef;
169
170 if (defined $dur_hour && defined $dur_min && defined *dur_sec) {
171 $total_dur_secs = $dur_hour*3600 + $dur_min*60 + $dur_sec;
172 }
173
174
175 # Convert the audio to a new type (if required).
176 my $converttotype = $self->{'converttotype'};
177 my $converttosize = $self->{'converttosize'};
178
179 # shorten duration prcessed for experimentation purposes
180 my $exp_duration = undef;
181
182 my $excerpt_duration = $self->{'excerpt_duration'};
183
184 if ((defined $excerpt_duration) && ($excerpt_duration ne "")) {
185 $exp_duration = $excerpt_duration;
186 my ($hh,$mm,$ss,$ms) = ($exp_duration =~ m/^(\d\d):(\d\d):(\d\d)\.?(\d\d)?/);
187 my $excerpt_dur_in_secs = $hh * 3600 + $mm * 60 + $ss;
188
189 if (defined $total_dur_secs) {
190 if ($excerpt_dur_in_secs > $total_dur_secs) {
191 # clip is already shorter than requested video excerpt duration
192 # set exp_duration back to undefined
193 $exp_duration = undef;
194 }
195 }
196 else {
197 $exp_duration = undef;
198 }
199 }
200
201
202 if (defined $exp_duration)
203 {
204 print $outhandle "Only encoding first $exp_duration of video.\n";
205 $self->{'exp_duration'} = $exp_duration;
206 }
207
208 my $ascii_only_filenames = $self->{'use_ascii_only_filenames'};
209
210 $self->init_cache_for_file($filename);
211
212
213 my $originalfilename = undef;
214 my $type = "unknown";
215
216 my $output_dir = $self->{'cached_dir'};
217 my $iaudio_root = $self->{'cached_file_root'};
218
219 my $convertto_regenerated = 0;
220 if (($converttotype ne "" && $filename !~ m/$converttotype$/i)) {
221
222 $originalfilename = $filename;
223
224 my ($convertto_command,$ofilename,$ofile) = $self->audio_convertto_cmd($filename,$converttotype);
225
226 my $convertto_result;
227 my $convertto_error;
228
229 my $convertto_options = { @{$self->{'ffmpeg_monitor'}},
230 'message_prefix' => "Convert to",
231 'message' => "Converting audio to $converttotype" };
232
233 ($convertto_regenerated,$convertto_result,$convertto_error)
234 = $self->run_cached_general_cmd($convertto_command,$filename,$ofilename,$convertto_options);
235
236 $type = $converttotype;
237 }
238
239
240 # Add the audio metadata
241
242 my $file_unicode = pack("U0C*", map { ord($_) } split(//,$file)); # force explicitly to unicode
243
244 $file_unicode =~ s/\x{2018}|\x{2019}|\x{201C}|\x{201D}//g; # remove smart quotes as cause problem in URL for video server
245 $file_unicode =~ s/\x{2013}/\-/g; # change en-dash to '-' as again causes problems for video server
246
247## print STDERR "**** file metadata = ", &gsprintf::debug_unicode_string($file_unicode), "\n";
248
249
250 # split filename on char if specified
251 if ($file_unicode =~ m/\-/) {
252
253 my @file_split = split(/\s*\-\s*/,$file_unicode);
254 my $creator = shift @file_split;
255 my $title = shift @file_split;
256
257 $title =~ s/\..*?$//;
258 $title =~ s/^(.)/\u$1/;
259
260 $doc_obj->add_utf8_metadata($section,"Title",$title);
261
262 my @creator_split = split(/\s+and\s+/,$creator);
263 foreach my $c (@creator_split) {
264 $doc_obj->add_utf8_metadata($section,"Creator",$c);
265 }
266 }
267
268 $file = $file_unicode;
269# my $filemeta = $self->filename_to_utf8_metadata($file);
270# my $filemeta_url_safe = $self->url_safe($filemeta);
271
272 my $filemeta_url_safe = &unicode::filename_to_url($file);
273
274 $doc_obj->add_utf8_metadata ($section, "Audio", $filemeta_url_safe);
275
276 # Also want to set filename as 'Source' metadata to be
277 # consistent with other plugins
278# $doc_obj->add_utf8_metadata ($section, "Source", $filemeta_url_safe);
279
280
281 if ($atype ne " ") {
282 $type = $atype;
283 }
284
285 $doc_obj->add_metadata ($section, "FileFormat", $type);
286 $doc_obj->add_metadata ($section, "FileSize", $asize);
287
288 $doc_obj->add_metadata ($section, "AudioType", $atype);
289 $doc_obj->add_metadata ($section, "AudioDuration", $aduration);
290
291 $doc_obj->add_metadata ($section, "AudioFreq", $afreq);
292 $doc_obj->add_metadata ($section, "AudioChannels", $achan);
293 $doc_obj->add_metadata ($section, "AudioRate", $arate);
294
295 $doc_obj->add_utf8_metadata ($section, "srclink",
296 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Audio]\">");
297 $doc_obj->add_utf8_metadata ($section, "/srclink", "</a>");
298 $doc_obj->add_metadata ($section, "srcicon", "[AudioType]");
299
300 # Add the image as an associated file
301 $doc_obj->associate_file($filename,$file,"audio/$type",$section);
302
303
304
305
306 my $streamable_regenerated = 0;
307 my $optionally_run_general_cmd
308 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
309
310 if ($self->{'enable_streaming'}) {
311
312 my $enable_streaming = $self->{'enable_streaming'};
313
314 # Even if the original file or 'converttotype' is set to MP3
315 # we'll still go ahead and generate this MP3 as it might very
316 # well have different settings (such as a lower sample rate)
317
318 my $streaming_bitrate = $self->{'streamingbitrate'};
319 my $streaming_size = $self->{'streamingsize'};
320
321 my $ifilename = $originalfilename || $filename;
322
323 my ($stream_cmd,$mp_filename,$mp_file);
324 if ($enable_streaming eq "mp4") {
325 ($stream_cmd,$mp_filename,$mp_file)
326 = $self->audio_mp4_stream_cmd($ifilename, $streaming_size,
327 $streaming_bitrate);
328 }
329 else {
330 ($stream_cmd,$mp_filename,$mp_file)
331 = $self->audio_stream_ffmpeg_cmd($ifilename, $enable_streaming,
332 $streaming_bitrate, $streaming_size,);
333
334 }
335
336
337 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
338 'message_prefix' => "Stream",
339 'message' => "Generating streamable audio: $mp_file" };
340
341 my $streamable_result;
342 my $streamable_had_error;
343 ($streamable_regenerated,$streamable_result,$streamable_had_error)
344 = $self->$optionally_run_general_cmd($stream_cmd,$ifilename,$mp_filename,$streamable_options);
345
346 if (!$streamable_had_error) {
347
348 my $streamable_url = $mp_file;
349 my $streamable_url_safe = $self->url_safe($streamable_url);
350
351 $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
352 $doc_obj->associate_file($mp_filename,$mp_file,"audio/mpeg",
353 $section);
354 }
355
356 # The following aren't currently used
357
358 $self->{'mp_file'} = $mp_file;
359 $self->{'mp_filename'} = $mp_filename;
360 }
361
362
363 return $type;
364}
365
366
367
368
369sub read_into_doc_obj {
370 my $self = shift (@_);
371 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
372
373 $self->{'media_type'} = "audio";
374
375 my ($rv,$doc_obj) = $self->SUPER::read_into_doc_obj(@_);
376
377 if ($rv != 1) {
378 return ($rv,$doc_obj);
379 }
380
381 $self->{'media_type'} = undef;
382
383 return ($rv,$doc_obj);
384}
385
386
387
3881;
389
390
391
392
393
394
395
396
397
398
399
Note: See TracBrowser for help on using the repository browser.