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