source: trunk/gsdl/perllib/plugins/MP3Plug.pm@ 12270

Last change on this file since 12270 was 12270, checked in by kjdon, 18 years ago

set_OIDtype now takes two arguments, the type and the metadata (used if type=assigned)

  • 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' => "string",
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
161 my $mp3_info = get_mp3info($filename);
162 my $mp3_tags = get_mp3tag($filename);
163
164 my $metadata_fields = $self->{'metadata_fields'};
165
166 if ($metadata_fields eq "*") {
167 # Locate all info and tag metadata
168
169 foreach my $ki ( keys %$mp3_info ) {
170 my $mp3_metavalue = $mp3_info->{$ki};
171
172 if ($mp3_metavalue !~ m/^\s*$/s) {
173 my $mp3_metaname = "mp3:".lc($ki);
174 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
175 }
176 }
177
178 foreach my $kt ( keys %$mp3_tags ) {
179 my $mp3_metavalue = $mp3_tags->{$kt};
180
181 if ($mp3_metavalue !~ m/^\s*$/s) {
182 my $kt_len = length($kt);
183 my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
184 my $mp3_metaname = "mp3:".$kt_initial_cap;
185
186 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
187 }
188 }
189 }
190 else {
191
192 # Restrict metadata to that specifically given
193 foreach my $field (split /,/, $metadata_fields) {
194
195 # check info
196 if (defined $mp3_info->{$field}) {
197
198 my $mp3i_metavalue = $mp3_info->{$field};
199
200 if ($mp3i_metavalue !~ m/^\s*$/s) {
201 my $mp3i_metaname = "mp3:".lc($field);
202 $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
203 }
204 }
205
206 # check tags
207 if (defined $mp3_tags->{uc($field)}) {
208
209 my $mp3t_metavalue = $mp3_tags->{uc($field)};
210
211 if ($mp3t_metavalue !~ m/^\s*$/s) {
212 my $mp3t_metaname = "mp3:".$field;
213
214 $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
215 }
216 }
217
218 }
219 }
220
221 $doc_obj->add_metadata ($section, "FileFormat", "MP3");
222
223 $doc_obj->add_metadata ($section, "srclink",
224 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
225 $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
226 $doc_obj->add_metadata ($section, "/srclink", "</a>");
227
228 my $applet_metadata = $self->{'applet_metadata'};
229 if (defined $applet_metadata && $applet_metadata ) {
230 my $applet_html
231 = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
232 $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
233 }
234
235 my $assoc_images = $self->{'assoc_images'};
236 if (defined $assoc_images && $assoc_images) {
237 my @search_terms = ();
238
239 my $title = $mp3_tags->{'TITLE'};
240 my $artist = $mp3_tags->{'ARTIST'};
241
242 if (defined $title && $title ne "") {
243
244 push(@search_terms,$title);
245
246 if (defined $artist && $artist ne "") {
247 push(@search_terms,$artist);
248 }
249 }
250 else {
251 push(@search_terms,$assoc_name);
252 }
253
254 push(@search_terms,"song");
255
256 my $output_dir = $filename;
257 $output_dir =~ s/\.\w+$//;
258
259 my ($imgref_urls) = giget(\@search_terms,$output_dir);
260
261 my $gi_base = gi_url_base();
262 my $gi_query_url = gi_query_url(\@search_terms);
263
264 $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
265 $doc_obj->add_metadata ($section, "/giquery", "</a>");
266
267 for (my $i=1; $i<=2; $i++) {
268 my $img_filename = "$output_dir/img_$i.jpg";
269 my $dst_file = "img_$i.jpg";
270
271 if (-e $img_filename) {
272 $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
273
274 my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
275
276 $doc_obj->add_metadata ($section, "img$i",
277 "<img $srcurl>");
278 $doc_obj->add_metadata ($section, "smallimg$i",
279 "<img $srcurl width=100>");
280
281 my $imgref_url = $imgref_urls->[$i-1];
282
283 $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
284 $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
285 }
286
287 }
288
289
290 }
291
292 return 1;
293}
294
295
296
297# The MP3Plug read() function is based on UnknownPlug read(). This
298# function does all the right things to make general options work for
299# a given plugin.
300
301my $mp3_doc_count = 0; ## is this used anywhere now !!???
302
303sub read {
304 my $self = shift (@_);
305 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
306
307 my $outhandle = $self->{'outhandle'};
308
309 #check for associate_ext, blocking etc
310 my ($block_status,$filename) = $self->read_block(@_);
311 return $block_status if ((!defined $block_status) || ($block_status==0));
312
313 print STDERR "<Processing n='$file' p='MP3Plug'>\n" if ($gli);
314 print $outhandle "MP3Plug processing \"$filename\"\n"
315 if $self->{'verbosity'} > 1;
316
317 #if there's a leading directory name, eat it...
318 $file =~ s/^.*[\/\\]//;
319
320 # create a new document
321 my $doc_obj = new doc ($filename, "indexed_doc");
322 $mp3_doc_count++;
323
324## $doc_obj->set_OIDtype ($processor->{'OIDtype'});
325 if ($processor->{'OIDtype'} =~ /^(assigned|dirname)$/) {
326 $doc_obj->set_OIDtype ($processor->{'OIDtype'}, $processor->{'OIDmetadata'});
327 }
328 else {
329 $doc_obj->set_OIDtype ("incremental"); # this is done to avoid hashing content of file
330 }
331 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
332 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
333
334 # associate the file with the document
335 if (associate_mp3_file($self, $filename, $file, $doc_obj) != 1)
336 {
337 print "MP3Plug: couldn't process \"$filename\"\n";
338 return 0;
339 }
340
341 #create an empty text string so we don't break downstream plugins
342 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
343 if ($self->{'assoc_images'}) {
344 $text .= "[img1]<br>";
345 $text .= "[img2]<br>";
346 }
347 # include any metadata passed in from previous plugins
348 my $section = $doc_obj->get_top_section();
349 $self->extra_metadata ($doc_obj, $section, $metadata);
350
351 $self->title_fallback($doc_obj,$section,$file);
352
353 # do plugin specific processing of doc_obj
354 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
355 $file, $metadata, $doc_obj));
356
357 # do any automatic metadata extraction
358 $self->auto_extract_metadata ($doc_obj);
359
360 # add an OID
361 $doc_obj->set_OID();
362 $doc_obj->add_text($section, $text);
363
364 # process the document
365 $processor->process($doc_obj);
366
367 $self->{'num_processed'} ++;
368 return 1;
369}
370
371
372sub title_fallback
373{
374 my $self = shift (@_);
375 my ($doc_obj,$section,$file) = @_;
376
377 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
378 my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
379 if (defined $mp3_title) {
380 $doc_obj->add_metadata ($section, "Title", $mp3_title);
381 }
382 else {
383 &BasPlug::title_fallback($self, $doc_obj, $section, $file);
384 }
385 }
386}
387
388
3891;
390
391
392
393
394
395
396
397
398
399
400
Note: See TracBrowser for help on using the repository browser.