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

Last change on this file since 14657 was 14657, checked in by anna, 17 years ago

Updated Perl Module in Marathi. Many thanks to Shubhada Nagarkar.

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