###########################################################################
#
# VideoConverter - helper plugin that does video (and audio) conversion using ffmpeg
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2008 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.
#
###########################################################################
package VideoConverter;

use MultimediaConverter;


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

use gsprintf 'gsprintf';

BEGIN {
    @VideoConverter::ISA = ('MultimediaConverter');
}


my @thumb_arguments = (       
      { 'name' => "create_thumbnail",
	'desc' => "{ImageConverter.create_thumbnail}",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
		   {'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "true",
	'reqd' => "no" },
	      { 'name' => "keep_original_video",
	'desc' => "Copy the original video file in the collection as an associated file.",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "Include the original video file in the collection"},
		   {'name' => "false", 'desc' => "Do not copy the original video file in the collection"}],
	'deft' => "false",
	'reqd' => "no" },
      { 'name' => "thumbnailsize",
	'desc' => "{ImageConverter.thumbnailsize}",
	'type' => "int",
	'deft' => "100",
	'range' => "1,",
	'reqd' => "no" },
      { 'name' => "thumbnailtype",
	'desc' => "{ImageConverter.thumbnailtype}",
	'type' => "string",
	'deft' => "jpg",
	'reqd' => "no" },
      { 'name' => "noscaleup",
	'desc' => "{ImageConverter.noscaleup}",
	'type' => "flag",
	'reqd' => "no" } );


