[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'};
|
---|
| 86 |
|
---|
| 87 | # Work out relative filename within 'base_dir'
|
---|
[18105] | 88 | $filename =~ s/\\/\//g;
|
---|
[16826] | 89 | my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
|
---|
| 90 |
|
---|
| 91 | $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
|
---|
| 92 |
|
---|
| 93 | # Setup cached_dir and file_root
|
---|
| 94 |
|
---|
| 95 | my ($file_root, $dirname, $suffix)
|
---|
| 96 | = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
|
---|
| 97 |
|
---|
| 98 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
[20001] | 99 | $collect_dir =~ s/\\/\//g; # Work in Unix style world
|
---|
| 100 |
|
---|
| 101 | # if dirname is in collections tmp area, remove collect_dir prefix
|
---|
| 102 | $dirname =~ s/^$collect_dir//;
|
---|
| 103 |
|
---|
| 104 | if ($ENV{'GSDLOS'} eq "windows") {
|
---|
| 105 | # if dirname starts with Windows drive letter, strip it off
|
---|
| 106 | $dirname =~ s/^[a-z]:\///i;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[16826] | 109 | my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
|
---|
| 110 |
|
---|
| 111 | if (!-e $base_output_dir ) {
|
---|
| 112 | print $outhandle "Creating directory $base_output_dir\n"
|
---|
| 113 | if ($verbosity>2);
|
---|
| 114 |
|
---|
| 115 | &util::mk_all_dir($base_output_dir);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | my $output_dir = &util::filename_cat($base_output_dir,$file_root);
|
---|
| 119 |
|
---|
| 120 | if (!-e $output_dir) {
|
---|
| 121 | print $outhandle "Creating directory $output_dir\n"
|
---|
| 122 | if ($verbosity>2);
|
---|
| 123 |
|
---|
| 124 | &util::mk_dir($output_dir);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | $self->{'cached_dir'} = $output_dir;
|
---|
[20001] | 128 | $self->{'cached_file_root'} = $file_root;
|
---|
[16826] | 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | sub run_general_cmd
|
---|
| 134 | {
|
---|
| 135 | my $self = shift @_;
|
---|
| 136 | my ($command,$print_info) = @_;
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 140 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 144 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | return &convertutil::run_general_cmd(@_);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | sub regenerate_general_cmd
|
---|
| 153 | {
|
---|
| 154 | my $self = shift @_;
|
---|
[22431] | 155 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 156 |
|
---|
| 157 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 158 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 162 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return &convertutil::regenerate_general_cmd(@_);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 |
|
---|
[18555] | 170 | sub run_uncached_general_cmd
|
---|
| 171 | {
|
---|
| 172 | my $self = shift @_;
|
---|
| 173 |
|
---|
[22431] | 174 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[18555] | 175 |
|
---|
| 176 | return $self->run_general_cmd($command,$print_info);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
[16826] | 181 | sub run_cached_general_cmd
|
---|
| 182 | {
|
---|
| 183 | my $self = shift @_;
|
---|
| 184 |
|
---|
[22431] | 185 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 186 |
|
---|
| 187 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 188 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 192 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return &convertutil::run_cached_general_cmd(@_);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | sub autorun_general_cmd
|
---|
| 201 | {
|
---|
| 202 | my $self = shift @_;
|
---|
| 203 |
|
---|
[22431] | 204 | my ($command,$ifilename,$ofilename,$print_info) = @_;
|
---|
[16826] | 205 |
|
---|
| 206 | my $result;
|
---|
| 207 | my $regenerated;
|
---|
| 208 | my $had_error;
|
---|
| 209 |
|
---|
[18555] | 210 | if ($self->{'enable_cache'}) {
|
---|
[16826] | 211 | ($regenerated,$result,$had_error)
|
---|
[22431] | 212 | = $self->run_cached_general_cmd($command,$ifilename,$ofilename,$print_info);
|
---|
[16826] | 213 | }
|
---|
| 214 | else {
|
---|
| 215 | $regenerated = 1; # always true for a command that is always run
|
---|
| 216 | ($result,$had_error)
|
---|
| 217 | = $self->run_general_cmd($command,$print_info);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | return ($regenerated,$result,$had_error);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | #
|
---|
| 225 | 1;
|
---|