source: gsdl/trunk/perllib/plugins/BaseMediaConverter.pm@ 18555

Last change on this file since 18555 was 18555, checked in by davidb, 15 years ago

General tidy up of code for supporting cached files. The generate_cached_images plugin option has been changed to enable_cache as this makes more sense for the other media formats we are begining to support, such as audio and video

  • Property svn:executable set to *
File size: 5.3 KB
RevLine 
[16826]1###########################################################################
2#
[16981]3# BaseMediaConverter - helper plugin that provide base functionality for
[16826]4# image/video conversion using ImageMagick/ffmpeg
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2008 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
[16981]27package BaseMediaConverter;
[16826]28
29use PrintInfo;
30
31use convertutil;
32
33use strict;
[18465]34no strict 'refs'; # allow filehandles to be variables and viceversa
[16826]35use gsprintf 'gsprintf';
36
37BEGIN {
[16981]38 @BaseMediaConverter::ISA = ('PrintInfo');
[16826]39}
40
41my $arguments = [
[18555]42 { 'name' => "enable_cache",
43 'desc' => "{BaseMediaConverter.enable_cache}",
44 'type' => "flag",
45 'reqd' => "no",
46 }
47
[16826]48 ];
49
[16981]50my $options = { 'name' => "BaseMediaConverter",
51 'desc' => "{BaseMediaConverter.desc}",
[16826]52 'abstract' => "yes",
53 'inherits' => "yes",
54 'args' => $arguments };
55
56sub new {
57 my ($class) = shift (@_);
58 my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
59 push(@$pluginlist, $class);
60
61 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
62 push(@{$hashArgOptLists->{"OptList"}},$options);
63
64 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, $auxiliary);
65
66 return bless $self, $class;
67}
68
[16847]69sub begin {
70 my $self = shift (@_);
71 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
[16826]72
[16847]73 # Save base_dir for use in file cache
74 $self->{'base_dir'} = $base_dir;
[16826]75
[16847]76}
77
78
[16826]79sub init_cache_for_file
80{
81 my $self = shift @_;
82 my ($filename) = @_;
83
84 my $verbosity = $self->{'verbosity'};
85 my $outhandle = $self->{'outhandle'};
86 my $base_dir = $self->{'base_dir'};
87
88 # Work out relative filename within 'base_dir'
[18105]89 $filename =~ s/\\/\//g;
[16826]90 my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
91
92 $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
93
94 # Setup cached_dir and file_root
95
96 my ($file_root, $dirname, $suffix)
97 = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
98
99 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
100 my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
101
102 if (!-e $base_output_dir ) {
103 print $outhandle "Creating directory $base_output_dir\n"
104 if ($verbosity>2);
105
106 &util::mk_all_dir($base_output_dir);
107 }
108
109 my $output_dir = &util::filename_cat($base_output_dir,$file_root);
110
111 if (!-e $output_dir) {
112 print $outhandle "Creating directory $output_dir\n"
113 if ($verbosity>2);
114
115 &util::mk_dir($output_dir);
116 }
117
118 $self->{'cached_dir'} = $output_dir;
119 $self->{'cached_file_root'} = $file_root;
120
121}
122
123
124
125sub run_general_cmd
126{
127 my $self = shift @_;
128 my ($command,$print_info) = @_;
129
130
131 if (!defined $print_info->{'verbosity'}) {
132 $print_info->{'verbosity'} = $self->{'verbosity'};
133 }
134
135 if (!defined $print_info->{'outhandle'}) {
136 $print_info->{'outhandle'} = $self->{'outhandle'};
137 }
138
139
140 return &convertutil::run_general_cmd(@_);
141}
142
143
144sub regenerate_general_cmd
145{
146 my $self = shift @_;
147 my ($command,$ofilename,$print_info) = @_;
148
149 if (!defined $print_info->{'verbosity'}) {
150 $print_info->{'verbosity'} = $self->{'verbosity'};
151 }
152
153 if (!defined $print_info->{'outhandle'}) {
154 $print_info->{'outhandle'} = $self->{'outhandle'};
155 }
156
157 return &convertutil::regenerate_general_cmd(@_);
158}
159
160
161
[18555]162sub run_uncached_general_cmd
163{
164 my $self = shift @_;
165
166 my ($command,$ofilename,$print_info) = @_;
167
168 return $self->run_general_cmd($command,$print_info);
169}
170
171
172
[16826]173sub run_cached_general_cmd
174{
175 my $self = shift @_;
176
177 my ($command,$ofilename,$print_info) = @_;
178
179 if (!defined $print_info->{'verbosity'}) {
180 $print_info->{'verbosity'} = $self->{'verbosity'};
181 }
182
183 if (!defined $print_info->{'outhandle'}) {
184 $print_info->{'outhandle'} = $self->{'outhandle'};
185 }
186
187 return &convertutil::run_cached_general_cmd(@_);
188}
189
190
191
192sub autorun_general_cmd
193{
194 my $self = shift @_;
195
196 my ($command,$ofilename,$print_info) = @_;
197
198 my $result;
199 my $regenerated;
200 my $had_error;
201
[18555]202 if ($self->{'enable_cache'}) {
[16826]203 ($regenerated,$result,$had_error)
204 = $self->run_cached_general_cmd($command,$ofilename,$print_info);
205 }
206 else {
207 $regenerated = 1; # always true for a command that is always run
208 ($result,$had_error)
209 = $self->run_general_cmd($command,$print_info);
210 }
211
212 return ($regenerated,$result,$had_error);
213}
214
215
216#
2171;
Note: See TracBrowser for help on using the repository browser.