my @screenview_arguments = (
      { 'name' => "create_screenview",
	'desc' => "{ImageConverter.create_screenview}",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
		   {'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "true",
	'reqd' => "no" },
      { 'name' => "screenviewsize",
	'desc' => "{ImageConverter.screenviewsize}",
	'type' => "int",
	'deft' => "720",
	'range' => "1,",
	'reqd' => "no" },
      { 'name' => "screenviewtype",
	'desc' => "{ImageConverter.screenviewtype}",
	'type' => "string",
	'deft' => "jpg",
	'reqd' => "no" } );

my $arguments = [
      @thumb_arguments,
      @screenview_arguments,

      { 'name' => "converttotype",
	'desc' => "{ImageConverter.converttotype}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no" },


      { 'name' => "converttosize",
	'desc' => "{VideoPlugin.converttosize}",
	'type' => "int",
	'deft' => "",
##	'deft' => "352",
	'reqd' => "no" },
      { 'name' => "converttobitrate",
	'desc' => "{VideoPlugin.converttobitrate}",
	'type' => "string",
	'deft' => "200k",
	'reqd' => "no" },
	
      { 'name' => "create_keyframes",
	'desc' => "{VideoPlugin.extractkeyframes}",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
			{'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "false",
	'reqd' => "no" },
	
	{ 'name' => "keyframesize",
	'desc' => "{ImageConverter.keyframesize}",
	'type' => "int",
	'deft' => "520",
	'range' => "1,",
	'reqd' => "no" },
	
	   { 'name' => "keyframes_algorithm",
	'desc' => "{VideoPlugin.keyframesAlgorithm}",
	'type' => "enum",
	'list' => [{'name' => "mtn", 'desc' => "{keyframesAlgorithm.mtn}"},
		{'name' => "ffkeyframe", 'desc' => "{keyframesAlgorithm.ffkeyframe}"}],
	'deft' => "mtn",
	'reqd' => "no" },
	
      { 'name' => "ffkeyframe_num_shots",
	'desc' => "{VideoPlugin.keep_keyframes}",
	'type' => "string",
	'deft' => "all",
	'reqd' => "no" },
	
	 { 'name' => "mtn_skip_intro",
	'desc' => "{VideoPlugin.mtnSkipIntro}",
	'type' => "int",
	'deft' => "0",
	'reqd' => "no" },
	 { 'name' => "mtn_skip_end",
	'desc' => "{VideoPlugin.mtnSkipEnd}",
	'type' => "int",
	'deft' => "0",
	'reqd' => "no" },
	 { 'name' => "mtn_timestep",
	'desc' => "{VideoPlugin.mtnTimestep}",
	'type' => "int",
	'deft' => "120",
	'reqd' => "no" },
	 { 'name' => "mtn_detect_blanks",
	'desc' => "{VideoPlugin.mtnDetectBlanks}",
	'type' => "int",
	'deft' => "80",
	'range' => "0,100",
	'reqd' => "no" },
#	{ 'name' => "mtn_detect_sharpness",
#	'desc' => "{VideoPlugin.mtnDetectSharpness}",
#	'type' => "enum",
#	[{'name' => "off", 'desc' => "{mtnDetectSharpness.off}"},
#	{'name' => "low", 'desc' => "{mtnDetectSharpness.low}"},
#	{'name' => "medium", 'desc' => "{mtnDetectSharpness.medium}"},
#	{'name' => "high", 'desc' => "{mtnDetectSharpness.high}"},
#	{'name' => "highest", 'desc' => "{mtnDetectSharpness.highest}"}],
#	'deft' => "highest",
#	'reqd' => "no" },	
	
	{ 'name' => "create_montage",
	'desc' => "{VideoPlugin.createMontage}",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
			{'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "false",
	'reqd' => "no" },
	
      { 'name' => "streamingsize",
	'desc' => "{VideoPlugin.streamingsize}",
	'type' => "int",
	'deft' => "352",
	'reqd' => "no" },
      { 'name' => "streamingbitrate",
	'desc' => "{VideoPlugin.streamingbitrate}",
	'type' => "string",
	'deft' => "200k",
	'reqd' => "no" },
	{ 'name' => "streamingHQsize",
	'desc' => "{VideoPlugin.streamingsize}",
	'type' => "int",
	'deft' => "720",
	'reqd' => "no" },
	{ 'name' => "streamingHQVideoBitrate",
	'desc' => "{VideoPlugin.streamingbitrate}",
	'type' => "int",
	'deft' => "496",
	'reqd' => "no" },
	{ 'name' => "streamingHQAudioBitrate",
	'desc' => "{VideoPlugin.streamingbitrate}",
	'type' => "int",
	'deft' => "80",
	'reqd' => "no" },
	{ 'name' => "videoDeinterlacingFilter",
	'desc' => "Activate a deinterlacing filter to increase the quality of TV footage",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
		   {'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "false",
	'reqd' => "no" },
	{ 'name' => "getMaximumMetadata",
	'desc' => "Extract the maximum technical information of each video",
	'type' => "enum",
	'list' => [{'name' => "true", 'desc' => "{common.true}"},
		   {'name' => "false", 'desc' => "{common.false}"}],
	'deft' => "false",
	'reqd' => "no" },

      { 'name' => "minimumsize",
	'desc' => "{ImageConverter.minimumsize}",
	'type' => "int",
	'deft' => "100",
	'range' => "1,",
	'reqd' => "no" },

		 ];

my $options = { 'name' => "VideoConverter",
		'desc' => "{VideoConverter.desc}",
		'abstract' => "yes",
		'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 MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);


    return bless $self, $class;

}


#  Here we use 'MediaInfo' for video and ask for a specific specification
sub mediaInfo_Inform_Cmd {
	my ($video, $parameter, $outhandle, $verbosity) = @_;
	
	# Use MediaInfo CLI to get the file spec according to the requested parameter
    my $command = "mediainfo --Inform=\"$parameter\" \"$video\"";

    print $outhandle "  $command\n" if ($verbosity > 2);
    my $result = '';
    $result = `$command 2>&1`;
	chomp $result;
    print $outhandle "  $result\n" if ($verbosity > 4);
	
	# Return the asked spec
    return ($result);
}

# Here we use mediaInfo to get all the possible metadata in XML form that could then be parsed
# the result will vary depending on the file type and how it was encoded
sub mediaInfo_XML_Cmd {
    my ($video, $outhandle, $verbosity) = @_;
	
    # Use MediaInfo CLI to get the file spec according to the requested parameter
    my $command = "mediainfo --Output=XML \"$video\"";
    
    print $outhandle "  $command\n" if ($verbosity > 2);
    my $result = '';
    $result = `$command 2>&1`;
    print $outhandle "  $result\n" if ($verbosity > 4);
    
	# Return the asked spec
    return ($result);
}

# Here we parse the video specs contained in the XML text in order to create a metadata entry for each field
# that mediainfo is giving back, this can vary from media file to media file.
sub mediaInfo_parse_XML {
    my ($xmlTxt, $outhandle, $verbosity) = @_;
    
    my @parts = split(/<track\s+type=\"(.*?)\">/is,$xmlTxt);
    
    shift @parts; # Skip preamble
    my $metadata_table = {};
    
    while (@parts) {
	my $type = shift(@parts);
	my $vals = shift(@parts);
	my @fieldlist=();
	while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) {
	    my $metaname = $1;
	    my $metaval  = $2;
	    my $fullmetaname = "mi.$type\^$metaname";
	    $metadata_table->{$fullmetaname}= $metaval;
	    push(@fieldlist,$fullmetaname);
	}
	
	$metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist);
    }
    
    return $metadata_table;

	while (@parts) {
		my $type = shift(@parts);
		my $vals = shift(@parts);
		my @fieldlist=();
		while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) {
		  my $metaname = $1;
		  my $metaval  = $2;
		  my $fullmetaname = "mi.$type\^$metaname";
		  if($metaname ne "Complete_name") #ignore this field as it is pointing to the cache directory
			  {
			  $metadata_table->{$fullmetaname}= $metaval;
			  push(@fieldlist,$fullmetaname);
			  }
		}
		$metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist);
	}
	return $metadata_table;

}

sub identifyMI { 
    my ($video, $outhandle, $verbosity) = @_;

    # Use the MediaInfo command to get the file specs
    my $command = "mediainfo \"$video\"";

    print $outhandle "  $command\n" if ($verbosity > 2);
    my $result = '';
    $result = `$command 2>&1`;
    print $outhandle "  $result\n" if ($verbosity > 4);

    # Read the type, width, and height etc. from media file
	# This could be done in a more efficient way by only calling the application MediaInfo once and
	# giving all the parameters inside a special macro file then parsing the results, however
	# on most operating system when the application is called once it is then cached for subsequent calls.
	# General info
    my $vtype			= mediaInfo_Inform_Cmd($video,"General;%Format%",$outhandle,0); #Container of the video e.g. MPEG-TS
	my $duration		= mediaInfo_Inform_Cmd($video,"General;%Duration%",$outhandle,0); #Duration of the video in ms e.g. 5545841
	my $durationDisplay	= mediaInfo_Inform_Cmd($video,"General;%Duration/String%",$outhandle,0); #Duration of the video in minutes and secs or hours and minutes e.g. 25m 12s or 2h 26m
	my $filesize		= mediaInfo_Inform_Cmd($video,"General;%FileSize%",$outhandle,0); #File size in bytes e.g. 1024
	my $filesizeDisplay	= mediaInfo_Inform_Cmd($video,"General;%FileSize/String%",$outhandle,0); #File size with measure e.g. 25.6 MiB
	my $orateDisplay	= mediaInfo_Inform_Cmd($video,"General;%OverallBitRate/String%",$outhandle,0); #General file bitrate with measure e.g. 7 538 Kbps
	# Video info
    my $vcodec			= mediaInfo_Inform_Cmd($video,"Video;%Format%",$outhandle,0); #Codec used for the video compression e.g. AVC
	my $vrate			= mediaInfo_Inform_Cmd($video,"Video;%BitRate%",$outhandle,0); #Bitrate used for the video stream in bps e.g. 546165
    my $width			= mediaInfo_Inform_Cmd($video,"Video;%Width%",$outhandle,0); #Width of the video in pixels e.g. 1920
    my $height			= mediaInfo_Inform_Cmd($video,"Video;%Height%",$outhandle,0); #Height of the video in pixels e.g. 1080
    my $fps				= mediaInfo_Inform_Cmd($video,"Video;%FrameRate%",$outhandle,0); #Video frames per second e.g. 30
	# Audio info
    my $acodec			= mediaInfo_Inform_Cmd($video,"Audio;%Format%",$outhandle,0); # Audio codec used for the compression e.g. AAC
    my $afreq			= mediaInfo_Inform_Cmd($video,"Audio;%SamplingRate/String%",$outhandle,0); #Sampling rate used for audio encoding e.g. 48000
    my $achan			= mediaInfo_Inform_Cmd($video,"Audio;%Channel(s)%",$outhandle,0); #Number of channels e.g. 2
    my $arate			= mediaInfo_Inform_Cmd($video,"Audio;%BitRate%",$outhandle,0); #Audio bitrate for the audio stream in bps e.g. 65436
	my $atracks			= mediaInfo_Inform_Cmd($video,"Audio;%StreamCount%",$outhandle,0); #number of audio tracks in this video file e.g. 1
	# Subtitles info
	my $stype			= mediaInfo_Inform_Cmd($video,"Text;%Format%",$outhandle,0); # Format used for the subtitles e.g. DVB Subtitle
	my $slang			= mediaInfo_Inform_Cmd($video,"Text;%Language/String%",$outhandle,0); # Language used for the subtitles e.g. English
		

	my $xmlTxt =  mediaInfo_XML_Cmd ($video,$outhandle,0);
	my $metadata_table = mediaInfo_parse_XML($xmlTxt,$outhandle,0);
	
    # Return the specs
	my $identify_vals = { "vtype" => $vtype, "width" => $width, "height" => $height, "duration" => $duration, "filesize" => $filesize, "vcodec" => $vcodec,
	"fps" => $fps, "acodec" => $acodec, "afreq" => $afreq, "achan" => $achan, "arate" => $arate, "metadata_table" => $metadata_table, "durationDisplay" => $durationDisplay,
	"filesizeDisplay" => $filesizeDisplay, "orateDisplay" => $orateDisplay, "vrate" => $vrate, "stype" => $stype, "slang" => $slang, "atracks" => $atracks	};

	return $identify_vals;
}

# Here we use 'ffmpeg' for video and ask for a specific specification
# This is now deprecated this will be replaced by identifyMI in the future
sub identify { 
    my ($video, $outhandle, $verbosity) = @_;

    # Use the ffmpeg command to get the file specs
    my $command = "ffmpeg -i \"$video\"";

    print $outhandle "  $command\n" if ($verbosity > 2);
    my $result = '';
    $result = `$command 2>&1`;
    print $outhandle "  $result\n" if ($verbosity > 4);

    # Read the type, width, and height etc.
    my $vtype =  'unknown';
    my $vcodec = 'unknown';
    my $width =  'unknown';
    my $height = 'unknown';
    my $fps    = 'unknown';

    my $atype =  'unknown';
    my $afreq =  'unknown';
    my $achan =  'unknown';
    my $arate =  'unknown';

    my $video_safe = quotemeta $video;

    # strip off everything up to filename
    $result =~ s/^.*\'$video_safe\'://s;

##    if ($result =~ m/Video: (.*?) fps/m) {
    if ($result =~ m/^\s+Stream.*?Video: (.*?)$/m) {
	my $video_info = $1;
	$video_info =~ s/\s*\[.*?\]//g;

	my @video_fields = split(/,\s*/,$video_info);
	
	$vtype = shift @video_fields;

	if ($video_fields[0] !~ m/(\d+)x(\d+)/) {
	    $vcodec = shift @video_fields;
	}
	my $video_dim = shift @video_fields;

	($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/);
	
	if ($video_fields[0] =~ m/(\d+)\s+tbr$/) {
	    $fps = $1;
	}

#	if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/)
#	{
#	    $vtype = $1;
#	    $vcodec = $2 if defined $2;
#	    $width = $3;
#	    $height = $4;
#	    $fps = $5;
#	}
    }

    if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) {
	$atype = $1;
	$afreq = $2;
	$achan = $3;
	$arate = $4 if (defined $4);
    }

    # Read the duration
    my $duration = "unknown";
    if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) {
	$duration = $1;
    }
    print $outhandle "  file: $video:\t $vtype, $width, $height, $duration\n" 
	if ($verbosity > 2);

    if ($verbosity >3) {
	print $outhandle "\t video codec=$vcodec, fps = $fps\n";
	print $outhandle "\t audio codec=$atype, freq = $afreq Hz, $achan, $arate\n";
    }

	
	my $identify_vals = { "vtype" => $vtype, "width" => $width, "height" => $height, "duration" => $duration, "filesize" => -s $video, "vcodec" => $vcodec,
	"fps" => $fps, "acodec" => $atype, "afreq" => $afreq, "achan" => $achan, "arate" => $arate };

	return $identify_vals;
	
    # Return the specs
    return ($vtype, $width, $height, $duration, -s $video,
	    $vcodec,$fps,$atype,$afreq,$achan,$arate);
}


