FfmpegHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace common\helpers;
  3. /**
  4. * Class FfmpegHelper
  5. * @package common\helpers
  6. * @author jianyan74 <751393839@qq.com>
  7. */
  8. class FfmpegHelper
  9. {
  10. /**
  11. * ffmege 启动
  12. *
  13. * 例如:/user/local/bin/ffmpeg
  14. *
  15. * 注意后面的空格
  16. * @var string
  17. */
  18. private static $ffmpegPath = '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 . "-ss {$second} -i {$filePath} -r 1 -vframes 1 -an -f mjpeg {$imagePath} 1>/dev/null");
  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. ob_start();
  64. passthru(sprintf(self::$ffmpegPath . '-i "%s" 2>&1', $file));
  65. $videoInfo = ob_get_contents();
  66. ob_end_clean();
  67. // 使用输出缓冲,获取ffmpeg所有输出内容
  68. $result = [];
  69. // Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
  70. if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $videoInfo, $matches)) {
  71. $result['duration'] = $matches[1]; // 视频长度
  72. $duration = explode(':', $matches[1]);
  73. $result['seconds'] = $duration[0] * 3600 + $duration[1] * 60 + $duration[2]; // 转为秒数
  74. $result['start'] = $matches[2]; // 开始时间
  75. $result['bitrate'] = $matches[3]; // bitrate 码率 单位kb
  76. }
  77. // 格式1:Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
  78. // 格式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)
  79. if (preg_match("/Video: (.*?), (.*?), (.*?), (.*?), (.*?)[,\s]/", $videoInfo, $matches)) {
  80. $result['vcodec'] = $matches[1]; // 编码格式
  81. try {
  82. $result['vformat'] = $matches[2]; // 视频格式
  83. list($width, $height) = explode('x', $matches[3]);
  84. } catch (\Exception $e) {
  85. $result['vformat'] = $matches[2] . ', ' . $matches[3] . ', ' . $matches[4]; // 视频格式
  86. $result['resolution'] = $matches[5]; // 分辨率
  87. list($width, $height) = explode('x', $matches[5]);
  88. }
  89. $result['width'] = $width;
  90. $result['height'] = $height;
  91. }
  92. // Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
  93. if (preg_match("/Audio: (.*), (\d*) Hz/", $videoInfo, $matches)) {
  94. $result['acodec'] = $matches[1]; // 音频编码
  95. $result['asamplerate'] = $matches[2]; // 音频采样频率
  96. }
  97. if (isset($result['seconds']) && isset($result['start'])) {
  98. $result['play_time'] = $result['seconds'] + $result['start']; // 实际播放时间
  99. }
  100. $result['size'] = filesize($file); // 视频文件大小
  101. // 基本信息
  102. // $videoInfo = iconv('gbk','utf8', $videoInfo);
  103. return $result;
  104. }
  105. }
粤ICP备19079148号