[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 |
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | sub init_cache_for_file
|
---|
| 65 | {
|
---|
| 66 | my $self = shift @_;
|
---|
| 67 | my ($filename) = @_;
|
---|
| 68 |
|
---|
| 69 | my $verbosity = $self->{'verbosity'};
|
---|
| 70 | my $outhandle = $self->{'outhandle'};
|
---|
| 71 | my $base_dir = $self->{'base_dir'};
|
---|
| 72 |
|
---|
| 73 | # Work out relative filename within 'base_dir'
|
---|
| 74 | my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
|
---|
| 75 |
|
---|
| 76 | $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
|
---|
| 77 |
|
---|
| 78 | # Setup cached_dir and file_root
|
---|
| 79 |
|
---|
| 80 | my ($file_root, $dirname, $suffix)
|
---|
| 81 | = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
|
---|
| 82 |
|
---|
| 83 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 84 | my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
|
---|
| 85 |
|
---|
| 86 | if (!-e $base_output_dir ) {
|
---|
| 87 | print $outhandle "Creating directory $base_output_dir\n"
|
---|
| 88 | if ($verbosity>2);
|
---|
| 89 |
|
---|
| 90 | &util::mk_all_dir($base_output_dir);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | my $output_dir = &util::filename_cat($base_output_dir,$file_root);
|
---|
| 94 |
|
---|
| 95 | if (!-e $output_dir) {
|
---|
| 96 | print $outhandle "Creating directory $output_dir\n"
|
---|
| 97 | if ($verbosity>2);
|
---|
| 98 |
|
---|
| 99 | &util::mk_dir($output_dir);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | $self->{'cached_dir'} = $output_dir;
|
---|
| 103 | $self->{'cached_file_root'} = $file_root;
|
---|
| 104 |
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | sub run_general_cmd
|
---|
| 110 | {
|
---|
| 111 | my $self = shift @_;
|
---|
| 112 | my ($command,$print_info) = @_;
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 116 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 120 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | return &convertutil::run_general_cmd(@_);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | sub regenerate_general_cmd
|
---|
| 129 | {
|
---|
| 130 | my $self = shift @_;
|
---|
| 131 | my ($command,$ofilename,$print_info) = @_;
|
---|
| 132 |
|
---|
| 133 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 134 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 138 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return &convertutil::regenerate_general_cmd(@_);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | sub run_cached_general_cmd
|
---|
| 147 | {
|
---|
| 148 | my $self = shift @_;
|
---|
| 149 |
|
---|
| 150 | my ($command,$ofilename,$print_info) = @_;
|
---|
| 151 |
|
---|
| 152 | if (!defined $print_info->{'verbosity'}) {
|
---|
| 153 | $print_info->{'verbosity'} = $self->{'verbosity'};
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if (!defined $print_info->{'outhandle'}) {
|
---|
| 157 | $print_info->{'outhandle'} = $self->{'outhandle'};
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return &convertutil::run_cached_general_cmd(@_);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | sub autorun_general_cmd
|
---|
| 166 | {
|
---|
| 167 | my $self = shift @_;
|
---|
| 168 |
|
---|
| 169 | my ($command,$ofilename,$print_info) = @_;
|
---|
| 170 |
|
---|
| 171 | my $result;
|
---|
| 172 | my $regenerated;
|
---|
| 173 | my $had_error;
|
---|
| 174 |
|
---|
| 175 | if ($self->{'cache_generated_images'}) {
|
---|
| 176 | ($regenerated,$result,$had_error)
|
---|
| 177 | = $self->run_cached_general_cmd($command,$ofilename,$print_info);
|
---|
| 178 | }
|
---|
| 179 | else {
|
---|
| 180 | $regenerated = 1; # always true for a command that is always run
|
---|
| 181 | ($result,$had_error)
|
---|
| 182 | = $self->run_general_cmd($command,$print_info);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return ($regenerated,$result,$had_error);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | #
|
---|
| 190 | 1;
|
---|