source: main/trunk/greenstone2/perllib/plugins/MediainfoOGVPlugin.pm

Last change on this file was 31492, checked in by kjdon, 7 years ago

renamed EncodingUtil to CommonUtil, BasePlugin to BaseImporter. The idea is that only top level plugins that you can specify in your collection get to have plugin in their name. Modified all other plugins to reflect these name changes

File size: 8.4 KB
Line 
1###########################################################################
2#
3# MediainfoOGVPlugin.pm -- Plugin for OGV multimedia files
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) 2010 Arnaud Yvan
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# MediainfoOGVPlugin - a plugin for OGV multimedia files
28# contributed to Greenstone by Arnaud Yvan
29
30# This is a simple Plugin for importing OGV multimedia files.
31# Mediainfo will retrieve the metadata of the file. A fictional document will
32# be created for every such file, and the file itself will be passed
33# to Greenstone as the "associated file" of the document.
34
35# Here's an example where it is useful: I have a collection of
36# ogg movie files with names like conference_20080402.ogv.
37# I add this line to the collection configuration file:
38
39# plugin MediainfoOGVPlugin -process_exp "*.ogv" -assoc_field "movie"
40
41# A document is created for each movie, with the associated movie
42# file's name in the "movie" metadata field.
43
44# The plugin also add some metadata :
45# Duration (in seconds), filesize, title, artist, location, organization,
46# date, contact, copyright.
47
48
49
50package MediainfoOGVPlugin;
51
52use BaseImporter;
53
54use strict;
55no strict 'refs'; # allow filehandles to be variables and viceversa
56no strict 'subs';
57
58sub BEGIN {
59 @MediainfoOGVPlugin::ISA = ('BaseImporter');
60}
61
62my $arguments =
63 [ { 'name' => "process_exp",
64 'desc' => "{BaseImporter.process_exp}",
65 'type' => "regexp",
66 'deft' => &get_default_process_exp(),
67 'reqd' => "no" },
68 { 'name' => "assoc_field",
69 'desc' => "{MediainfoOGVPlugin.assoc_field}",
70 'type' => "string",
71 'deft' => "Movie",
72 'reqd' => "no" },
73 ];
74
75my $options = { 'name' => "MediainfoOGVPlugin",
76 'desc' => "{MediainfoOGVPlugin.desc}",
77 'abstract' => "no",
78 'inherits' => "yes",
79 'args' => $arguments };
80
81
82sub new {
83 my ($class) = shift (@_);
84 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
85 push(@$pluginlist, $class);
86
87 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
88 push(@{$hashArgOptLists->{"OptList"}},$options);
89
90 my $self = new BaseImporter($pluginlist, $inputargs, $hashArgOptLists);
91
92 return bless $self, $class;
93}
94
95sub init {
96 my $self = shift(@_);
97 my ($verbosity, $outhandle, $failhandle) = @_;
98
99 $self->BaseImporter::init($verbosity, $outhandle, $failhandle);
100
101 # Check that Mediainfo is installed and available on the path (except for Windows 95/98)
102 if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
103 my $result = `mediainfo 2>&1`;
104 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
105 $self->{'mediainfo_not_installed'} = 1;
106 }
107 }
108
109}
110sub get_default_process_exp {
111 return q^(?i)\.ogv$^;
112}
113
114# MediainfoOGVPlugin processing of doc_obj.
115
116sub process {
117 my $self = shift (@_);
118 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
119
120 #print STDERR "\n\n**** START of processing MediainfoOGVPlugin\n\n";
121
122 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
123 my $outhandle = $self->{'outhandle'};
124 my $verbosity = $self->{'verbosity'};
125
126 # check the filename is okay - do we need this??
127 if ($filename_full_path eq "" || $filename_no_path eq "") {
128 print $outhandle "MediainfoOGVPlugin: couldn't process \"$filename_no_path\"\n";
129 return undef;
130 }
131
132 # Add the file as an associated file ...
133 my $section = $doc_obj->get_top_section();
134 my $file_format = "ogv";
135 my $mime_type = "video/ogg";
136 my $assoc_field = $self->{'assoc_field'};
137
138 # The assocfilename is the url-encoded version of the utf8 filename
139 my $assoc_file = $doc_obj->get_assocfile_from_sourcefile();
140
141 $doc_obj->associate_file($filename_full_path, $assoc_file, $mime_type, $section);
142 $doc_obj->add_metadata ($section, "FileFormat", $file_format);
143 $doc_obj->add_metadata ($section, "dc.Format", $file_format);
144 $doc_obj->add_metadata ($section, "MimeType", $mime_type);
145 $doc_obj->add_utf8_metadata ($section, $assoc_field, $doc_obj->get_source()); # Source metadata is already in utf8
146
147 # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
148 $doc_obj->add_metadata ($section, "srclink_file", $doc_obj->get_sourcefile());
149 $doc_obj->add_metadata ($section, "srclinkFile", $doc_obj->get_sourcefile());
150 $doc_obj->add_metadata ($section, "srcicon", "_iconogg_");
151
152 # we have no text - add dummy text and NoText metadata
153 $self->add_dummy_text($doc_obj, $section);
154
155 if (defined $self->{'mediainfo_not_installed'}) {
156 # can't do any extracted
157 print STDERR "Mediainfo not installed, so can't extract metadata\n";
158 return 1;
159 }
160
161
162 # Retrieve the file metadata
163 my $command = "mediainfo --inform=\"General;%Duration/String2%|%Duration%|%FileSize/String2%|%Performer%|%Title%|%Recorded_Date%|%Recorded/Location%|%Producer%|%Copyright%|%LICENSE%|%Publisher%\" \"$filename_full_path\"";
164 print $outhandle "$command\n" if ($verbosity > 2);
165
166 # execute the mediainfo command and store the output of execution in $videodata
167 # my $video_metadata = `$command`; # backticks operator way
168 # Another way to execute the command: better experience with this than with backticks operator above:
169 # use open() to read the output of executing the command (which is piped through to a handle using the | operator)
170 my $video_metadata;
171 if (open(VMIN,"$command|")) {
172 my $line;
173
174 # we read a line of output at a time
175 while (defined ($line=<VMIN>)) {
176 #print STDERR "***** line = $line\n";
177
178 $video_metadata .= $line;
179 }
180
181 close(VMIN);
182 }
183
184 print $outhandle "$video_metadata\n" if ($verbosity > 2);
185
186 # There are 10 fields separated by |, split these into ordered, individual-named variables
187 my ($FormattedDuration,$Duration,$Filesize,$Artist,$Title,$Date,$Location,$Organization,$Copyright,$License,$Contact) = split(/\|/,$video_metadata);
188 $Duration = int($Duration/1000);
189 $Artist =~ s/\\047/\'/g; # Perl's way of doing: $artist = `echo $artist | sed \"s/\\047/'/g\"`;
190 $Title =~ s/\\047/\'/g; # $title = `echo $title | sed \"s/\\047/'/g\"`;
191
192 print $outhandle "RESULT = $FormattedDuration\n$Duration\n$Filesize\n$Artist\n$Title\n$Date\n$Location\n$Organization\n$Copyright\n$License\n$Contact\n" if ($verbosity > 2);
193
194
195 $doc_obj->add_metadata ($section, "FormattedDuration", $FormattedDuration);
196 $doc_obj->add_metadata ($section, "Duration", $Duration);
197
198
199 $doc_obj->add_metadata($section,"FileSize",$Filesize);
200 $doc_obj->add_utf8_metadata($section,"dc.Creator",$Artist);
201 $doc_obj->add_utf8_metadata($section,"Artist",$Artist);
202 $doc_obj->add_utf8_metadata($section,"dc.Title",$Title);
203 $doc_obj->add_utf8_metadata($section,"Title",$Title);
204 $doc_obj->add_utf8_metadata($section,"dc.Date",$Date);
205 $doc_obj->add_utf8_metadata($section,"Date",$Date);
206 $doc_obj->add_utf8_metadata($section,"dc.Coverage",$Location);
207 $doc_obj->add_utf8_metadata($section,"Location",$Location);
208 $doc_obj->add_utf8_metadata($section,"dc.Publisher",$Organization);
209 $doc_obj->add_utf8_metadata($section,"Organization",$Organization);
210 $doc_obj->add_utf8_metadata($section,"dc.Rights",$Copyright);
211 $doc_obj->add_utf8_metadata($section,"Copyright",$Copyright);
212 $doc_obj->add_utf8_metadata($section,"dc.accessRights",$License);
213 $doc_obj->add_utf8_metadata($section,"License",$License);
214 $doc_obj->add_utf8_metadata($section,"dc.Contributor",$Contact);
215 $doc_obj->add_utf8_metadata($section,"Contact",$Contact);
216
217
218 #print STDERR "\n\n**** END of MediainfoOGVPlugin\n\n";
219
220 return 1;
221}
222
223
2241;
225
226
227
228
229
230
231
232
233
234
235
Note: See TracBrowser for help on using the repository browser.