/application/library/Tgx/Ffmpeg.php

https://github.com/sandboxdigital/zephyr-core · PHP · 124 lines · 42 code · 19 blank · 63 comment · 2 complexity · 7cbeea736d93a83ffa9b4a1ee796af4c MD5 · raw file

  1. <?php
  2. class Tgx_Ffmpeg
  3. {
  4. public static $output;
  5. /*
  6. * // get video width and height - 10-14-07
  7. // for testing call with: ffmpeg_get_width_height.php?file=video.flv
  8. $videofile = (isset($_GET['file'])) ? strval($_GET['file']) : 'video.flv';
  9. ob_start();
  10. passthru("ffmpeg-10141.exe -i \"". $videofile . "\" 2>&1");
  11. $size = ob_get_contents();
  12. ob_end_clean();
  13. preg_match('/(\d{2,4})x(\d{2,4})/', $size, $matches);
  14. $width = $matches[1];
  15. $height = $matches[2];
  16. print " Size: " . $size . "<br />\n";
  17. print " Width: " . $width . "<br />\n";
  18. print "Height: " . $height . "<br />\n";
  19. ?>
  20. private static function _ffmpeg_exec($cmd)
  21. {
  22. self::$output = 'start';
  23. self::$output .= '1' . "\n";
  24. $handle = popen($cmd, 'w');
  25. self::$output .= "'$handle'; " . gettype($handle) . "\n";
  26. $read = fread($handle, 2096);
  27. self::$output .= $read;
  28. pclose($handle);
  29. return ;
  30. }//fn ffmpeg_exec
  31. */
  32. private static function _ffmpeg_proc($cmd)
  33. {
  34. self::$output = 'start'."\n";
  35. $descriptorspec = array(
  36. 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  37. 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  38. 2 => array("pipe", "a") // stderr is a file to write to
  39. );
  40. $cwd = '/tmp';
  41. $env = array('some_option' => 'aeiou');
  42. self::$output = $cmd."\n";
  43. $process = proc_open($cmd, $descriptorspec, $pipes, null, null);
  44. if (is_resource($process)) {
  45. // fwrite($pipes[0], '<?php print_r($_ENV); ?'.'>');
  46. // fclose($pipes[0]);
  47. echo stream_get_contents($pipes[1]);
  48. fclose($pipes[1]);
  49. // $stderr = '';
  50. // while(!feof($pipes[2])) {
  51. // $stderr .= fgets($pipes[2], 128);
  52. // }
  53. // echo $stderr;
  54. // fclose($pipes[2]);
  55. echo stream_get_contents($pipes[2]);
  56. fclose($pipes[2]);
  57. // It is important that you close any pipes before calling
  58. // proc_close in order to avoid a deadlock
  59. $return_value = proc_close($process);
  60. self::$output .= "command returned $return_value\n";
  61. return $return_value;
  62. }
  63. }
  64. public static function convertToFLV($inVideo, $outVideo, $options = array())
  65. {
  66. /*
  67. * -sameq means keep the same quality
  68. -acodec libmp3lame means use the lame MP3 library for the audio codec
  69. -ar 22050 means use an audio sampling rate of 22050Hz
  70. -ab 96000 means use an audio bit rate of 96000bps
  71. -deinterlace means deinterlace the video
  72. -nr 500 means use noise reduction of 500
  73. -s 320x240 means make a video that is 320 pixels wide and 240 pixels high
  74. -aspect 4:3 means maintain an aspect ratio (width:height) of 4:3
  75. -r 20 means maintain a frame rate of 20fps
  76. -g 500 means use a group of pictures of 500 (keyframes every 500 frames or 25 seconds at 20fps)
  77. -me_range 20 means limit motion vectors range to 20
  78. -b 270k means use a video bitrate of 270000bps
  79. -f flv means output into an FLV container file
  80. -y means overwrite if the ouput file already exists
  81. */
  82. $frameRate = 20;
  83. $keyFrameRate = $frameRate * 5;
  84. $arg = "ffmpeg -i $inVideo -acodec libmp3lame -ar 22050 -ab 96000 -sameq -nr 500 -s 720x406 -r $frameRate -g $keyFrameRate -me_range 20 -b 300k -f flv -y $outVideo";
  85. $exec = self::_ffmpeg_proc($arg);
  86. // echo $outVideo;
  87. // echo '<br />';
  88. // echo file_exists($outVideo);
  89. if(file_exists($outVideo))
  90. {
  91. return true;
  92. }
  93. else
  94. {
  95. return false;
  96. }
  97. }
  98. }