[7487] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # MP3Plugin.pm -- Plugin for MP3 files (MPEG audio layer 3).
|
---|
[7487] | 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 |
|
---|
[15872] | 28 | package MP3Plugin;
|
---|
[7487] | 29 |
|
---|
[15872] | 30 | use BasePlugin;
|
---|
[7487] | 31 |
|
---|
[10254] | 32 | use strict;
|
---|
| 33 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[15872] | 34 | no strict 'subs';
|
---|
[10254] | 35 |
|
---|
[7487] | 36 | use MP3::Info;
|
---|
| 37 |
|
---|
| 38 | require giget;
|
---|
| 39 |
|
---|
| 40 | sub BEGIN {
|
---|
[15872] | 41 | @MP3Plugin::ISA = ('BasePlugin');
|
---|
[7487] | 42 | }
|
---|
| 43 |
|
---|
| 44 | my $arguments =
|
---|
[9167] | 45 | [ { 'name' => "process_exp",
|
---|
[15872] | 46 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[14687] | 47 | 'type' => "regexp",
|
---|
[9167] | 48 | 'deft' => &get_default_process_exp(),
|
---|
| 49 | 'reqd' => "no" },
|
---|
| 50 | { 'name' => "assoc_images",
|
---|
[15872] | 51 | 'desc' => "{MP3Plugin.assoc_images}",
|
---|
[7487] | 52 | 'type' => "flag",
|
---|
| 53 | 'deft' => "",
|
---|
[7504] | 54 | 'reqd' => "no" },
|
---|
| 55 | { 'name' => "applet_metadata",
|
---|
[15872] | 56 | 'desc' => "{MP3Plugin.applet_metadata}",
|
---|
[7504] | 57 | 'type' => "flag",
|
---|
| 58 | 'deft' => "" },
|
---|
| 59 | { 'name' => "metadata_fields",
|
---|
[15872] | 60 | 'desc' => "{MP3Plugin.metadata_fields}",
|
---|
[7504] | 61 | 'type' => "string",
|
---|
| 62 | 'deft' => "Title,Artist,Genre" } ];
|
---|
[7487] | 63 |
|
---|
[15872] | 64 | my $options = { 'name' => "MP3Plugin",
|
---|
| 65 | 'desc' => "{MP3Plugin.desc}",
|
---|
[7487] | 66 | 'abstract' => "no",
|
---|
| 67 | 'inherits' => "yes",
|
---|
| 68 | 'args' => $arguments };
|
---|
| 69 |
|
---|
| 70 | sub new {
|
---|
[10218] | 71 | my ($class) = shift (@_);
|
---|
| 72 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 73 | push(@$pluginlist, $class);
|
---|
[7487] | 74 |
|
---|
[15872] | 75 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 76 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[7487] | 77 |
|
---|
[15872] | 78 | my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 79 |
|
---|
[7487] | 80 | return bless $self, $class;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | sub get_default_process_exp {
|
---|
| 84 | return q^(?i)\.mp3$^;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[15872] | 87 | sub process {
|
---|
| 88 | my $self = shift (@_);
|
---|
| 89 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
| 90 |
|
---|
[16392] | 91 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[15872] | 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 |
|
---|
[15911] | 107 | my $text = &gsprintf::lookup_string("{BasePlugin.dummy_text}",1);
|
---|
[15872] | 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);
|
---|
[15911] | 113 | $doc_obj->add_metadata ($doc_obj->get_top_section(), "NoText", "1");
|
---|
[15872] | 114 |
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[7487] | 117 | sub 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" >
|
---|
[9962] | 127 | <PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar" >
|
---|
[7487] | 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"
|
---|
[9962] | 137 | ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar"
|
---|
[7487] | 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 |
|
---|
| 161 | sub 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();
|
---|
[7504] | 182 | my $mime_type = $self->{'mime_type'} || "audio/mp3";
|
---|
[7487] | 183 | my $assoc_field = $self->{'assoc_field'} || "mp3";
|
---|
| 184 | my $assoc_name = $file;
|
---|
| 185 | $assoc_name =~ s/\.mp3$//;
|
---|
| 186 |
|
---|
[7504] | 187 | $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
|
---|
[7487] | 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);
|
---|
[7504] | 192 | my $mp3_tags = get_mp3tag($filename);
|
---|
[7487] | 193 |
|
---|
[7504] | 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 | }
|
---|
[7487] | 206 | }
|
---|
[7504] | 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);
|
---|
[10254] | 213 | my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
|
---|
[7504] | 214 | my $mp3_metaname = "mp3:".$kt_initial_cap;
|
---|
| 215 |
|
---|
| 216 | $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
[7487] | 219 | }
|
---|
[7504] | 220 | else {
|
---|
[7487] | 221 |
|
---|
[7504] | 222 | # Restrict metadata to that specifically given
|
---|
| 223 | foreach my $field (split /,/, $metadata_fields) {
|
---|
[7487] | 224 |
|
---|
[7504] | 225 | # check info
|
---|
| 226 | if (defined $mp3_info->{$field}) {
|
---|
[7487] | 227 |
|
---|
[7504] | 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 | }
|
---|
[7487] | 234 | }
|
---|
[7504] | 235 |
|
---|
| 236 | # check tags
|
---|
[7513] | 237 | if (defined $mp3_tags->{uc($field)}) {
|
---|
[7504] | 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 |
|
---|
[7487] | 248 | }
|
---|
| 249 | }
|
---|
[7528] | 250 |
|
---|
| 251 | $doc_obj->add_metadata ($section, "FileFormat", "MP3");
|
---|
[7487] | 252 |
|
---|
| 253 | $doc_obj->add_metadata ($section, "srclink",
|
---|
[11834] | 254 | "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
|
---|
[11305] | 255 | $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
|
---|
[7487] | 256 | $doc_obj->add_metadata ($section, "/srclink", "</a>");
|
---|
| 257 |
|
---|
[7504] | 258 | my $applet_metadata = $self->{'applet_metadata'};
|
---|
| 259 | if (defined $applet_metadata && $applet_metadata ) {
|
---|
| 260 | my $applet_html
|
---|
[11834] | 261 | = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
|
---|
[7504] | 262 | $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
|
---|
| 263 | }
|
---|
[7487] | 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 |
|
---|
[9962] | 284 | push(@search_terms,"song");
|
---|
| 285 |
|
---|
[7487] | 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 |
|
---|
[8365] | 294 | $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
|
---|
[7487] | 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 |
|
---|
[11834] | 304 | my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
|
---|
[7487] | 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 |
|
---|
[8365] | 313 | $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
|
---|
[7487] | 314 | $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | return 1;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 |
|
---|
[15872] | 326 | # we want to use mp3:Title if its there, otherwise we'll use BasePlugin method
|
---|
[8519] | 327 | sub title_fallback
|
---|
| 328 | {
|
---|
| 329 | my $self = shift (@_);
|
---|
| 330 | my ($doc_obj,$section,$file) = @_;
|
---|
[7487] | 331 |
|
---|
[8519] | 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 {
|
---|
[15872] | 338 | $self->BasePlugin::title_fallback($doc_obj, $section, $file);
|
---|
[8519] | 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 |
|
---|
[7487] | 344 | 1;
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 |
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 |
|
---|