kaltura /package/app/app/alpha/apps/kaltura/lib/conversion/kConversionHelper.class.php

Language PHP Lines 205
MD5 Hash 199ffc816d518f0fdfc6fcbae2dd50f2 Estimated Cost $3,480 (why?)
Repository https://bitbucket.org/pandaos/kaltura.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
 * Will hold severa helper functions common both to the client-side and the server side
 * 
 * @package Core
 * @subpackage Conversion
 * @deprecated
 */
class kConversionHelper
{
	const INDICATOR_SUFFIX = ".indicator";
	const INPROC_SUFFIX = ".inproc";
	
	public static function getExclusiveFile ( $path , $process_id = 1 , $log_number_of_files = false )
	{
		$indicators = glob ( $path . "/*" . self::INDICATOR_SUFFIX  );
		$count = count ( $indicators );
		if ( $count > 0 || $log_number_of_files )
		{
			TRACE ( "[" . $count . "] indicator in directory [" . $path . "]" );
		}
		
		if ( $indicators == null || count ( $indicators ) == 0 ) return null;
		
		foreach ( $indicators as $indicator )
		{
			$new_indicator = $indicator . "-{$process_id}";

			$move_res = @rename ( $indicator, $new_indicator );
			// only one server will actually move the indicator ... 
			if ( $move_res )
			{
				$file = str_replace ( kConversionCommand::INDICATOR_SUFFIX , ""  , $indicator );
				$file_name = basename ( $file );
				// now remove the indicator 
				//unlink( $new_indicator );
				// move to in-proc
				$in_proc = self::inProcFromIndicator ( $indicator );
				@rename ( $new_indicator ,  $in_proc );

				return array ( $file , $file_name , $in_proc );
			}
			else
			{
				TRACE ( "[$indicator] grabbed by other process");
			}
			
			// keep on trying ...
		}
		
		return null;
	}
		
	public static function inProcFromIndicator ( $full_file_path )
	{
		return str_replace ( self::INDICATOR_SUFFIX , self::INPROC_SUFFIX, $full_file_path );
	}

	public static function removeInProc ( $in_proc )
	{
		@unlink( $in_proc );
	}
	
	public static function createFileIndicator ( $full_file_path )
	{
		$path = $full_file_path . self::INDICATOR_SUFFIX ;
		if ( file_exists( $path ))
		{
			$content = file_get_contents( $path ); // sync - OK
			if ( is_numeric( $content ) )
				$content++;
			else
				$content = 1;
			file_put_contents( $path , $content ); // sync - OK
		}
		else
		{
			touch( $path );
			$content = "";
		}	
		return array ( $path , $content );	
	}
	
	public static function isFlv ( $full_file_path )
	{
		return myFlvStaticHandler::isFlv( $full_file_path );
	}
	
	
	public static function getFlvDuration ( $full_file_path )
	{
		return myFileConverter::getFlvDuration( $full_file_path );	
	}
	
	// will return an array  ( $width , $height )
	public static function getVideoDimensions ( $full_file_path )
	{
		return myFileConverter::getVideoDimensions( $full_file_path );	
	}

	// will return an array ( $found_video , $found_audio )
	public static function fileHasVideoAndAudio ( $full_file_path )
	{
		$audio = $video = true;
		$audio_video_status = myFileConverter::videoAudioStatus ( $full_file_path );
		if ( $audio_video_status == myFileConverter::VIDEO_ONLY ) $audio = false;
		elseif ( $audio_video_status == myFileConverter::AUDIO_ONLY ) $video = false;

		return array ( $video , $audio );
	}
	
	// return the full_file_path as if it was FLV
	public static function flvFileName ( $full_file_path )
	{
		$full_path = kFile::getFileNameNoExtension( $full_file_path , true ) . ".flv";
		return $full_path;
	}
	
	/*
	 * Will fill missing params from the conv_params according to data from the source_file
	 * 1. will check if the file is audio only
	 * 2. will check if the file is video only
	 * 3. will calculate the heigth from the width and the size of the source video depending on the aspect_radio 
	 */
	public static function fillConversionParams ( $source_file , kConversionParams $conv_params )
	{
		// check to see if audio or video should be set
		if ( $conv_params->audio === null || $conv_params->video === null )
		{
			list ( $video , $audio ) = self::fileHasVideoAndAudio ( $source_file );
			if ( $conv_params->audio === null ) $conv_params->audio = $audio;
			if ( $conv_params->video === null ) $conv_params->video = $video;
		}
		
		// if should use "aspect_ratio" but have no width - act as if "original_size" 
		if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_DIMENSIONS || 
			 ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_RATIO &&  $conv_params->width == 0 ) 
			)
		{
			$conv_params->width = -1;
			$conv_params->height = -1;
			//list ( $conv_params->width , $conv_params->height ) = self::getVideoDimensions ( $source_file );
			// in this case we don't even need to pass the size 
			return;
		}

		if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_IGNORE )
		{
			// use the width & heigth from the params - IGNORE all the external requests 
		}
		elseif ( $conv_params->aspect_ratio == "" && $conv_params->height > 0 && $conv_params->width > 0 )
		{
			// leave untouched 
		}
		// if the aspect_ratio implies to keep the height - see if the height is 0 or not... 
		elseif ( $conv_params->height == 0 || 
			( 	$conv_params->aspect_ratio != kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_HEIGHT &&  
				$conv_params->aspect_ratio != kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_DIMENSIONS )
			) // can be empty , null or 0
		{
			if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_RATIO )
			{
				list ( $video_width , $video_height ) = self::getVideoDimensions ( $source_file );
				
				if ( $video_height != 0 ) 
					$conv_params->height = self::calcHeight ( $conv_params->width , $video_width / $video_height );
				else
					$conv_params->height = 0;
			}
			elseif ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_16_9 )
			{
				$conv_params->height = self::calcHeight ( $conv_params->width , ( 16/9 ) );
			}
		
			else //( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_4_3 )
			{
				// default is CONV_PARAMS_ASPECT_RATIO_4_3
				$conv_params->height = self::calcHeight ( $conv_params->width , ( 4/3 ) );
			}			
		}
		
		// make sure the width and heigth are even numbers
		if ( $conv_params->width % 2 == 1 ) $conv_params->width = $conv_params->width+1;
		if ( $conv_params->height % 2 == 1 ) $conv_params->height = $conv_params->height+1;
		
		// if by the end of all the calculations - still 0 or smaller - set to hard-coded defaults...
		if ( $conv_params->width <= 0 ) $conv_params->width = 400;
		if ( $conv_params->height <= 0 ) $conv_params->height = 300;
	}
	
	
	// TODO - extract all the data that might be required for fixing the prams
	protected static function getSourceInfo ( $source_file )
	{
			
	}
	
	protected static function calcHeight ( $width , $aspect_ratio )
	{
		if ( $aspect_ratio == 0 ) return $width;
		return  (int)( $width / $aspect_ratio );
	}
	
}
?>
Back to Top