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

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

plugin overhaul: plugins renamed to xxPlugin, and in some cases the names are made more sensible. They now use the new base plugins. Hopefully we have better code reuse. Some of the plugins still need work done as I didn't want to spend another month doing this before committing it. Alos, I haven't really tested anything yet...

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1###########################################################################
2#
3# MP3Plugin.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 MP3Plugin;
29
30use BasePlugin;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34no strict 'subs';
35
36use MP3::Info;
37
38require giget;
39
40sub BEGIN {
41 @MP3Plugin::ISA = ('BasePlugin');
42}
43
44my $arguments =
45 [ { 'name' => "process_exp",
46 'desc' => "{BasePlugin.process_exp}",
47 'type' => "regexp",
48 'deft' => &get_default_process_exp(),
49 'reqd' => "no" },
50 { 'name' => "assoc_images",
51 'desc' => "{MP3Plugin.assoc_images}",
52 'type' => "flag",
53 'deft' => "",
54 'reqd' => "no" },
55 { 'name' => "applet_metadata",
56 'desc' => "{MP3Plugin.applet_metadata}",
57 'type' => "flag",
58 'deft' => "" },
59 { 'name' => "metadata_fields",
60 'desc' => "{MP3Plugin.metadata_fields}",
61 'type' => "string",
62 'deft' => "Title,Artist,Genre" } ];
63
64my $options = { 'name' => "MP3Plugin",
65 'desc' => "{MP3Plugin.desc}",
66 'abstract' => "no",
67 'inherits' => "yes",
68 'args' => $arguments };
69
70sub new {
71 my ($class) = shift (@_);
72 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
73 push(@$pluginlist, $class);
74
75 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
76 push(@{$hashArgOptLists->{"OptList"}},$options);
77
78 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
79
80 return bless $self, $class;
81}
82
83sub get_default_process_exp {
84 return q^(?i)\.mp3$^;
85}
86
87sub process {
88 my $self = shift (@_);
89 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
90
91 my ($filename_full_path, $filename_no_path) = $self->get_full_filenames($base_dir, $file);
92 # do something about OIDtype so no hashing
93
94 # old code was in effect the following.
95 if ($doc_obj->{'OIDtype'} =~ /^hash$/) {
96 $doc_obj->set_OIDtype ("incremental");
97 }
98
99
100 # associate the file with the document
101 if ($self->associate_mp3_file($filename_full_path, $filename_no_path, $doc_obj) != 1)
102 {
103 print "MP3Plugin: couldn't process \"$filename_full_path\"\n";
104 return 0;
105 }
106
107 #whats this crap?
108 my $text = &gsprintf::lookup_string("{BasePlugin.dummy_text}",1);
109 if ($self->{'assoc_images'}) {
110 $text .= "[img1]<br>";
111 $text .= "[img2]<br>";
112 }
113 $doc_obj->add_utf8_text($doc_obj->get_top_section(), $text);
114
115}
116
117sub gen_mp3applet {
118
119 my ($mp3_filename) = @_;
120
121 my $applet_html = '
122<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
123 WIDTH = "59"
124 HEIGHT = "32"
125 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
126<PARAM NAME = CODE VALUE = "javazoom.jlGui.TinyPlayer" >
127<PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar" >
128<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
129<PARAM NAME="scriptable" VALUE="false">
130<PARAM NAME = "skin" VALUE ="_httpcollection_/skins/Digitalized">
131<PARAM NAME = "autoplay" VALUE ="yes">
132<PARAM NAME = "bgcolor" VALUE ="638182">
133<PARAM NAME = "audioURL" VALUE ="MP3FILENAME">
134<COMMENT>
135<EMBED type="application/x-java-applet;version=1.3"
136 CODE = "javazoom.jlGui.TinyPlayer"
137 ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar"
138 WIDTH = "59"
139 HEIGHT = "32"
140 skin = "_httpcollection_/skins/Digitalized"
141 autoplay = "yes"
142 bgcolor = "638182"
143 audioURL = "MP3FILENAME"
144 scriptable=false
145 pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
146<NOEMBED>
147</COMMENT>
148</NOEMBED></EMBED>
149</OBJECT>
150';
151
152 $applet_html =~ s/MP3FILENAME/$mp3_filename/g;
153
154 return $applet_html;
155}
156
157
158
159# Associate the mp3 file with the new document
160
161sub associate_mp3_file {
162 my $self = shift (@_);
163 my $filename = shift (@_); # filename with full path
164 my $file = shift (@_); # filename without path
165 my $doc_obj = shift (@_);
166
167 my $verbosity = $self->{'verbosity'};
168 my $outhandle = $self->{'outhandle'};
169
170 # check the filename is okay
171 return 0 if ($file eq "" || $filename eq "");
172
173 # Add the file metadata
174 my $assoc_url = $file;
175# $assoc_url =~ s/ /%20/g; # probably need to do more escaping than this!!
176 $assoc_url =~ s/ /_/g; # workaround for now
177 my $dst_file = $file;
178 $dst_file =~ s/ /_/g;
179
180 # Add the file as an associated file ...
181 my $section = $doc_obj->get_top_section();
182 my $mime_type = $self->{'mime_type'} || "audio/mp3";
183 my $assoc_field = $self->{'assoc_field'} || "mp3";
184 my $assoc_name = $file;
185 $assoc_name =~ s/\.mp3$//;
186
187 $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
188 $doc_obj->add_metadata ($section, $assoc_field, $assoc_name);
189 $doc_obj->add_metadata ($section, "srcurl", $assoc_url);
190 $doc_obj->add_metadata ($section, "NoText", "1");
191
192 my $mp3_info = get_mp3info($filename);
193 my $mp3_tags = get_mp3tag($filename);
194
195 my $metadata_fields = $self->{'metadata_fields'};
196
197 if ($metadata_fields eq "*") {
198 # Locate all info and tag metadata
199
200 foreach my $ki ( keys %$mp3_info ) {
201 my $mp3_metavalue = $mp3_info->{$ki};
202
203 if ($mp3_metavalue !~ m/^\s*$/s) {
204 my $mp3_metaname = "mp3:".lc($ki);
205 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
206 }
207 }
208
209 foreach my $kt ( keys %$mp3_tags ) {
210 my $mp3_metavalue = $mp3_tags->{$kt};
211
212 if ($mp3_metavalue !~ m/^\s*$/s) {
213 my $kt_len = length($kt);
214 my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
215 my $mp3_metaname = "mp3:".$kt_initial_cap;
216
217 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
218 }
219 }
220 }
221 else {
222
223 # Restrict metadata to that specifically given
224 foreach my $field (split /,/, $metadata_fields) {
225
226 # check info
227 if (defined $mp3_info->{$field}) {
228
229 my $mp3i_metavalue = $mp3_info->{$field};
230
231 if ($mp3i_metavalue !~ m/^\s*$/s) {
232 my $mp3i_metaname = "mp3:".lc($field);
233 $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
234 }
235 }
236
237 # check tags
238 if (defined $mp3_tags->{uc($field)}) {
239
240 my $mp3t_metavalue = $mp3_tags->{uc($field)};
241
242 if ($mp3t_metavalue !~ m/^\s*$/s) {
243 my $mp3t_metaname = "mp3:".$field;
244
245 $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
246 }
247 }
248
249 }
250 }
251
252 $doc_obj->add_metadata ($section, "FileFormat", "MP3");
253
254 $doc_obj->add_metadata ($section, "srclink",
255 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
256 $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
257 $doc_obj->add_metadata ($section, "/srclink", "</a>");
258
259 my $applet_metadata = $self->{'applet_metadata'};
260 if (defined $applet_metadata && $applet_metadata ) {
261 my $applet_html
262 = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
263 $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
264 }
265
266 my $assoc_images = $self->{'assoc_images'};
267 if (defined $assoc_images && $assoc_images) {
268 my @search_terms = ();
269
270 my $title = $mp3_tags->{'TITLE'};
271 my $artist = $mp3_tags->{'ARTIST'};
272
273 if (defined $title && $title ne "") {
274
275 push(@search_terms,$title);
276
277 if (defined $artist && $artist ne "") {
278 push(@search_terms,$artist);
279 }
280 }
281 else {
282 push(@search_terms,$assoc_name);
283 }
284
285 push(@search_terms,"song");
286
287 my $output_dir = $filename;
288 $output_dir =~ s/\.\w+$//;
289
290 my ($imgref_urls) = giget(\@search_terms,$output_dir);
291
292 my $gi_base = gi_url_base();
293 my $gi_query_url = gi_query_url(\@search_terms);
294
295 $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
296 $doc_obj->add_metadata ($section, "/giquery", "</a>");
297
298 for (my $i=1; $i<=2; $i++) {
299 my $img_filename = "$output_dir/img_$i.jpg";
300 my $dst_file = "img_$i.jpg";
301
302 if (-e $img_filename) {
303 $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
304
305 my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
306
307 $doc_obj->add_metadata ($section, "img$i",
308 "<img $srcurl>");
309 $doc_obj->add_metadata ($section, "smallimg$i",
310 "<img $srcurl width=100>");
311
312 my $imgref_url = $imgref_urls->[$i-1];
313
314 $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
315 $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
316 }
317
318 }
319
320
321 }
322
323 return 1;
324}
325
326
327# we want to use mp3:Title if its there, otherwise we'll use BasePlugin method
328sub title_fallback
329{
330 my $self = shift (@_);
331 my ($doc_obj,$section,$file) = @_;
332
333 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
334 my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
335 if (defined $mp3_title) {
336 $doc_obj->add_metadata ($section, "Title", $mp3_title);
337 }
338 else {
339 $self->BasePlugin::title_fallback($doc_obj, $section, $file);
340 }
341 }
342}
343
344
3451;
346
347
348
349
350
351
352
353
354
355
356
Note: See TracBrowser for help on using the repository browser.