source: gsdl/trunk/perllib/plugins/MP3Plugin.pm@ 15865

Last change on this file since 15865 was 15865, checked in by kjdon, 16 years ago

renaming plugins in preparation for my plugin overhaul

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1###########################################################################
2#
3# MP3Plug.pm -- Plugin for MP3 files (MPEG audio layer 3).
4#
5# A component of the Greenstone digital library software from the New
6# Zealand Digital Library Project at the University of Waikato, New
7# Zealand.
8#
9# Copyright (C) 2001 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful, but
17# WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19# General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27
28package MP3Plug;
29
30use UnknownPlug;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use MP3::Info;
36
37require giget;
38
39sub BEGIN {
40 @MP3Plug::ISA = ('UnknownPlug');
41}
42
43my $arguments =
44 [ { 'name' => "process_exp",
45 'desc' => "{BasPlug.process_exp}",
46 'type' => "regexp",
47 'deft' => &get_default_process_exp(),
48 'reqd' => "no" },
49 { 'name' => "assoc_images",
50 'desc' => "{MP3Plug.assoc_images}",
51 'type' => "flag",
52 'deft' => "",
53 'reqd' => "no" },
54 { 'name' => "applet_metadata",
55 'desc' => "{MP3Plug.applet_metadata}",
56 'type' => "flag",
57 'deft' => "" },
58 { 'name' => "metadata_fields",
59 'desc' => "{MP3Plug.metadata_fields}",
60 'type' => "string",
61 'deft' => "Title,Artist,Genre" } ];
62
63my $options = { 'name' => "MP3Plug",
64 'desc' => "{MP3Plug.desc}",
65 'abstract' => "no",
66 'inherits' => "yes",
67 'args' => $arguments };
68
69sub new {
70 my ($class) = shift (@_);
71 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
72 push(@$pluginlist, $class);
73
74 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
75 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
76
77 my $self = new UnknownPlug($pluginlist, $inputargs, $hashArgOptLists);
78
79 return bless $self, $class;
80}
81
82sub get_default_process_exp {
83 return q^(?i)\.mp3$^;
84}
85
86sub gen_mp3applet {
87
88 my ($mp3_filename) = @_;
89
90 my $applet_html = '
91<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
92 WIDTH = "59"
93 HEIGHT = "32"
94 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
95<PARAM NAME = CODE VALUE = "javazoom.jlGui.TinyPlayer" >
96<PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar" >
97<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
98<PARAM NAME="scriptable" VALUE="false">
99<PARAM NAME = "skin" VALUE ="_httpcollection_/skins/Digitalized">
100<PARAM NAME = "autoplay" VALUE ="yes">
101<PARAM NAME = "bgcolor" VALUE ="638182">
102<PARAM NAME = "audioURL" VALUE ="MP3FILENAME">
103<COMMENT>
104<EMBED type="application/x-java-applet;version=1.3"
105 CODE = "javazoom.jlGui.TinyPlayer"
106 ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar"
107 WIDTH = "59"
108 HEIGHT = "32"
109 skin = "_httpcollection_/skins/Digitalized"
110 autoplay = "yes"
111 bgcolor = "638182"
112 audioURL = "MP3FILENAME"
113 scriptable=false
114 pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
115<NOEMBED>
116</COMMENT>
117</NOEMBED></EMBED>
118</OBJECT>
119';
120
121 $applet_html =~ s/MP3FILENAME/$mp3_filename/g;
122
123 return $applet_html;
124}
125
126
127
128# Associate the mp3 file with the new document
129
130sub associate_mp3_file {
131 my $self = shift (@_);
132 my $filename = shift (@_); # filename with full path
133 my $file = shift (@_); # filename without path
134 my $doc_obj = shift (@_);
135
136 my $verbosity = $self->{'verbosity'};
137 my $outhandle = $self->{'outhandle'};
138
139 # check the filename is okay
140 return 0 if ($file eq "" || $filename eq "");
141
142 # Add the file metadata
143 my $assoc_url = $file;
144# $assoc_url =~ s/ /%20/g; # probably need to do more escaping than this!!
145 $assoc_url =~ s/ /_/g; # workaround for now
146 my $dst_file = $file;
147 $dst_file =~ s/ /_/g;
148
149 # Add the file as an associated file ...
150 my $section = $doc_obj->get_top_section();
151 my $mime_type = $self->{'mime_type'} || "audio/mp3";
152 my $assoc_field = $self->{'assoc_field'} || "mp3";
153 my $assoc_name = $file;
154 $assoc_name =~ s/\.mp3$//;
155
156 $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
157 $doc_obj->add_metadata ($section, "Source", $file);
158 $doc_obj->add_metadata ($section, $assoc_field, $assoc_name);
159 $doc_obj->add_metadata ($section, "srcurl", $assoc_url);
160 $doc_obj->add_metadata ($section, "NoText", "1");
161
162 my $mp3_info = get_mp3info($filename);
163 my $mp3_tags = get_mp3tag($filename);
164
165 my $metadata_fields = $self->{'metadata_fields'};
166
167 if ($metadata_fields eq "*") {
168 # Locate all info and tag metadata
169
170 foreach my $ki ( keys %$mp3_info ) {
171 my $mp3_metavalue = $mp3_info->{$ki};
172
173 if ($mp3_metavalue !~ m/^\s*$/s) {
174 my $mp3_metaname = "mp3:".lc($ki);
175 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
176 }
177 }
178
179 foreach my $kt ( keys %$mp3_tags ) {
180 my $mp3_metavalue = $mp3_tags->{$kt};
181
182 if ($mp3_metavalue !~ m/^\s*$/s) {
183 my $kt_len = length($kt);
184 my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
185 my $mp3_metaname = "mp3:".$kt_initial_cap;
186
187 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
188 }
189 }
190 }
191 else {
192
193 # Restrict metadata to that specifically given
194 foreach my $field (split /,/, $metadata_fields) {
195
196 # check info
197 if (defined $mp3_info->{$field}) {
198
199 my $mp3i_metavalue = $mp3_info->{$field};
200
201 if ($mp3i_metavalue !~ m/^\s*$/s) {
202 my $mp3i_metaname = "mp3:".lc($field);
203 $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
204 }
205 }
206
207 # check tags
208 if (defined $mp3_tags->{uc($field)}) {
209
210 my $mp3t_metavalue = $mp3_tags->{uc($field)};
211
212 if ($mp3t_metavalue !~ m/^\s*$/s) {
213 my $mp3t_metaname = "mp3:".$field;
214
215 $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
216 }
217 }
218
219 }
220 }
221
222 $doc_obj->add_metadata ($section, "FileFormat", "MP3");
223
224 $doc_obj->add_metadata ($section, "srclink",
225 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
226 $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
227 $doc_obj->add_metadata ($section, "/srclink", "</a>");
228
229 my $applet_metadata = $self->{'applet_metadata'};
230 if (defined $applet_metadata && $applet_metadata ) {
231 my $applet_html
232 = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
233 $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
234 }
235
236 my $assoc_images = $self->{'assoc_images'};
237 if (defined $assoc_images && $assoc_images) {
238 my @search_terms = ();
239
240 my $title = $mp3_tags->{'TITLE'};
241 my $artist = $mp3_tags->{'ARTIST'};
242
243 if (defined $title && $title ne "") {
244
245 push(@search_terms,$title);
246
247 if (defined $artist && $artist ne "") {
248 push(@search_terms,$artist);
249 }
250 }
251 else {
252 push(@search_terms,$assoc_name);
253 }
254
255 push(@search_terms,"song");
256
257 my $output_dir = $filename;
258 $output_dir =~ s/\.\w+$//;
259
260 my ($imgref_urls) = giget(\@search_terms,$output_dir);
261
262 my $gi_base = gi_url_base();
263 my $gi_query_url = gi_query_url(\@search_terms);
264
265 $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
266 $doc_obj->add_metadata ($section, "/giquery", "</a>");
267
268 for (my $i=1; $i<=2; $i++) {
269 my $img_filename = "$output_dir/img_$i.jpg";
270 my $dst_file = "img_$i.jpg";
271
272 if (-e $img_filename) {
273 $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
274
275 my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
276
277 $doc_obj->add_metadata ($section, "img$i",
278 "<img $srcurl>");
279 $doc_obj->add_metadata ($section, "smallimg$i",
280 "<img $srcurl width=100>");
281
282 my $imgref_url = $imgref_urls->[$i-1];
283
284 $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
285 $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
286 }
287
288 }
289
290
291 }
292
293 return 1;
294}
295
296
297
298# The MP3Plug read() function is based on UnknownPlug read(). This
299# function does all the right things to make general options work for
300# a given plugin.
301
302my $mp3_doc_count = 0; ## is this used anywhere now !!???
303
304sub read {
305 my $self = shift (@_);
306 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
307
308 my $outhandle = $self->{'outhandle'};
309
310 #check for associate_ext, blocking etc
311 my ($block_status,$filename) = $self->read_block(@_);
312 return $block_status if ((!defined $block_status) || ($block_status==0));
313
314 print STDERR "<Processing n='$file' p='MP3Plug'>\n" if ($gli);
315 print $outhandle "MP3Plug processing \"$filename\"\n"
316 if $self->{'verbosity'} > 1;
317
318 #if there's a leading directory name, eat it...
319 $file =~ s/^.*[\/\\]//;
320
321 # create a new document
322 my $doc_obj = new doc ($filename, "indexed_doc");
323 $mp3_doc_count++;
324
325## $doc_obj->set_OIDtype ($processor->{'OIDtype'});
326 if ($processor->{'OIDtype'} =~ /^(assigned|dirname)$/) {
327 $doc_obj->set_OIDtype ($processor->{'OIDtype'}, $processor->{'OIDmetadata'});
328 }
329 else {
330 $doc_obj->set_OIDtype ("incremental"); # this is done to avoid hashing content of file
331 }
332 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
333 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
334
335 # associate the file with the document
336 if (associate_mp3_file($self, $filename, $file, $doc_obj) != 1)
337 {
338 print "MP3Plug: couldn't process \"$filename\"\n";
339 return 0;
340 }
341
342 #create an empty text string so we don't break downstream plugins
343 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}",1);
344 if ($self->{'assoc_images'}) {
345 $text .= "[img1]<br>";
346 $text .= "[img2]<br>";
347 }
348 # include any metadata passed in from previous plugins
349 my $section = $doc_obj->get_top_section();
350 $self->extra_metadata ($doc_obj, $section, $metadata);
351
352 $self->title_fallback($doc_obj,$section,$file);
353
354 # do plugin specific processing of doc_obj
355 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
356 $file, $metadata, $doc_obj));
357
358 # do any automatic metadata extraction
359 $self->auto_extract_metadata ($doc_obj);
360
361 # add an OID
362 $doc_obj->set_OID();
363 $doc_obj->add_utf8_text($section, $text);
364
365 # process the document
366 $processor->process($doc_obj);
367
368 $self->{'num_processed'} ++;
369 return 1;
370}
371
372
373sub title_fallback
374{
375 my $self = shift (@_);
376 my ($doc_obj,$section,$file) = @_;
377
378 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
379 my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
380 if (defined $mp3_title) {
381 $doc_obj->add_metadata ($section, "Title", $mp3_title);
382 }
383 else {
384 &BasPlug::title_fallback($self, $doc_obj, $section, $file);
385 }
386 }
387}
388
389
3901;
391
392
393
394
395
396
397
398
399
400
401
Note: See TracBrowser for help on using the repository browser.