PageRenderTime 99ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 16ms

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

https://bitbucket.org/pandaos/kaltura
PHP | 205 lines | 67 code | 12 blank | 126 comment | 34 complexity | 199ffc816d518f0fdfc6fcbae2dd50f2 MD5 | raw file
  1<?php
  2/**
  3 * Will hold severa helper functions common both to the client-side and the server side
  4 * 
  5 * @package Core
  6 * @subpackage Conversion
  7 * @deprecated
  8 */
  9class kConversionHelper
 10{
 11	const INDICATOR_SUFFIX = ".indicator";
 12	const INPROC_SUFFIX = ".inproc";
 13	
 14	public static function getExclusiveFile ( $path , $process_id = 1 , $log_number_of_files = false )
 15	{
 16		$indicators = glob ( $path . "/*" . self::INDICATOR_SUFFIX  );
 17		$count = count ( $indicators );
 18		if ( $count > 0 || $log_number_of_files )
 19		{
 20			TRACE ( "[" . $count . "] indicator in directory [" . $path . "]" );
 21		}
 22		
 23		if ( $indicators == null || count ( $indicators ) == 0 ) return null;
 24		
 25		foreach ( $indicators as $indicator )
 26		{
 27			$new_indicator = $indicator . "-{$process_id}";
 28
 29			$move_res = @rename ( $indicator, $new_indicator );
 30			// only one server will actually move the indicator ... 
 31			if ( $move_res )
 32			{
 33				$file = str_replace ( kConversionCommand::INDICATOR_SUFFIX , ""  , $indicator );
 34				$file_name = basename ( $file );
 35				// now remove the indicator 
 36				//unlink( $new_indicator );
 37				// move to in-proc
 38				$in_proc = self::inProcFromIndicator ( $indicator );
 39				@rename ( $new_indicator ,  $in_proc );
 40
 41				return array ( $file , $file_name , $in_proc );
 42			}
 43			else
 44			{
 45				TRACE ( "[$indicator] grabbed by other process");
 46			}
 47			
 48			// keep on trying ...
 49		}
 50		
 51		return null;
 52	}
 53		
 54	public static function inProcFromIndicator ( $full_file_path )
 55	{
 56		return str_replace ( self::INDICATOR_SUFFIX , self::INPROC_SUFFIX, $full_file_path );
 57	}
 58
 59	public static function removeInProc ( $in_proc )
 60	{
 61		@unlink( $in_proc );
 62	}
 63	
 64	public static function createFileIndicator ( $full_file_path )
 65	{
 66		$path = $full_file_path . self::INDICATOR_SUFFIX ;
 67		if ( file_exists( $path ))
 68		{
 69			$content = file_get_contents( $path ); // sync - OK
 70			if ( is_numeric( $content ) )
 71				$content++;
 72			else
 73				$content = 1;
 74			file_put_contents( $path , $content ); // sync - OK
 75		}
 76		else
 77		{
 78			touch( $path );
 79			$content = "";
 80		}	
 81		return array ( $path , $content );	
 82	}
 83	
 84	public static function isFlv ( $full_file_path )
 85	{
 86		return myFlvStaticHandler::isFlv( $full_file_path );
 87	}
 88	
 89	
 90	public static function getFlvDuration ( $full_file_path )
 91	{
 92		return myFileConverter::getFlvDuration( $full_file_path );	
 93	}
 94	
 95	// will return an array  ( $width , $height )
 96	public static function getVideoDimensions ( $full_file_path )
 97	{
 98		return myFileConverter::getVideoDimensions( $full_file_path );	
 99	}
100
101	// will return an array ( $found_video , $found_audio )
102	public static function fileHasVideoAndAudio ( $full_file_path )
103	{
104		$audio = $video = true;
105		$audio_video_status = myFileConverter::videoAudioStatus ( $full_file_path );
106		if ( $audio_video_status == myFileConverter::VIDEO_ONLY ) $audio = false;
107		elseif ( $audio_video_status == myFileConverter::AUDIO_ONLY ) $video = false;
108
109		return array ( $video , $audio );
110	}
111	
112	// return the full_file_path as if it was FLV
113	public static function flvFileName ( $full_file_path )
114	{
115		$full_path = kFile::getFileNameNoExtension( $full_file_path , true ) . ".flv";
116		return $full_path;
117	}
118	
119	/*
120	 * Will fill missing params from the conv_params according to data from the source_file
121	 * 1. will check if the file is audio only
122	 * 2. will check if the file is video only
123	 * 3. will calculate the heigth from the width and the size of the source video depending on the aspect_radio 
124	 */
125	public static function fillConversionParams ( $source_file , kConversionParams $conv_params )
126	{
127		// check to see if audio or video should be set
128		if ( $conv_params->audio === null || $conv_params->video === null )
129		{
130			list ( $video , $audio ) = self::fileHasVideoAndAudio ( $source_file );
131			if ( $conv_params->audio === null ) $conv_params->audio = $audio;
132			if ( $conv_params->video === null ) $conv_params->video = $video;
133		}
134		
135		// if should use "aspect_ratio" but have no width - act as if "original_size" 
136		if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_DIMENSIONS || 
137			 ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_RATIO &&  $conv_params->width == 0 ) 
138			)
139		{
140			$conv_params->width = -1;
141			$conv_params->height = -1;
142			//list ( $conv_params->width , $conv_params->height ) = self::getVideoDimensions ( $source_file );
143			// in this case we don't even need to pass the size 
144			return;
145		}
146
147		if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_IGNORE )
148		{
149			// use the width & heigth from the params - IGNORE all the external requests 
150		}
151		elseif ( $conv_params->aspect_ratio == "" && $conv_params->height > 0 && $conv_params->width > 0 )
152		{
153			// leave untouched 
154		}
155		// if the aspect_ratio implies to keep the height - see if the height is 0 or not... 
156		elseif ( $conv_params->height == 0 || 
157			( 	$conv_params->aspect_ratio != kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_HEIGHT &&  
158				$conv_params->aspect_ratio != kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_DIMENSIONS )
159			) // can be empty , null or 0
160		{
161			if ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_KEEP_ORIG_RATIO )
162			{
163				list ( $video_width , $video_height ) = self::getVideoDimensions ( $source_file );
164				
165				if ( $video_height != 0 ) 
166					$conv_params->height = self::calcHeight ( $conv_params->width , $video_width / $video_height );
167				else
168					$conv_params->height = 0;
169			}
170			elseif ( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_16_9 )
171			{
172				$conv_params->height = self::calcHeight ( $conv_params->width , ( 16/9 ) );
173			}
174		
175			else //( $conv_params->aspect_ratio == kConversionParams::CONV_PARAMS_ASPECT_RATIO_4_3 )
176			{
177				// default is CONV_PARAMS_ASPECT_RATIO_4_3
178				$conv_params->height = self::calcHeight ( $conv_params->width , ( 4/3 ) );
179			}			
180		}
181		
182		// make sure the width and heigth are even numbers
183		if ( $conv_params->width % 2 == 1 ) $conv_params->width = $conv_params->width+1;
184		if ( $conv_params->height % 2 == 1 ) $conv_params->height = $conv_params->height+1;
185		
186		// if by the end of all the calculations - still 0 or smaller - set to hard-coded defaults...
187		if ( $conv_params->width <= 0 ) $conv_params->width = 400;
188		if ( $conv_params->height <= 0 ) $conv_params->height = 300;
189	}
190	
191	
192	// TODO - extract all the data that might be required for fixing the prams
193	protected static function getSourceInfo ( $source_file )
194	{
195			
196	}
197	
198	protected static function calcHeight ( $width , $aspect_ratio )
199	{
200		if ( $aspect_ratio == 0 ) return $width;
201		return  (int)( $width / $aspect_ratio );
202	}
203	
204}
205?>