###########################################################################
#
# MediainfoOGVPlugin.pm -- Plugin for OGV multimedia files
#
# A component of the Greenstone digital library software from the New
# Zealand Digital Library Project at the University of Waikato, New
# Zealand.
#
# Copyright (C) 2001 Gordon W. Paynter
# Copyright (C) 2001 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

# MediainfoOGVPlugin - a plugin for OGV multimedia files

# This is a simple Plugin for importing OGV multimedia files.
# Mediainfo will retrieve the metadata of the file. A fictional document will
# be created for every such file, and the file itself will be passed
# to Greenstone as the "associated file" of the document.

# Here's an example where it is useful: I have a collection of
# ogg movie files with names like conference_20080402.ogv.
# I add this line to the collection configuration file:

# plugin MediainfoOGVPlugin -process_exp "*.ogv" -assoc_field "movie"

# A document is created for each movie, with the associated movie
# file's name in the "movie" metadata field.

# Te plugin also add some metadata :
# Duration (in seconds), filesize, title, artist, location, organization,
# date, contact, copyright.



package MediainfoOGVPlugin;

use BasePlugin;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa
no strict 'subs';

sub BEGIN {
    @MediainfoOGVPlugin::ISA = ('BasePlugin');
}

my $arguments  =  [ { 'name' => "assoc_field",
	'desc' => "{MediainfoOGVPlugin.assoc_field}",
	'type' => "string",
	'deft' => "movie",
	'reqd' => "no" },
      { 'name' => "file_format",
	'desc' => "{MediainfoOGVPlugin.file_format}",
	'type' => "string",
	'deft' => "ogv",
	'reqd' => "no" },
      { 'name' => "mime_type",
	'desc' => "{MediainfoOGVPlugin.mime_type}",
	'type' => "string",
	'deft' => "video/ogg",
	'reqd' => "no" },
      { 'name' => "srcicon",
	'desc' => "{MediainfoOGVPlugin.srcicon}",
	'type' => "string",
	'deft' => "iconogg",
	'reqd' => "no" },
      { 'name' => "process_extension",
	'desc' => "{MediainfoOGVPlugin.process_extension}",
	'type' => "string",
	'deft' => "ogv",
	'reqd' => "no" } ];

my $options = { 'name'     => "MediainfoOGVPlugin",
		'desc'     => "{MediainfoOGVPlugin.desc}",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };


sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
    push(@{$hashArgOptLists->{"OptList"}},$options);

    my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);

    # "-process_extension" is a simpler alternative to -process_exp for non-regexp people
    if (!$self->{'process_exp'} && $self->{'process_extension'}) {
	$self->{'process_exp'} = "\\." . $self->{'process_extension'} . "\$";
    }

    # Check that Mediainfo is installed and available on the path (except for Windows 95/98)
    if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
	my $result = `mediainfo 2>&1`;
	if ($? == -1 || $? == 256) {  # Linux and Windows return different values for "program not found"
	    $self->{'mediainfo_not_installed'} = 1;
	}
    }

    return bless $self, $class;
}


# MediainfoOGVPlugin processing of doc_obj.

sub process {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    #print STDERR "\n\n**** START of processing MediainfoOGVPlugin\n\n";

    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
    my $outhandle = $self->{'outhandle'};
    my $verbosity = $self->{'verbosity'};

    # check the filename is okay - do we need this??
    if ($filename_full_path eq "" || $filename_no_path eq "") {
        print $outhandle "UnknownPlugin: couldn't process \"$filename_no_path\"\n";
        return undef;
    }

    # Retrieve the file metadata

    my $command = "mediainfo --inform=\"General;%Duration/String2%|%Duration%|%FileSize/String2%|%Performer%|%Title%|%Recorded_Date%|%Recorded/Location%|%Producer%|%Copyright%|%Publisher%\" \"$filename_full_path\"";
    print $outhandle "$command\n" if ($verbosity > 2);

    # execute the mediainfo command and store the output of execution in $videodata
    my $video_metadata = `$command`;
    print $outhandle "$video_metadata\n" if ($verbosity > 2);

    # There are 10 fields separated by |, split these into ordered, individual-named variables
    my ($formattedduration,$duration,$mfilesize,$artist,$title,$date,$location,$organization,$copyright,$contact) = split(/\|/,$video_metadata);
    $duration = int($duration/1000);
    $artist =~ s/\\047/\'/g; # Perl's way of doing: $artist = `echo $artist | sed \"s/\\047/'/g\"`;
    $title =~ s/\\047/\'/g; # $title = `echo $title | sed \"s/\\047/'/g\"`;

    #print STDERR "**** RESULT = $formattedduration\n$duration\n$mfilesize\n$artist\n$title\n$date\n$location\n$organization\n$copyright\n$contact\n";
    print $outhandle "RESULT = $formattedduration\n$duration\n$mfilesize\n$artist\n$title\n$date\n$location\n$organization\n$copyright\n$contact\n" if ($verbosity > 2);
    

    # Add the file as an associated file ...
    my $section = $doc_obj->get_top_section();
    my $file_format = $self->{'file_format'} || "ogv";
    my $mime_type = $self->{'mime_type'} || "video/ogg";
    my $assoc_field = $self->{'assoc_field'} || "movie";

    # The assocfilename is the url-encoded version of the utf8 filename
    my $assoc_file = $doc_obj->get_assocfile_from_sourcefile();

    $doc_obj->associate_file($filename_full_path, $assoc_file, $mime_type, $section);
    $doc_obj->add_metadata ($section, "FileFormat", $file_format);
    $doc_obj->add_metadata ($section, "dc.Format", $file_format);
    $doc_obj->add_metadata ($section, "MimeType", $mime_type);
    $doc_obj->add_utf8_metadata ($section, $assoc_field, $doc_obj->get_source()); # Source metadata is already in utf8
 
    $doc_obj->add_metadata ($section, "FormattedDuration", $formattedduration);
    $doc_obj->add_metadata ($section, "Duration", $duration);
 
    $doc_obj->add_metadata ($section, "srclink",
                            "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[SourceFile]\">");
    $doc_obj->add_metadata ($section, "srcicon", "_".$self->{'srcicon'}."_");
    $doc_obj->add_metadata ($section, "/srclink", "</a>");

    $doc_obj->add_metadata($section,"mFileSize",$mfilesize);
    $doc_obj->add_utf8_metadata($section,"dc.Creator",$artist);
    $doc_obj->add_utf8_metadata($section,"Artist",$artist);
    $doc_obj->add_utf8_metadata($section,"dc.Title",$title);
    $doc_obj->add_utf8_metadata($section,"Title",$title);
    $doc_obj->add_utf8_metadata($section,"dc.Date",$date);
    $doc_obj->add_utf8_metadata($section,"Date",$date);
    $doc_obj->add_utf8_metadata($section,"Location",$location);
    $doc_obj->add_utf8_metadata($section,"dc.Publisher",$organization);
    $doc_obj->add_utf8_metadata($section,"Organization",$organization);
    $doc_obj->add_utf8_metadata($section,"Copyright",$copyright);
    $doc_obj->add_utf8_metadata($section,"Contact",$contact);
 
    # we have no text - add dummy text and NoText metadata
    $self->add_dummy_text($doc_obj, $section);

#    print STDERR "\n\n**** END of MediainfoOGVPlugin\n\n";
    
    return 1;
}


1;











