FfmpegHelper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace common\helpers;
  3. /**
  4. * Class FfmpegHelper
  5. * @package common\helpers
  6. * @author qimall
  7. */
  8. class FfmpegHelper
  9. {
  10. /**
  11. * ffmege 启动
  12. *
  13. * 例如:/user/local/bin/ffmpeg
  14. *
  15. * 注意后面的空格
  16. * @var string
  17. */
  18. private static $ffmpegPath = '/usr/local/ffmpeg/bin/ffmpeg ';
  19. /**
  20. * 转码
  21. *
  22. * @param $filePath
  23. * @param $fileNewPath
  24. */
  25. public static function transcoding($filePath, $fileNewPath)
  26. {
  27. exec(self::$ffmpegPath . "-i $filePath $fileNewPath");
  28. }
  29. /**
  30. * 获取视频截图
  31. *
  32. * @param string $filePath 视频文件(绝对路径)
  33. * @param string $imagePath 保存图片地址(绝对路径)
  34. * @param string $second 秒 例如 00:00:01
  35. */
  36. public static function imageResize($filePath, $imagePath, $second)
  37. {
  38. exec(self::$ffmpegPath . " -y -ss {$second} -i {$filePath} -r 1 -vframes 1 -an -f mjpeg {$imagePath} 2>&1", $output, $errcode);
  39. }
  40. /**
  41. * 获取视频信息
  42. *
  43. * Array(
  44. * [duration] => 00:02:28.63
  45. * [seconds] => 148.63
  46. * [start] => 0.000000
  47. * [bitrate] => 1606
  48. * [vcodec] => h264 (Main) (avc1 / 0x31637661)
  49. * [vformat] => yuv420p
  50. * [resolution] => 1280x720
  51. * [width] => 1280
  52. * [height] => 720
  53. * [acodec] => aac (LC) (mp4a / 0x6134706D)
  54. * [asamplerate] => 44100
  55. * [play_time] => 148.63
  56. * [size] => 29842292
  57. * )
  58. * @param $file
  59. * @resulturn array
  60. */
  61. public static function getVideoInfo($file)
  62. {
  63. exec(sprintf(self::$ffmpegPath . '-i "%s" 2>&1', $file), $output, $errcode);
  64. $videoInfo = implode(' ', $output);
  65. $result = [];
  66. // Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
  67. if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $videoInfo, $matches)) {
  68. $result['duration'] = $matches[1]; // 视频长度
  69. $duration = explode(':', $matches[1]);
  70. $result['seconds'] = $duration[0] * 3600 + $duration[1] * 60 + $duration[2]; // 转为秒数
  71. $result['start'] = $matches[2]; // 开始时间
  72. $result['bitrate'] = $matches[3]; // bitrate 码率 单位kb
  73. }
  74. // 格式1:Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
  75. // 格式2:Stream #0:1: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt709/bt709, progressive), 240x320, 475 kb/s, 29.84 fps, 29.97 tbr, 600 tbn, 1200 tbc (default)
  76. if (preg_match("/Video: (.*?), (.*?), (.*?), (.*?), (.*?)[,\s]/", $videoInfo, $matches)) {
  77. $result['vcodec'] = $matches[1]; // 编码格式
  78. if (strpos($matches[3], 'x') > 0) {
  79. $result['vformat'] = $matches[2]; // 视频格式
  80. list($width, $height) = explode('x', $matches[3]);
  81. } elseif (strpos($matches[5], 'x') > 0) {
  82. $result['vformat'] = $matches[2] . ', ' . $matches[3] . ', ' . $matches[4]; // 视频格式
  83. $result['resolution'] = $matches[5]; // 分辨率
  84. list($width, $height) = explode('x', $matches[5]);
  85. } elseif (strpos($matches[4], 'x')) {
  86. $result['vformat'] = $matches[2] . ',' . $matches[3];
  87. $result['resolution'] = $matches[5];
  88. list($width, $height) = explode('x', $matches[4]);
  89. } else {
  90. throw new \Exception('获取视频格式出错');
  91. }
  92. $result['width'] = $width;
  93. $result['height'] = $height;
  94. }
  95. // Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
  96. if (preg_match("/Audio: (.*), (\d*) Hz/", $videoInfo, $matches)) {
  97. $result['acodec'] = $matches[1]; // 音频编码
  98. $result['asamplerate'] = $matches[2]; // 音频采样频率
  99. }
  100. if (isset($result['seconds']) && isset($result['start'])) {
  101. $result['play_time'] = $result['seconds'] + $result['start']; // 实际播放时间
  102. }
  103. $result['size'] = filesize($file); // 视频文件大小
  104. // 基本信息
  105. // $videoInfo = iconv('gbk','utf8', $videoInfo);
  106. return $result;
  107. }
  108. /*
  109. public static function getVideoInfo($file)
  110. {
  111. ob_start();
  112. passthru(sprintf(self::$ffmpegPath . '-i "%s" 2>&1', $file));
  113. $videoInfo = ob_get_contents();
  114. ob_end_clean();
  115. // 使用输出缓冲,获取ffmpeg所有输出内容
  116. $result = [];
  117. // Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
  118. if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $videoInfo, $matches)) {
  119. $result['duration'] = $matches[1]; // 视频长度
  120. $duration = explode(':', $matches[1]);
  121. $result['seconds'] = $duration[0] * 3600 + $duration[1] * 60 + $duration[2]; // 转为秒数
  122. $result['start'] = $matches[2]; // 开始时间
  123. $result['bitrate'] = $matches[3]; // bitrate 码率 单位kb
  124. }
  125. // 格式1:Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
  126. // 格式2:Stream #0:1: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt709/bt709, progressive), 240x320, 475 kb/s, 29.84 fps, 29.97 tbr, 600 tbn, 1200 tbc (default)
  127. if (preg_match("/Video: (.*?), (.*?), (.*?), (.*?), (.*?)[,\s]/", $videoInfo, $matches)) {
  128. $result['vcodec'] = $matches[1]; // 编码格式
  129. try {
  130. $result['vformat'] = $matches[2]; // 视频格式
  131. list($width, $height) = explode('x', $matches[3]);
  132. } catch (\Exception $e) {
  133. $result['vformat'] = $matches[2] . ', ' . $matches[3] . ', ' . $matches[4]; // 视频格式
  134. $result['resolution'] = $matches[5]; // 分辨率
  135. list($width, $height) = explode('x', $matches[5]);
  136. }
  137. $result['width'] = $width;
  138. $result['height'] = $height;
  139. }
  140. // Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
  141. if (preg_match("/Audio: (.*), (\d*) Hz/", $videoInfo, $matches)) {
  142. $result['acodec'] = $matches[1]; // 音频编码
  143. $result['asamplerate'] = $matches[2]; // 音频采样频率
  144. }
  145. if (isset($result['seconds']) && isset($result['start'])) {
  146. $result['play_time'] = $result['seconds'] + $result['start']; // 实际播放时间
  147. }
  148. $result['size'] = filesize($file); // 视频文件大小
  149. // 基本信息
  150. // $videoInfo = iconv('gbk','utf8', $videoInfo);
  151. return $result;
  152. }
  153. */
  154. }