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

Last change on this file since 18465 was 18465, checked in by mdewsnip, 12 years ago

Added "no strict 'refs'" to prevent errors when increasing verbosity. By Jeffrey Ke at DL Consulting Ltd.

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1###########################################################################
2#
3# BaseMediaConverter - helper plugin that provide base functionality for
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###########################################################################
27package BaseMediaConverter;
28
29use PrintInfo;
30
31use convertutil;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35use gsprintf 'gsprintf';
36
37BEGIN {
38 @BaseMediaConverter::ISA = ('PrintInfo');
39}
40
41my $arguments = [
42 ];
43
44my $options = { 'name' => "BaseMediaConverter",
45 'desc' => "{BaseMediaConverter.desc}",
46 'abstract' => "yes",
47 'inherits' => "yes",
48 'args' => $arguments };
49
50sub new {
51 my ($class) = shift (@_);
52 my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
53 push(@$pluginlist, $class);
54
55 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
56 push(@{$hashArgOptLists->{"OptList"}},$options);
57
58 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, $auxiliary);
59
60 return bless $self, $class;
61}
62
63sub begin {
64 my $self = shift (@_);
65 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
66
67 # Save base_dir for use in file cache
68 $self->{'base_dir'} = $base_dir;
69
70}
71
72
73sub init_cache_for_file
74{
75 my $self = shift @_;
76 my ($filename) = @_;
77
78 my $verbosity = $self->{'verbosity'};
79 my $outhandle = $self->{'outhandle'};
80 my $base_dir = $self->{'base_dir'};
81
82 # Work out relative filename within 'base_dir'
83 $filename =~ s/\\/\//g;
84 my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
85
86 $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
87
88 # Setup cached_dir and file_root
89
90 my ($file_root, $dirname, $suffix)
91 = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
92
93 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
94 my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
95
96 if (!-e $base_output_dir ) {
97 print $outhandle "Creating directory $base_output_dir\n"
98 if ($verbosity>2);
99
100 &util::mk_all_dir($base_output_dir);
101 }
102
103 my $output_dir = &util::filename_cat($base_output_dir,$file_root);
104
105 if (!-e $output_dir) {
106 print $outhandle "Creating directory $output_dir\n"
107 if ($verbosity>2);
108
109 &util::mk_dir($output_dir);
110 }
111
112 $self->{'cached_dir'} = $output_dir;
113 $self->{'cached_file_root'} = $file_root;
114
115}
116
117
118
119sub run_general_cmd
120{
121 my $self = shift @_;
122 my ($command,$print_info) = @_;
123
124
125 if (!defined $print_info->{'verbosity'}) {
126 $print_info->{'verbosity'} = $self->{'verbosity'};
127 }
128
129 if (!defined $print_info->{'outhandle'}) {
130 $print_info->{'outhandle'} = $self->{'outhandle'};
131 }
132
133
134 return &convertutil::run_general_cmd(@_);
135}
136
137
138sub regenerate_general_cmd
139{
140 my $self = shift @_;
141 my ($command,$ofilename,$print_info) = @_;
142
143 if (!defined $print_info->{'verbosity'}) {
144 $print_info->{'verbosity'} = $self->{'verbosity'};
145 }
146
147 if (!defined $print_info->{'outhandle'}) {
148 $print_info->{'outhandle'} = $self->{'outhandle'};
149 }
150
151 return &convertutil::regenerate_general_cmd(@_);
152}
153
154
155
156sub run_cached_general_cmd
157{
158 my $self = shift @_;
159
160 my ($command,$ofilename,$print_info) = @_;
161
162 if (!defined $print_info->{'verbosity'}) {
163 $print_info->{'verbosity'} = $self->{'verbosity'};
164 }
165
166 if (!defined $print_info->{'outhandle'}) {
167 $print_info->{'outhandle'} = $self->{'outhandle'};
168 }
169
170 return &convertutil::run_cached_general_cmd(@_);
171}
172
173
174
175sub autorun_general_cmd
176{
177 my $self = shift @_;
178
179 my ($command,$ofilename,$print_info) = @_;
180
181 my $result;
182 my $regenerated;
183 my $had_error;
184
185 if ($self->{'cache_generated_images'}) {
186 ($regenerated,$result,$had_error)
187 = $self->run_cached_general_cmd($command,$ofilename,$print_info);
188 }
189 else {
190 $regenerated = 1; # always true for a command that is always run
191 ($result,$had_error)
192 = $self->run_general_cmd($command,$print_info);
193 }
194
195 return ($regenerated,$result,$had_error);
196}
197
198
199#
2001;
Note: See TracBrowser for help on using the repository browser.