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 | ###########################################################################
|
---|
27 | package BaseMediaConverter;
|
---|
28 |
|
---|
29 | use PrintInfo;
|
---|
30 |
|
---|
31 | use convertutil;
|
---|
32 |
|
---|
33 | use strict;
|
---|
34 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
35 | use gsprintf 'gsprintf';
|
---|
36 |
|
---|
37 | BEGIN {
|
---|
38 | @BaseMediaConverter::ISA = ('PrintInfo');
|
---|
39 | }
|
---|
40 |
|
---|
41 | my $arguments = [
|
---|
42 | { 'name' => "enable_cache",
|
---|
43 | 'desc' => "{BaseMediaConverter.enable_cache}",
|
---|
44 | 'type' => "flag",
|
---|
45 | 'reqd' => "no",
|
---|
46 | }
|
---|
47 |
|
---|
48 | ];
|
---|
49 |
|
---|
50 | my $options = { 'name' => "BaseMediaConverter",
|
---|
51 | 'desc' => "{BaseMediaConverter.desc}",
|
---|
52 | 'abstract' => "yes",
|
---|
53 | 'inherits' => "yes",
|
---|
54 | 'args' => $arguments };
|
---|
55 |
|
---|
56 | sub 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 |
|
---|
69 | sub begin {
|
---|
70 | my $self = shift (@_);
|
---|
71 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
72 |
|
---|
73 | # Save base_dir for use in file cache
|
---|
74 | $self->{'base_dir'} = $base_dir;
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | sub 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'
|
---|
89 | $filename =~ s/\\/\//g;
|
---|
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 | $collect_dir =~ s/\\/\//g; # Work in Unix style world
|
---|
101 |
|
---|
102 | # if dirname is in collections tmp area, remove collect_dir prefix
|
---|
103 | $dirname =~ s/^$collect_dir//;
|
---|
104 |
|
---|
105 | if ($ENV{'GSDLOS'} eq "windows") {
|
---|
106 | # if dirname starts with Windows drive letter, strip it off
|
---|
107 | $dirname =~ s/^[a-z]:\///i;
|
---|
108 | }
|
---|
109 |
|
---|
110 | my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
|
---|
111 |
|
---|
112 | if (!-e $base_output_dir ) {
|
---|
113 | print $outhandle "Creating directory $base_output_dir\n"
|
---|
114 | if ($verbosity>2);
|
---|
115 |
|
---|
116 | &util::mk_all_dir($base_output_dir);
|
---|
117 | }
|
---|
118 |
|
---|
119 | my $output_dir = &util::filename_cat($base_output_dir,$file_root);
|
---|
120 |
|
---|
121 | if (!-e $output_dir) {
|
---|
122 | print $outhandle "Creating directory $output_dir\n"
|
---|
123 | if ($verbosity>2);
|
---|
124 |
|
---|
125 | &util::mk_dir($output_dir);
|
---|
126 | }
|
---|
127 |
|
---|
128 | $self->{'cached_dir'} = $output_dir;
|
---|
129 | $self->{'cached_file_root'} = $file_root;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | sub run_general_cmd
|
---|
135 | {
|
---|
136 | my $self = shift @_;
|
---|
137 | my ($command,$print_info) = @_;
|
---|
138 |
|
---|
139 |
|
---|
140 | if (!defined $print_info->{'verbosity'}) {
|
---|
141 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (!defined $print_info->{'outhandle'}) {
|
---|
145 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | return &convertutil::run_general_cmd(@_);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | sub regenerate_general_cmd
|
---|
154 | {
|
---|
155 | my $self = shift @_;
|
---|
156 | my ($command,$ofilename,$print_info) = @_;
|
---|
157 |
|
---|
158 | if (!defined $print_info->{'verbosity'}) {
|
---|
159 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (!defined $print_info->{'outhandle'}) {
|
---|
163 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
164 | }
|
---|
165 |
|
---|
166 | return &convertutil::regenerate_general_cmd(@_);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | sub run_uncached_general_cmd
|
---|
172 | {
|
---|
173 | my $self = shift @_;
|
---|
174 |
|
---|
175 | my ($command,$ofilename,$print_info) = @_;
|
---|
176 |
|
---|
177 | return $self->run_general_cmd($command,$print_info);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 |
|
---|
182 | sub run_cached_general_cmd
|
---|
183 | {
|
---|
184 | my $self = shift @_;
|
---|
185 |
|
---|
186 | my ($command,$ofilename,$print_info) = @_;
|
---|
187 |
|
---|
188 | if (!defined $print_info->{'verbosity'}) {
|
---|
189 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (!defined $print_info->{'outhandle'}) {
|
---|
193 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
194 | }
|
---|
195 |
|
---|
196 | return &convertutil::run_cached_general_cmd(@_);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | sub autorun_general_cmd
|
---|
202 | {
|
---|
203 | my $self = shift @_;
|
---|
204 |
|
---|
205 | my ($command,$ofilename,$print_info) = @_;
|
---|
206 |
|
---|
207 | my $result;
|
---|
208 | my $regenerated;
|
---|
209 | my $had_error;
|
---|
210 |
|
---|
211 | if ($self->{'enable_cache'}) {
|
---|
212 | ($regenerated,$result,$had_error)
|
---|
213 | = $self->run_cached_general_cmd($command,$ofilename,$print_info);
|
---|
214 | }
|
---|
215 | else {
|
---|
216 | $regenerated = 1; # always true for a command that is always run
|
---|
217 | ($result,$had_error)
|
---|
218 | = $self->run_general_cmd($command,$print_info);
|
---|
219 | }
|
---|
220 |
|
---|
221 | return ($regenerated,$result,$had_error);
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | #
|
---|
226 | 1;
|
---|