sub vob_durations
{
    my ($media_base_dir,$title_num,$outhandle) = @_;

    my $filter_re = sprintf("^VTS_%02d_[1-9]\\.VOB\$",$title_num);

    my $duration_info = {};

    if (opendir(VTSIN,$media_base_dir)) {
	my @vts_title_vobs = grep { $_ =~ m/$filter_re/ } readdir(VTSIN);
	closedir(VTSIN);

	foreach my $v (@vts_title_vobs) {
	    my $full_v = &util::filename_cat($media_base_dir,$v);

	    my $identify_hash = identifyMI($full_v,$outhandle,0);
		
		my $duration = $identify_hash->{'duration'};

	    my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/);

	    $duration_info->{$vob_num} = $duration;
	    print STDERR "**** $title_num: $title_num, storing {$vob_num} => $duration\n";

	}

    }
    else {
	print $outhandle "Warning: unable to read files in directory $media_base_dir\n";
    }
    
    return $duration_info;

}



sub init_cache_for_file {
    my $self = shift(@_);

    my ($video_filename) = @_;

    $self->SUPER::init_cache_for_file($video_filename);

					
    my @flvtool2_monitor = ( 'monitor_init'   ,"convertutil::monitor_init_unbuffered",
			     'monitor_line'   , "VideoConverter::flvtool2_monitor_line",
			     'monitor_deinit' , "convertutil::monitor_deinit_unbuffered" );
					
    $self->{'flvtool2_monitor'} = \@flvtool2_monitor;


}



