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

Last change on this file since 10218 was 10218, checked in by kjdon, 19 years ago

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

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