Video.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/2/21
  6. * Time: 12:12
  7. */
  8. namespace app\admin\model;
  9. use FFMpeg\Coordinate\Dimension;
  10. use FFMpeg\Coordinate\FrameRate;
  11. use FFMpeg\Coordinate\TimeCode;
  12. use FFMpeg\FFMpeg;
  13. use FFMpeg\FFProbe;
  14. use FFMpeg\Format\Audio\Flac;
  15. use FFMpeg\Format\Video\WMV;
  16. use FFMpeg\Format\Video\X264;
  17. class Video
  18. {
  19. // private static $ffmpeg = "D:/install/ffmpeg/bin/ffmpeg.exe";
  20. private static $ffmpeg = app_paths."ffmpeg/bin/ffmpeg.exe";
  21. // private static $ffprobe = "D:/install/ffmpeg/bin/ffprobe.exe";
  22. private static $ffprobe = app_paths."ffmpeg/bin/ffprobe.exe";
  23. private static $file_img=Pah."/storage/videoimg/";
  24. //总长度时间和第2帧 图片
  25. public static function ffmpeg_getTime($file)
  26. {
  27. $ffmpeg = FFMpeg::create([
  28. 'ffmpeg.binaries' => self::$ffmpeg ,
  29. 'ffprobe.binaries' => self::$ffprobe,
  30. ]);
  31. $video = $ffmpeg ->open($file);
  32. $frame = $video->frame(TimeCode::fromSeconds(2));
  33. $time=time();
  34. $file_name=self::$file_img.$time.".png";
  35. $frame->save($file_name);
  36. $data["img"]="/storage/videoimg/".$time.".png";// 背景图片
  37. $dimension=self::video_time($file);
  38. $data["seconds"]=$dimension;
  39. return $data;
  40. }
  41. // 获取总时长
  42. public static function video_time($file) {
  43. define('FFMPEG_PATH', self::$ffmpeg.' -i "%s" 2>&1');
  44. ob_start();
  45. passthru(sprintf(FFMPEG_PATH, $file)); //passthru()类似exec()
  46. $info = ob_get_contents();
  47. ob_end_clean();
  48. // 通过使用输出缓冲,获取到ffmpeg所有输出的内容。
  49. $ret = array();
  50. // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
  51. if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
  52. // $ret['duration'] = $match[1]; // 提取出播放时间
  53. $arr_dian = explode('.', $match[1]);
  54. $arr_duration = explode(':', $arr_dian[0]);
  55. $seconds = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
  56. // $arr_duration = explode(':', $arr_dian[0]);
  57. return $seconds;
  58. }
  59. return 0;
  60. }
  61. // 获取所有参数
  62. public static function getVideoInfo($file) {
  63. define('FFMPEG_PATH', self::$ffmpeg.' -i "%s" 2>&1');
  64. $command = sprintf(FFMPEG_PATH, $file);
  65. ob_start();
  66. passthru($command);
  67. $info = ob_get_contents();
  68. ob_end_clean();
  69. $data = array();
  70. if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
  71. $data['duration'] = $match[1]; //播放时间
  72. $arr_duration = explode(':', $match[1]);
  73. $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
  74. $data['start'] = $match[2]; //开始时间
  75. $data['bitrate'] = $match[3]; //码率(kb)
  76. }
  77. if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
  78. $data['vcodec'] = $match[1]; //视频编码格式
  79. $data['vformat'] = $match[2]; //视频格式
  80. $data['resolution'] = $match[3]; //视频分辨率
  81. $arr_resolution = explode('x', $match[3]);
  82. $data['width'] = $arr_resolution[0];
  83. $data['height'] = $arr_resolution[1];
  84. }
  85. if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
  86. $data['acodec'] = $match[1]; //音频编码
  87. $data['asamplerate'] = $match[2]; //音频采样频率
  88. }
  89. if (isset($data['seconds']) && isset($data['start'])) {
  90. $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
  91. }
  92. // $data['size'] = filesize($file); //文件大小
  93. return $data;
  94. }
  95. // 视频处理
  96. public function file_con($file)
  97. {
  98. $ffmpeg = FFMpeg::create([
  99. 'ffmpeg.binaries' => self::$ffmpeg ,
  100. 'ffprobe.binaries' => self::$ffprobe,
  101. "timeout"=>3600,
  102. "ffmpeg.threads"=>12,
  103. ]);
  104. // $ffmpeg = FFMpeg::create();
  105. $video=$ffmpeg->open(Pah.$file);
  106. $this->video_size($ffmpeg,$video);
  107. }
  108. // 设置视频大小
  109. public function video_size($ffmpeg,$video)
  110. {
  111. $saves=Pah."storage/zhuan/text26.mp4";
  112. $video->filters()->resize(new Dimension(750, 400),"inset");
  113. $video->save(new X264("aac"),$saves);
  114. }
  115. // 视频转码 H264
  116. public static function mp4_video($file)
  117. {
  118. $saves=Pah."storage/zhuan/texts11.mkv";
  119. $ffmpeg = FFMpeg::create([
  120. 'ffmpeg.binaries' => self::$ffmpeg ,
  121. 'ffprobe.binaries' => self::$ffprobe,
  122. "timeout"=>3600,
  123. "ffmpeg.threads"=>12,
  124. ]);
  125. $video=$ffmpeg->open(Pah.$file);//FrameRate
  126. $video->filters()->resize(new Dimension(750, 400),"inset")->framerate(new FrameRate(10),1)->synchronize();
  127. // $format=new X264();
  128. // $format
  129. // $video->save(new WMV(),$saves);
  130. $video->save(new X264("aac"),$saves);
  131. dump(1212);
  132. }
  133. }