sub optional_frame_scale
{
    my $self = shift (@_);
    my ($orig_size,$video_width,$video_height) = @_;

    my $s_opt = "";

    if ($video_width > $video_height) {
	if ($video_width > $orig_size) {
	    my $scale_factor = $orig_size/$video_width;
	    my $scaled_width = int($video_width * $scale_factor);
	    my $scaled_height = int($video_height * $scale_factor);

	    # round to be ensure multiple of 2 (needed by some codecs)
	    $scaled_width  = int($scaled_width/2)*2;
	    $scaled_height = int($scaled_height/2)*2;

	    $s_opt = "-s ${scaled_width}x${scaled_height}";
	}
	# else, video is smaller than requested size, don't scale up
    }
    else {
	if ($video_height > $orig_size) {
	    my $scale_factor = $orig_size/$video_height;
	    my $scaled_width = int($video_width * $scale_factor);
	    my $scaled_height = int($video_height * $scale_factor);

	    # round to be ensure multiple of 2 (needed by some codecs)
	    $scaled_width  = int($scaled_width/2)*2;
	    $scaled_height = int($scaled_height/2)*2;

	    $s_opt = "-s ${scaled_width}x${scaled_height}";
	}
	# else, video is smaller than requested size, don't scale up
	
    }
    
    return $s_opt;
}


sub ffKeyframe_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename) = @_;

    my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video");

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $oshot_filename = &util::filename_cat($output_dir,"shots.xml");

    my $exp_duration = $self->{'exp_duration'};
    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";

    my $main_opts = "-y $t_opt";

    my $hive = &util::filename_cat($video_ext_dir,"lib","vhook","hive.so");

    my $oflash_filename = &util::filename_cat($output_dir,"$ivideo_root\_keyframe.flv");

    my $vhook_opts = "$hive  -o $oshot_filename -k $output_dir $ivideo_filename";

    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);

    my $ffmpeg_cmd = "ffkeyframe  $main_opts -vhook \"$vhook_opts\"   -i \"$ivideo_filename_gsdlenv\" -an -y \"$oflash_filename_gsdlenv\"";


    return ($ffmpeg_cmd,$oflash_filename);
}


sub mtnKeyframe_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename,$video_width,$video_height) = @_;

    my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video");

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $frames_output_dir = &util::filename_cat($output_dir,"frames");

	my $main_opts = "-z -P -I -O \"$frames_output_dir\"";
	 
	my $mtn_skip_intro_secs   = $self->{'mtn_skip_intro'};
	my $mtn_skip_end_secs     = $self->{'mtn_skip_end'};
	my $mtn_timestep_secs     = $self->{'mtn_timestep'};
	my $mtn_detect_blank_perc = $self->{'mtn_detect_blanks'};
	
	my $mtn_detect_blank = $mtn_detect_blank_perc/100.0;
	
	my $keyframe_height = $self->{'keyframesize'};
	
#	if ($self->{'create_screenview'} eq "true") {
#		$keyframe_height = $self->{'screenviewsize'};
#	}
#	else {
		# assume thumbnail size
#		$keyframe_height = $self->{'thumbnailsize'};
#	}
	
	
	
	$main_opts .= " -B $mtn_skip_intro_secs";
	$main_opts .= " -E $mtn_skip_end_secs";
	$main_opts .= " -s $mtn_timestep_secs";
	$main_opts .= " -b $mtn_detect_blank";
	$main_opts .= " -h $keyframe_height";
	
    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
 
    my $mtn_cmd = "mtn  $main_opts \"$ivideo_filename_gsdlenv\"";

    my $output_s_filename = &util::filename_cat($frames_output_dir,$ivideo_root."_s.jpg");

    return ($mtn_cmd,$output_s_filename);
}


sub get_ovideo_file
{
	my $self = shift (@_);
	my ($stream_type) = @_;
	
    my $ivideo_root = $self->{'cached_file_root'};
	my $ofile;
	
	if ($stream_type eq "flv")
	{
		$ofile = "${ivideo_root}_vstream.flv";
	}
	else
	{
		$ofile = "${ivideo_root}_vHQstream.mp4";
	}
	
	return $ofile;
}

sub get_ovideo_filename
{
	my $self = shift (@_);
	my ($stream_type) = @_;
	
	my $output_dir = $self->{'cached_dir'};

	my $ofile = $self->get_ovideo_file($stream_type);
		
	my $ofilename = &util::filename_cat($output_dir,$ofile);
	
	return $ofilename;
}

