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