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

Last change on this file since 16952 was 16952, checked in by ak19, 16 years ago

Associated file names are url-encoded

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 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) = &util::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 my $text = &gsprintf::lookup_string("{BasePlugin.dummy_text}",1);
108 if ($self->{'assoc_images'}) {
109 $text .= "[img1]<br>";
110 $text .= "[img2]<br>";
111 }
112 $doc_obj->add_utf8_text($doc_obj->get_top_section(), $text);
113 $doc_obj->add_metadata ($doc_obj->get_top_section(), "NoText", "1");
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 # $assoc_url will be the srcurl. Since it is URL encoded here, it will be
175 # able to cope with special characters--including spaces--in mp3 filenames.
176 # the assocfilename generated will be a URL encoded version of the utf8 filename
177 my $assoc_url = $doc_obj->get_sourcefile();
178 my $dst_file = $doc_obj->get_assocfile_from_sourcefile();
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
191 my $mp3_info = get_mp3info($filename);
192 my $mp3_tags = get_mp3tag($filename);
193
194 my $metadata_fields = $self->{'metadata_fields'};
195
196 if ($metadata_fields eq "*") {
197 # Locate all info and tag metadata
198
199 foreach my $ki ( keys %$mp3_info ) {
200 my $mp3_metavalue = $mp3_info->{$ki};
201
202 if ($mp3_metavalue !~ m/^\s*$/s) {
203 my $mp3_metaname = "mp3:".lc($ki);
204 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
205 }
206 }
207
208 foreach my $kt ( keys %$mp3_tags ) {
209 my $mp3_metavalue = $mp3_tags->{$kt};
210
211 if ($mp3_metavalue !~ m/^\s*$/s) {
212 my $kt_len = length($kt);
213 my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
214 my $mp3_metaname = "mp3:".$kt_initial_cap;
215
216 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
217 }
218 }
219 }
220 else {
221
222 # Restrict metadata to that specifically given
223 foreach my $field (split /,/, $metadata_fields) {
224
225 # check info
226 if (defined $mp3_info->{$field}) {
227
228 my $mp3i_metavalue = $mp3_info->{$field};
229
230 if ($mp3i_metavalue !~ m/^\s*$/s) {
231 my $mp3i_metaname = "mp3:".lc($field);
232 $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
233 }
234 }
235
236 # check tags
237 if (defined $mp3_tags->{uc($field)}) {
238
239 my $mp3t_metavalue = $mp3_tags->{uc($field)};
240
241 if ($mp3t_metavalue !~ m/^\s*$/s) {
242 my $mp3t_metaname = "mp3:".$field;
243
244 $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
245 }
246 }
247
248 }
249 }
250
251 $doc_obj->add_metadata ($section, "FileFormat", "MP3");
252
253 $doc_obj->add_metadata ($section, "srclink",
254 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
255 $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
256 $doc_obj->add_metadata ($section, "/srclink", "</a>");
257
258 my $applet_metadata = $self->{'applet_metadata'};
259 if (defined $applet_metadata && $applet_metadata ) {
260 my $applet_html
261 = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
262 $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
263 }
264
265 my $assoc_images = $self->{'assoc_images'};
266 if (defined $assoc_images && $assoc_images) {
267 my @search_terms = ();
268
269 my $title = $mp3_tags->{'TITLE'};
270 my $artist = $mp3_tags->{'ARTIST'};
271
272 if (defined $title && $title ne "") {
273
274 push(@search_terms,$title);
275
276 if (defined $artist && $artist ne "") {
277 push(@search_terms,$artist);
278 }
279 }
280 else {
281 push(@search_terms,$assoc_name);
282 }
283
284 push(@search_terms,"song");
285
286 my $output_dir = $filename;
287 $output_dir =~ s/\.\w+$//;
288
289 my ($imgref_urls) = giget(\@search_terms,$output_dir);
290
291 my $gi_base = gi_url_base();
292 my $gi_query_url = gi_query_url(\@search_terms);
293
294 $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
295 $doc_obj->add_metadata ($section, "/giquery", "</a>");
296
297 for (my $i=1; $i<=2; $i++) {
298 my $img_filename = "$output_dir/img_$i.jpg";
299 my $dst_file = "img_$i.jpg";
300
301 if (-e $img_filename) {
302 $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
303
304 my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
305
306 $doc_obj->add_metadata ($section, "img$i",
307 "<img $srcurl>");
308 $doc_obj->add_metadata ($section, "smallimg$i",
309 "<img $srcurl width=100>");
310
311 my $imgref_url = $imgref_urls->[$i-1];
312
313 $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
314 $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
315 }
316
317 }
318
319
320 }
321
322 return 1;
323}
324
325
326# we want to use mp3:Title if its there, otherwise we'll use BasePlugin method
327sub title_fallback
328{
329 my $self = shift (@_);
330 my ($doc_obj,$section,$file) = @_;
331
332 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
333 my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
334 if (defined $mp3_title) {
335 $doc_obj->add_metadata ($section, "Title", $mp3_title);
336 }
337 else {
338 $self->BasePlugin::title_fallback($doc_obj, $section, $file);
339 }
340 }
341}
342
343
3441;
345
346
347
348
349
350
351
352
353
354
355
Note: See TracBrowser for help on using the repository browser.