# Generate the command to use with Handbrake to recompress a video using h264/aac compatible with Flash Player 10
sub stream_mp4_video_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename,$streaming_HQ_size, $streaming_HQ_VideoBitrate,
	 $streaming_HQ_AudioBitrate) = @_;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $omp4_file = "${ivideo_root}_vHQstream.mp4";
    my $omp4_filename = &util::filename_cat($output_dir,$omp4_file);


    #my $exp_duration = $self->{'exp_duration'};
    #my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";

	my $deinterlace = $self->{'videoDeinterlacingFilter'};
	my $video_processing_parameters;	
	# Use specific resizing algorythms depending if we need to downsize the video resolution or not
	if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") { 
		$video_processing_parameters = "--strict-anamorphic";
	} else {
		$video_processing_parameters = "-w $streaming_HQ_size --loose-anamorphic";
	}
	# Use the deinterlace video filter if enabled in the plugin
	if ($deinterlace eq "true")
	{
		$video_processing_parameters .= " --decomb";
	}

    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
	
	# Generate the pre and post video parameters for Handbrake v0.9.4
	my $pre_opts = "-t 1 -c 1";
	my $post_opts = "-f mp4 -O $video_processing_parameters -e x264 -b $streaming_HQ_VideoBitrate -a 1 -E faac -6 dpl2 -R Auto -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0";

	my $handbrake_cmd = "HandBrakeCLI -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";

    #print STDERR "****\nHandbrake command: $handbrake_cmd\n****\n";
	
    return ($handbrake_cmd,$omp4_filename,$omp4_file);
}



sub stream_flv_video_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename,$video_width,$video_height,
	$streaming_quality,
	$streaming_bitrate,$streaming_size, 
	$opt_streaming_achan, $opt_streaming_arate) = @_;

    my $streaming_achan 
	= (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;

    my $streaming_arate 
	= (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $oflash_file = "${ivideo_root}_vstream.flv";
    my $oflash_filename = &util::filename_cat($output_dir,$oflash_file);

    my $s_opt = "";
    my $media_type = $self->{'media_type'};
    if ($media_type ne "audio") {
	$s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);
    }


    my $exp_duration = $self->{'exp_duration'};
    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";

    my $main_opts = "-y $t_opt";

    my $bitrate_opt = "-b $streaming_bitrate";
    ### my $stream_opts = "-r 25 $s_opt";
    my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate";

    # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'

    my $all_opts = "$main_opts $stream_opts"; 

    my $ffmpeg_cmd;

    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);

    if ($streaming_quality eq "high") {

	my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt");
	if (-e $pass_log_file) {
	    &util::rm($pass_log_file);
	}

	my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file);

	$all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\"";

	my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";

	my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\"";
	$ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )";
    }
    else {
	# single pass

	$ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
    }

    return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
}




sub stream_flv_audio_cmd
{
    # AudioConverter also has a routine for doing this
    # => merge into one!
    # **************

    my $self = shift (@_);
    my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_;

    # Convert either audio or video to streamable audio format

    my $streaming_achan 
	= (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;

    my $streaming_arate 
	= (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $ofla_file = "${ivideo_root}_astream.flv";
    my $ofla_filename = &util::filename_cat($output_dir,$ofla_file);

    my $exp_duration = $self->{'exp_duration'};
    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";

    my $main_opts = "-vn -y $t_opt";

    my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";

    my $all_opts = "$main_opts $stream_opts"; 

    my $ffmpeg_cmd;

    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename);
    $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\"";

    return ($ffmpeg_cmd,$ofla_filename,$ofla_file);
}




sub audio_excerpt_cmd
{
    my $self = shift (@_);
    my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_;

    # ivoa = input video or audio

    my $time_encoded = "$hh:$mm:$ss";
    my $time_encoded_file = "$hh$mm$ss";
    
    
    my $output_dir = $self->{'cached_dir'};
    my $ivoa_root = $self->{'cached_file_root'};

    my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3";
    my $omp3_filename = &util::filename_cat($output_dir,$omp3_file);

    my $all_opts = "-y -acodec mp3 -ss $time_encoded ";

    if (defined $opt_excerpt_len) {
	$all_opts .= "-t $opt_excerpt_len ";
    }


    my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename);
    my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename);


    my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts  \"$omp3_filename_gsdlenv\"";

    return ($ffmpeg_cmd,$omp3_filename,$omp3_file);
}



sub streamseekable_cmd
{
    my $self = shift (@_);
    my ($oflash_filename) = @_;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    ## my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");

    my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\"";

    return ($flvtool_cmd,$oflash_filename);
}


sub streamkeyframes_cmd
{
    my $self = shift (@_);
    my ($oflash_filename,$doc_obj,$section) = @_;

    my $assocfilepath 
	= $doc_obj->get_metadata_element($section,"assocfilepath");

    my $output_dir = $self->{'cached_dir'};

    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
 
    my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\"  \"$oflash_filename\"";

    return ($flvtool_cmd,$oflash_filename);
}


sub streamkeyframes_cmd_old
{
    my $self = shift (@_);
    my ($oflash_filename,$doc_obj,$section) = @_;

    my $assocfilepath 
	= $doc_obj->get_metadata_element($section,"assocfilepath");

    my $output_dir = $self->{'cached_dir'};

    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");

    my $video_server = $ENV{'GEXT_VIDEO_SERVER'}; 
    my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};

    my $collect = $ENV{'GSDLCOLLECTION'};
 
    print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n";

    my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\"";


    return ($flvtool_cmd,$oflash_filename);
}


sub streamcuepts_cmd
{
    my $self = shift (@_);
    my ($oflash_filename) = @_;

    my $output_dir = $self->{'cached_dir'};

    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");

    ##my $video_server = $ENV{'GEXT_VIDEO_SERVER'}; 
    ##my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};

    ## my $collect = $ENV{'GSDLCOLLECTION'};

    ## my $thumbloc = "$video_server$video_prefix/collect/$collect";


#    my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\"";

#    my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";



#    my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";


##    my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\"";

##    my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\"";

    my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\"";

    return ($flvtool_cmd,$oflash_filename);
}


