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

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

OID generation modifications: OIDtype and OIDmetadata options now available for plugins as well as import. OIDtype for plugins defaults to auto - if set to auto, then use the values from import. All plugins now call self->add_OID instead of doc_obj->set_OID. This sets the doc_obj OIDtype so that doesn't need to be donein other places any more. all plugins have the get_oid_hash_type method - normally returns hash_on_file, but can be overridden to return hash_on_ga_xml for those plugins that don't want hashing on file (MP3,OggVorbis...)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 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
87# we don't want to hash on the file
88sub get_oid_hash_type {
89 my $self = shift (@_);
90 return "hash_on_ga_xml";
91}
92
93sub process {
94 my $self = shift (@_);
95 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
96
97 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
98
99 # associate the file with the document
100 if ($self->associate_mp3_file($filename_full_path, $filename_no_path, $doc_obj) != 1)
101 {
102 print "MP3Plugin: couldn't process \"$filename_full_path\"\n";
103 return 0;
104 }
105
106 my $text = &gsprintf::lookup_string("{BasePlugin.dummy_text}",1);
107 if ($self->{'assoc_images'}) {
108 $text .= "[img1]<br>";
109 $text .= "[img2]<br>";
110 }
111 $doc_obj->add_utf8_text($doc_obj->get_top_section(), $text);
112 $doc_obj->add_metadata ($doc_obj->get_top_section(), "NoText", "1");
113
114}
115
116sub gen_mp3applet {
117
118 my ($mp3_filename) = @_;
119
120 my $applet_html = '
121<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
122 WIDTH = "59"
123 HEIGHT = "32"
124 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
125<PARAM NAME = CODE VALUE = "javazoom.jlGui.TinyPlayer" >
126<PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar" >
127<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
128<PARAM NAME="scriptable" VALUE="false">
129<PARAM NAME = "skin" VALUE ="_httpcollection_/skins/Digitalized">
130<PARAM NAME = "autoplay" VALUE ="yes">
131<PARAM NAME = "bgcolor" VALUE ="638182">
132<PARAM NAME = "audioURL" VALUE ="MP3FILENAME">
133<COMMENT>
134<EMBED type="application/x-java-applet;version=1.3"
135 CODE = "javazoom.jlGui.TinyPlayer"
136 ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar"
137 WIDTH = "59"
138 HEIGHT = "32"
139 skin = "_httpcollection_/skins/Digitalized"
140 autoplay = "yes"
141 bgcolor = "638182"
142 audioURL = "MP3FILENAME"
143 scriptable=false
144 pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
145<NOEMBED>
146</COMMENT>
147</NOEMBED></EMBED>
148</OBJECT>
149';
150
151 $applet_html =~ s/MP3FILENAME/$mp3_filename/g;
152
153 return $applet_html;
154}
155
156
157
158# Associate the mp3 file with the new document
159
160sub associate_mp3_file {
161 my $self = shift (@_);
162 my $filename = shift (@_); # filename with full path
163 my $file = shift (@_); # filename without path
164 my $doc_obj = shift (@_);
165
166 my $verbosity = $self->{'verbosity'};
167 my $outhandle = $self->{'outhandle'};
168
169 # check the filename is okay
170 return 0 if ($file eq "" || $filename eq "");
171
172 # Add the file metadata.
173 # $assoc_url will be the srcurl. Since it is URL encoded here, it will be
174 # able to cope with special characters--including spaces--in mp3 filenames.
175 # the assocfilename generated will be a URL encoded version of the utf8 filename
176 my $assoc_url = $doc_obj->get_sourcefile();
177 my $dst_file = $doc_obj->get_assocfile_from_sourcefile();
178
179 # Add the file as an associated file ...
180 my $section = $doc_obj->get_top_section();
181 my $mime_type = $self->{'mime_type'} || "audio/mp3";
182 my $assoc_field = $self->{'assoc_field'} || "mp3";
183 my $assoc_name = $file;
184 $assoc_name =~ s/\.mp3$//;
185
186 $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
187 $doc_obj->add_metadata ($section, $assoc_field, $assoc_name);
188 $doc_obj->add_metadata ($section, "srcurl", $assoc_url);
189
190 my $mp3_info = get_mp3info($filename);
191 my $mp3_tags = get_mp3tag($filename);
192
193 my $metadata_fields = $self->{'metadata_fields'};
194
195 if ($metadata_fields eq "*") {
196 # Locate all info and tag metadata
197
198 foreach my $ki ( keys %$mp3_info ) {
199 my $mp3_metavalue = $mp3_info->{$ki};
200
201 if ($mp3_metavalue !~ m/^\s*$/s) {
202 my $mp3_metaname = "mp3:".lc($ki);
203 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
204 }
205 }
206
207 foreach my $kt ( keys %$mp3_tags ) {
208 my $mp3_metavalue = $mp3_tags->{$kt};
209
210 if ($mp3_metavalue !~ m/^\s*$/s) {
211 my $kt_len = length($kt);
212 my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
213 my $mp3_metaname = "mp3:".$kt_initial_cap;
214
215 $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
216 }
217 }
218 }
219 else {
220
221 # Restrict metadata to that specifically given
222 foreach my $field (split /,/, $metadata_fields) {
223
224 # check info
225 if (defined $mp3_info->{$field}) {
226
227 my $mp3i_metavalue = $mp3_info->{$field};
228
229 if ($mp3i_metavalue !~ m/^\s*$/s) {
230 my $mp3i_metaname = "mp3:".lc($field);
231 $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
232 }
233 }
234
235 # check tags
236 if (defined $mp3_tags->{uc($field)}) {
237
238 my $mp3t_metavalue = $mp3_tags->{uc($field)};
239
240 if ($mp3t_metavalue !~ m/^\s*$/s) {
241 my $mp3t_metaname = "mp3:".$field;
242
243 $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
244 }
245 }
246
247 }
248 }
249
250 $doc_obj->add_metadata ($section, "FileFormat", "MP3");
251
252 $doc_obj->add_metadata ($section, "srclink",
253 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
254 $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
255 $doc_obj->add_metadata ($section, "/srclink", "</a>");
256
257 my $applet_metadata = $self->{'applet_metadata'};
258 if (defined $applet_metadata && $applet_metadata ) {
259 my $applet_html
260 = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
261 $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
262 }
263
264 my $assoc_images = $self->{'assoc_images'};
265 if (defined $assoc_images && $assoc_images) {
266 my @search_terms = ();
267
268 my $title = $mp3_tags->{'TITLE'};
269 my $artist = $mp3_tags->{'ARTIST'};
270
271 if (defined $title && $title ne "") {
272
273 push(@search_terms,$title);
274
275 if (defined $artist && $artist ne "") {
276 push(@search_terms,$artist);
277 }
278 }
279 else {
280 push(@search_terms,$assoc_name);
281 }
282
283 push(@search_terms,"song");
284
285 my $output_dir = $filename;
286 $output_dir =~ s/\.\w+$//;
287
288 my ($imgref_urls) = giget(\@search_terms,$output_dir);
289
290 my $gi_base = gi_url_base();
291 my $gi_query_url = gi_query_url(\@search_terms);
292
293 $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
294 $doc_obj->add_metadata ($section, "/giquery", "</a>");
295
296 for (my $i=1; $i<=2; $i++) {
297 my $img_filename = "$output_dir/img_$i.jpg";
298 my $dst_file = "img_$i.jpg";
299
300 if (-e $img_filename) {
301 $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
302
303 my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
304
305 $doc_obj->add_metadata ($section, "img$i",
306 "<img $srcurl>");
307 $doc_obj->add_metadata ($section, "smallimg$i",
308 "<img $srcurl width=100>");
309
310 my $imgref_url = $imgref_urls->[$i-1];
311
312 $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
313 $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
314 }
315
316 }
317
318
319 }
320
321 return 1;
322}
323
324
325# we want to use mp3:Title if its there, otherwise we'll use BasePlugin method
326sub title_fallback
327{
328 my $self = shift (@_);
329 my ($doc_obj,$section,$file) = @_;
330
331 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
332 my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
333 if (defined $mp3_title) {
334 $doc_obj->add_metadata ($section, "Title", $mp3_title);
335 }
336 else {
337 $self->BasePlugin::title_fallback($doc_obj, $section, $file);
338 }
339 }
340}
341
342
3431;
344
345
346
347
348
349
350
351
352
353
354
Note: See TracBrowser for help on using the repository browser.