[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] | 27 | package BaseMediaConverter;
|
---|
[16826] | 28 |
|
---|
| 29 | use PrintInfo;
|
---|
| 30 |
|
---|
| 31 | use convertutil;
|
---|
| 32 |
|
---|
| 33 | use strict;
|
---|
[18465] | 34 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[16826] | 35 | use gsprintf 'gsprintf';
|
---|
| 36 |
|
---|
| 37 | BEGIN {
|
---|
[16981] | 38 | @BaseMediaConverter::ISA = ('PrintInfo');
|
---|
[16826] | 39 | }
|
---|
| 40 |
|
---|
| 41 | my $arguments = [
|
---|
[18555] | 42 | { 'name' => "enable_cache",
|
---|
| 43 | 'desc' => "{BaseMediaConverter.enable_cache}",
|
---|
| 44 | 'type' => "flag",
|
---|
| 45 | 'reqd' => "no",
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[16826] | 48 | ];
|
---|
| 49 |
|
---|
[16981] | 50 | my $options = { 'name' => "BaseMediaConverter",
|
---|
| 51 | 'desc' => "{BaseMediaConverter.desc}",
|
---|
[16826] | 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 |
|
---|
[16847] | 69 | sub 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;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
[16826] | 78 | sub init_cache_for_file
|
---|
| 79 | {
|
---|
| 80 | my $self = shift @_;
|
---|
| 81 | my ($filename) = @_;
|
---|
| 82 |
|
---|
| 83 | my $verbosity = $self->{'verbosity'};
|
---|
| 84 | my $outhandle = $self->{'outhandle'};
|
---|
| 85 | my $base_dir = $self->{'base_dir'};
|
---|
[24350] | 86 |
|
---|
| 87 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 88 | $collect_dir =~ s/\\/\//g; # Work in Unix style world
|
---|
[16826] | 89 |
|
---|
| 90 | # Work out relative filename within 'base_dir'
|
---|
[18105] | 91 | $filename =~ s/\\/\//g;
|
---|
[30427] | 92 | $base_dir =~ s/\\/\//g;
|
---|
[16826] | 93 | my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
|
---|
| 94 |
|
---|
[24350] | 95 | if (!defined $file) {
|
---|
| 96 | # Perhaps the filename is taken from within cache_dir area?
|
---|
[30427] | 97 | my $cached_dir = &FileUtils::filenameConcatenate($collect_dir,"cached");
|
---|
| 98 | ($file) = ($filename =~ m/^$cached_dir(.*?)$/);
|
---|
| 99 | # A cached name already has the fileroot baked -in as the last directory name
|
---|
| 100 | # => strip it out
|
---|
| 101 | $file =~ s/[\/\\][^\/\\]+([\/\\][^\/\\]*)$/$1/;
|
---|
| 102 |
|
---|
| 103 | # Commented out code is the previous version that looked to
|
---|
| 104 | # handle this situation, but ran foul of situtation where
|
---|
| 105 | # sub-directories within 'import' are used
|
---|
| 106 | #
|
---|
| 107 | # # Perhaps the filename is taken from within cache_dir area?
|
---|
| 108 | # my $prev_cached_dir = $self->{'cached_dir'};
|
---|
| 109 | # ($file) = ($filename =~ m/^$prev_cached_dir(.*?)$/);
|
---|
[24350] | 110 | }
|
---|
| 111 |
|
---|
[16826] | 112 | $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
|
---|
| 113 |
|
---|
[24350] | 114 |
|
---|
[16826] | 115 | # Setup cached_dir and file_root
|
---|
| 116 |
|
---|
| 117 | my ($file_root, $dirname, $suffix)
|
---|
| 118 | = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
|
---|
| 119 |
|
---|
[20001] | 120 | # if dirname is in collections tmp area, remove collect_dir prefix
|
---|
| 121 | $dirname =~ s/^$collect_dir//;
|
---|
| 122 |
|
---|
| 123 | if ($ENV{'GSDLOS'} eq "windows") {
|
---|
| 124 | # if dirname starts with Windows drive letter, strip it off
|
---|
| 125 | $dirname =~ s/^[a-z]:\///i;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[27509] | 128 | my $base_output_dir = &FileUtils::filenameConcatenate($collect_dir,"cached",$dirname);
|
---|
[30427] | 129 | ## my $base_output_dir = &FileUtils::filenameConcatenate($collect_dir,"cached",$dirname);
|
---|
[16826] | 130 |
|
---|
| 131 | if (!-e $base_output_dir ) {
|
---|
| 132 | print $outhandle "Creating directory $base_output_dir\n"
|
---|
| 133 | if ($verbosity>2);
|
---|
| 134 |
|
---|
[27509] | 135 | &FileUtils::makeAllDirectories($base_output_dir);
|
---|
[16826] | 136 | }
|
---|
| 137 |
|
---|
[27509] | 138 | my $output_dir = &FileUtils::filenameConcatenate($base_output_dir,$file_root);
|
---|
[16826] | 139 |
|
---|
| 140 | if (!-e $output_dir) {
|
---|
| 141 | print $outhandle "Creating directory $output_dir\n"
|
---|
| 142 | if ($verbosity>2);
|
---|
| 143 |
|
---|
[27509] | 144 | &FileUtils::makeAllDirectories($output_dir);
|
---|
[16826] | 145 | }
|
---|
| 146 |
|
---|
| 147 | $self->{'cached_dir'} = $output_dir;
|
---|
[20001] | 148 | $self->{'cached_file_root'} = $file_root;
|
---|
[16826] | 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | sub run_general_cmd
|
---|
| 154 | {
|
---|
| 155 | my $self = shift @_;
|
---|
| 156 | my ($command,$print_info) = @_;
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 160 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 164 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | return &convertutil::run_general_cmd(@_);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | sub regenerate_general_cmd
|
---|
| 173 | {
|
---|
| 174 | my $self = shift @_;
|
---|
[22431] | 175 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 176 |
|
---|
| 177 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 178 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 182 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return &convertutil::regenerate_general_cmd(@_);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 |
|
---|
[18555] | 190 | sub run_uncached_general_cmd
|
---|
| 191 | {
|
---|
| 192 | my $self = shift @_;
|
---|
| 193 |
|
---|
[22431] | 194 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[18555] | 195 |
|
---|
| 196 | return $self->run_general_cmd($command,$print_info);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
[16826] | 201 | sub run_cached_general_cmd
|
---|
| 202 | {
|
---|
| 203 | my $self = shift @_;
|
---|
| 204 |
|
---|
[22431] | 205 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 206 |
|
---|
| 207 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 208 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 212 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | return &convertutil::run_cached_general_cmd(@_);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | sub autorun_general_cmd
|
---|
| 221 | {
|
---|
| 222 | my $self = shift @_;
|
---|
| 223 |
|
---|
[22431] | 224 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 225 |
|
---|
| 226 | my $result;
|
---|
| 227 | my $regenerated;
|
---|
| 228 | my $had_error;
|
---|
| 229 |
|
---|
[18555] | 230 | if ($self->{'enable_cache'}) {
|
---|
[16826] | 231 | ($regenerated,$result,$had_error)
|
---|
[22431] | 232 | = $self->run_cached_general_cmd($command,$ifilename,$ofilename,$print_info);
|
---|
[16826] | 233 | }
|
---|
| 234 | else {
|
---|
| 235 | $regenerated = 1; # always true for a command that is always run
|
---|
| 236 | ($result,$had_error)
|
---|
| 237 | = $self->run_general_cmd($command,$print_info);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | return ($regenerated,$result,$had_error);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | #
|
---|
| 245 | 1;
|
---|