sub keyframe_thumbnail_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight,$video_duration) = @_;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);


    # Try for 4th keyframe, but fall back to 1st if doesn't exist
    my $key_filename = "${key_filename_prefix}_0003.jpg";
    $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename);

    my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename);
    my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile);

    my $command;

    if (-e $key_filename) {
	$command = "convert -interlace plane -verbose -geometry $thumbnailwidth" 
	    . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\"";
    }
    else {
	# create_keyframe is either false, or else had a problem when running
	# => extract a from 
	# my $frame_rate = 1.0 / 60.0;
	
	my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);

	# If ever reused, then needs to change to newer hashmap method
	#my ($vtype, $width, $height, $duration, $vsize,
	#	$vcodec ,$fps ,$atype ,$afreq ,$achan ,$arate) = identify($ivideo_filename_gsdlenv,$outhandle,0);
	
	print STDERR "***** grabbing keyframe as 50% of $video_duration\n";
	my $ss_opt = ($video_duration / 1000.0) * 0.45;
	print STDERR "*** ssopt = $ss_opt\n";
	
##	$command = "ffmpeg  -ss $ss_opt -i \"$ivideo_filename_gsdlenv\" -ss 4.5 -an -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -deinterlace -y \"$thumbnailfile_gsdlenv\"";
##	$command = "ffmpeg -ss 4.5 -i \"$ivideo_filename_gsdlenv\" -ss 4.5 -an -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -deinterlace -y \"$thumbnailfile_gsdlenv\"";

##	$command = "ffmpeg -i \"$ivideo_filename_gsdlenv\"  -ss 10.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -deinterlace -y \"$thumbnailfile_gsdlenv\"";

        # *NOTE* "-ss X" should occur before "-i" if you want to seek in the
        # input file (hence making generation significantly faster). [jmt12]
	$command = "ffmpeg -ss 16.5 -i \"$ivideo_filename_gsdlenv\" -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -deinterlace -y \"$thumbnailfile_gsdlenv\"";


	# fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
    }

    return ($command,$thumbnailfile);
}




sub keyframe_montage_cmd
{
    my $self = shift (@_);
    my ($ivideo_filename,$montagefile) = @_;

    my $output_dir = $self->{'cached_dir'};
    my $ivideo_root = $self->{'cached_file_root'};

    my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);

    my $options = "-tile 10  -geometry 75x62+2+2";

    my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\"";

    return ($command,$montagefile);
}



sub parse_shot_xml
{
    my ($self) = shift(@_);

    my $outhandle = $self->{'outhandle'};
    my $output_dir = $self->{'cached_dir'};

    my $shots_filename = &util::filename_cat($output_dir,"shots.xml");

    eval {
	$self->{'parser'}->parsefile($shots_filename);
    };
    
    if ($@) {
	print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1);
	print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2);
	return 0;
    }

}


sub parse_shot_dir
{
    my ($self) = shift(@_);
    
    my $outhandle = $self->{'outhandle'};
    my $output_dir = $self->{'cached_dir'};
    
    my $ivideo_root  = $self->{'cached_file_root'};
    
    my $frames_output_dir = &util::filename_cat($output_dir,"frames");
    
    my @jpeg_frames = ();
    
    if (opendir(FDIRIN, $frames_output_dir)) {
	@jpeg_frames = grep { $_ =~ /_\d+\.jpg$/ } readdir(FDIRIN);
	closedir(FDIRIN);
    }
    else {
	print STDERR "VideoConverter:: Failed to open directory $frames_output_dir:\n$!\n";
	return 0;
    }
    
    $self->{'keyframe_timeline'} = {};
    
    my $vfps = $self->{'video-fps'};
    
    my $keyframe_index = 1;
    
    foreach my $f (@jpeg_frames) {
	my ($hh,$mm,$ss) = ($f =~ m/_(\d{2})_(\d{2})_(\d{2})_\d+\.jpg$/);
	my $frame_num = (3600*$hh + 60*$mm + $ss) * $vfps;
	
	my $time_msec = (3600*$hh + 60*$mm + $ss) * 1000;
	
	my $timeline_rec = { 'name'=> "Keyframe $keyframe_index",
			     'keyframeindex' => $keyframe_index,
			     'timestamp' => $time_msec,
			     'thumb' => &util::filename_cat("frames",$f),
			     'keyframenum' => $keyframe_index };
	
	$self->{'keyframe_timeline'}->{$keyframe_index}=$timeline_rec;
	
	$keyframe_index++;
    }
	
    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
    
    $self->open_on_cue($cue_filename);

    $self->output_distributed_keyframes($self->{'keyframe_timeline'},$self->{'ffkeyframe_num_shots'});

    $self->close_on_cue();
	
    return 1;
}

sub associate_keyframes_old
{
    my ($self) = shift(@_);

    my ($doc_obj,$section) = @_;

    my $output_dir = $self->{'cached_dir'};

    my $count = 1;
    foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) {

	my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
	$doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
				 $section);
	$count++;
    }

    $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));


    # *****
    # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});

}

sub associate_keyframes
{
    my ($self) = shift(@_);

    my ($doc_obj,$topsection,$timeline) = @_;

    my $output_dir = $self->{'cached_dir'};
    
    my $count = 1;

##    print STDERR "**** timeline keys = ", join("; ", keys %$timeline), "\n";
    foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline)
    {
	my $kframe_file = $timeline->{$t}->{'thumb'};
	my $timestamp = $timeline->{$t}->{'timestamp'};

	# create next sub-section to video "document"
	my $endchild = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
	$doc_obj->add_utf8_metadata($endchild,"Title","Timestamp $timestamp");
	$doc_obj->add_utf8_metadata($endchild,"FrameNum",$t);

	my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
	$doc_obj->associate_file($kframe_filename,"keyframe$count.jpg",
				 "image/jpeg",
				 $endchild);

	$doc_obj->add_utf8_metadata($endchild,"assockeyframe","keyframe$count.jpg");

	$doc_obj->add_utf8_metadata($topsection,"KeyframeTimestamp",$timestamp);
	$doc_obj->add_utf8_metadata($topsection,"KeyframeFrameNum",$t);



	$count++;
    }

    #### $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));

    $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(keys %$timeline));


    # *****
    # $doc_obj->add_metadata ($topsection, "thumblist", $self->{'flowplayer_thumblist'});
}



