PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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