sub enable_audio_streaming
{
    my $self = shift (@_);
    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_;

    my $section = $doc_obj->get_top_section();

    my $output_dir   = $self->{'cached_dir'};
    my $ivideo_root  = $self->{'cached_file_root'};
    
    # Generate FLV audio-only format for streaming purposes

    my $optionally_run_general_cmd = "run_uncached_general_cmd";
    if ($self->{'enable_cache'}) {
	$optionally_run_general_cmd
	    = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
    }

    
    my $ifilename = $originalfilename || $filename;

    my ($stream_cmd,$ofla_filename,$ofla_file) 
	= $self->stream_flv_audio_cmd($ifilename);
    
    
    my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
			       'message_prefix' => "Stream",
			       'message' => "Generating streamable audio: $ofla_file" }; 
    

    my ($streamable_regenerated,$streamable_result,$streamable_had_error)
	= $self->$optionally_run_general_cmd($stream_cmd,
					     $ifilename,$ofla_filename,
					     $streamable_options);


    if (!$streamable_had_error) {
	my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename);
	
	my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
				       'message_prefix' => "Stream Seekable",
				       'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" }; 
	
	if ($streamable_regenerated) {
	    $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
	}
    
	my $streamable_url = $ofla_file;
	my $streamable_url_safe = $self->url_safe($streamable_url);

	$doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
	$doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash",
			     $section);
    }

    $doc_obj->add_metadata ($section, "audioflashwidth",    400);
    $doc_obj->add_metadata ($section, "audioflashheight",   22 + 100);

    $self->{'ofla_file'} = $ofla_file;
    $self->{'ofla_filename'} = $ofla_filename;

    return $streamable_regenerated;
}





sub enable_video_streaming
{
    my $self = shift (@_);
    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
	$video_width,$video_height) = @_;

    my $section = $doc_obj->get_top_section();

    my $output_dir   = $self->{'cached_dir'};
    my $ivideo_root  = $self->{'cached_file_root'};
    
    # Generate the Flash FLV format for streaming purposes

    my $optionally_run_general_cmd = "run_uncached_general_cmd";
    if ($self->{'enable_cache'}) {
	$optionally_run_general_cmd
	    = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
    }


    my $streaming_bitrate = $self->{'streamingbitrate'};
    my $streaming_size    = $self->{'streamingsize'};
    
    my $streaming_quality = "high";
    
    my $ifilename = $originalfilename || $filename;

    my ($stream_cmd,$oflash_filename,$oflash_file) 
	= $self->stream_flv_video_cmd($ifilename,
				     $video_width,$video_height,
				     $streaming_quality,
				     $streaming_bitrate, $streaming_size);
    
    
    my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
			       'message_prefix' => "Stream",
			       'message' => "Generating streamable video: $oflash_file" }; 
    

    my ($streamable_regenerated,$streamable_result,$streamable_had_error)
	= $self->$optionally_run_general_cmd($stream_cmd,
					     $ifilename,$oflash_filename,
					     $streamable_options);
    
    if (!$streamable_had_error) {
	my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
	
	my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
				       'message_prefix' => "Stream Seekable",
				       'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" }; 
	
	if ($streamable_regenerated) {
	    $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
	}
	
	my $streamable_url = $oflash_file;
	my $streamable_url_safe = $self->url_safe($streamable_url);
	
	$doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
	$doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash",
				 $section);
    }


    #
    # FlowPlayer.swf       height+22 pixels
    # FlowPlayerBlack.swf  height+16 pixels
    # FlowPlayerThermo.swf height+16 pixels
    # FlowPlayerWhite.swf  height+26 pixels

    my $flashwidth = $video_width;
    my $flashheight = $video_height + 22;

    if ($self->{'create_keyframes'} eq "true") {
	$flashheight += 100;
    }

    $doc_obj->add_metadata ($section, "flashwidth",    $flashwidth);
    $doc_obj->add_metadata ($section, "flashheight",   $flashheight);
     
    $self->{'oflash_file'} = $oflash_file;
    $self->{'oflash_filename'} = $oflash_filename;

    return $streamable_regenerated;
}

sub enable_direct_streaming
{
    my $self = shift (@_);
    my ($doc_obj,$originalfilename,$filename,$file,
	$video_width,$video_height) = @_;

    my $section = $doc_obj->get_top_section();
    
    my $ifilename = $originalfilename || $filename;
	#my $ifile = $self->gsdlhome_independent($ifilename);
	my $ifile = $file;

	print STDERR "Using the original video file for direct streaming: $ifile\n";

	my $streamable_url = $ifile;
	my $streamable_url_safe = $self->url_safe($streamable_url);
	
	my $outhandle = $self->{'outhandle'};

	my $identify_vals = identifyMI($ifilename,$outhandle,0);

	#Add the most common metadata extracted by MediaInfo from the streamable video file
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $identify_vals->{'vtype'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $identify_vals->{'filesizeDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSizeBytes", $identify_vals->{'filesize'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $identify_vals->{'width'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $identify_vals->{'height'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $identify_vals->{'durationDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDurationMs", $identify_vals->{'duration'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $identify_vals->{'fps'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $identify_vals->{'vcodec'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $identify_vals->{'vrate'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $identify_vals->{'acodec'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $identify_vals->{'arate'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioTracks", $identify_vals->{'atracks'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingOverallBitrate", $identify_vals->{'orateDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleType", $identify_vals->{'stype'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleLanguage", $identify_vals->{'slang'});
	
	my $get_max_metadata = $self->{'getMaximumMetadata'};
	if ($get_max_metadata eq "true"){
		my $metadata_table = $identify_vals->{'metadata_table'};
		foreach my $metaname(keys %$metadata_table)
		{
			my $metaval = $identify_vals->{'metadata_table'}->{$metaname};
			$doc_obj->add_utf8_metadata ($section, $metaname, $metaval);
		}
	}
	
	#add the metadata for the resulting file that should be open inside the replayme video player
	$doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
	$doc_obj->associate_file($ifilename,$ifile,"application/octet-stream",$section);

    my $flashwidth = $video_width;
    my $flashheight = $video_height + 22;

    if ($self->{'create_keyframes'} eq "true") {
	$flashheight += 100;
    }

    $doc_obj->add_metadata ($section, "flashwidth",    $flashwidth);
    $doc_obj->add_metadata ($section, "flashheight",   $flashheight);
     
    $self->{'oflash_file'} = $ifile;
    $self->{'oflash_filename'} = $ifilename;

}


sub enable_h264_streaming
{
    my $self = shift (@_);
    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
	$video_width,$video_height) = @_;
    

    my $section = $doc_obj->get_top_section();

    my $output_dir   = $self->{'cached_dir'};
    my $ivideo_root  = $self->{'cached_file_root'};
    
    # Generate the H264 video file format for streaming purposes using the cache feature if used

    my $optionally_run_general_cmd = "run_uncached_general_cmd";
    if ($self->{'enable_cache'}) {
	$optionally_run_general_cmd
	    = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
    }

	my $streaming_HQ_size    = $self->{'streamingHQsize'};
	my $streaming_HQ_VideoBitrate    = $self->{'streamingHQVideoBitrate'};
	my $streaming_HQ_AudioBitrate    = $self->{'streamingHQAudioBitrate'};

    my $ifilename = $originalfilename || $filename;
    my ($stream_cmd,$oflash_filename,$oflash_file) 
	= $self->stream_mp4_video_cmd($ifilename,
				     $streaming_HQ_size,
				     $streaming_HQ_VideoBitrate,
				     $streaming_HQ_AudioBitrate);
    
    
    my $streamable_options = { @{$self->{'handbrake_monitor'}},
			       'message_prefix' => "MP4 Stream",
			       'message' => "Generating streamable MP4 video: $oflash_file" }; 
    

    my ($streamable_regenerated,$streamable_result,$streamable_had_error)
	= $self->$optionally_run_general_cmd($stream_cmd,
					     $ifilename,$oflash_filename,
					     $streamable_options);
    
    if (!$streamable_had_error) {

	
	my $streamable_url = $oflash_file;
	my $streamable_url_safe = $self->url_safe($streamable_url);
	
	my $outhandle = $self->{'outhandle'};

	my $identify_vals = identifyMI($oflash_filename,$outhandle,0);
	
	#Add the most common metadata extracted by MediaInfo from the streamable video file
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $identify_vals->{'vtype'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $identify_vals->{'filesizeDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $identify_vals->{'width'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $identify_vals->{'height'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $identify_vals->{'durationDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $identify_vals->{'fps'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $identify_vals->{'vcodec'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $identify_vals->{'vrate'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $identify_vals->{'acodec'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $identify_vals->{'arate'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingOverallBitrate", $identify_vals->{'orateDisplay'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleType", $identify_vals->{'stype'});
	$doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleLangue", $identify_vals->{'slang'});
	
	my $get_max_metadata = $self->{'getMaximumMetadata'};
	if ($get_max_metadata eq "true"){
		my $metadata_table = $identify_vals->{'metadata_table'};
		foreach my $metaname(keys %$metadata_table)
		{
			my $metaval = $identify_vals->{'metadata_table'}->{$metaname};
			$doc_obj->add_utf8_metadata ($section, $metaname, $metaval);
		}
	}
	
	#add the metadata for the resulting file that should be open inside the flash video player
	$doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
	$doc_obj->associate_file($oflash_filename,$oflash_file,"video/mp4",
				 $section);
    }

    my $flashwidth = $video_width;
    my $flashheight = $video_height + 22;

    if ($self->{'create_keyframes'} eq "true") {
	$flashheight += 100;
    }

    $doc_obj->add_metadata ($section, "flashwidth",    $flashwidth);
    $doc_obj->add_metadata ($section, "flashheight",   $flashheight);
     
    $self->{'oflash_file'} = $oflash_file;
    $self->{'oflash_filename'} = $oflash_filename;

    return $streamable_regenerated;
	
	
}

sub enable_full_streaming
{
    my $self = shift (@_);
    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
	$video_width,$video_height) = @_;

    my $video_streamable_regenerated
	= $self->enable_video_streaming($doc_obj,$originalfilename,$filename,
					$convertto_regenerated,
					$video_width,$video_height);

    my $audio_streamable_regenerated
	= $self->enable_audio_streaming($doc_obj,$originalfilename,$filename,
					$convertto_regenerated);

    return ($video_streamable_regenerated || $audio_streamable_regenerated);
}



sub flvtool2_monitor_line
{
    my ($line) = @_;

    my $had_error = 0;
    my $generate_dot = 1;

    if ($line =~ m/\s+\- /) {
	# ignore tabulated output printed at end of command
	$generate_dot = 0;
    }
    
    if ($line =~ m/^Error:/i) {
	$had_error = 1;
    }

    return ($had_error,$generate_dot);
}





1;
