thinkphp6 使用FFMpeg获取视频信息

1.本地安装 FFMpeg,官网下载地址:https://ffmpeg.org/download.html#build-windows

 

 解压后,把文件夹放到自定义目录,添加系统变量

 

2.安装依赖,composer.json 添加

"php-ffmpeg/php-ffmpeg": "^0.19.0",

3.封装class类

<?php
namespace app\api\controller;

use FFMpeg\FFMpeg;
use think\facade\Config;
use FFMpeg\Coordinate\TimeCode;

/**
 * FFMpeg处理音视频
 */
class Media extends Common
{
    /**
     * 获取视频时长 单位:秒
     * @param string $oriPath
     * @return string
     */
    public function videoDuration(string $oriPath=''): string
    {
        $config = [
            'ffmpeg.binaries' => Config::get('app.app_ffmpeg'),
            'ffprobe.binaries' => Config::get('app.app_ffprobe')
        ];
        $ffmpeg = FFMpeg::create($config);
        $video = $ffmpeg->open($oriPath);

        $duration = $video->getStreams()->first()->get('duration');

        return bcdiv($duration,1,0);
    }

    /**
     * 获取视频封面图(视频第一帧)
     * @param string $oriPath
     * @return string
     */
    public function videoCoverImage(string $oriPath=''): string
    {
        try {
            $config = [
                'ffmpeg.binaries' => Config::get('app.app_ffmpeg'),
                'ffprobe.binaries' => Config::get('app.app_ffprobe')
            ];
            $ffmpeg = FFMpeg::create($config);
            $video = $ffmpeg->open($oriPath);

            //获取视频第一帧
            $frame = $video->frame(TimeCode::fromSeconds(1));
            $imgDir = $this->imgPath . '/' . date('Ymd');
            if(!is_dir('.'.$imgDir)){
                mkdir('.'.$imgDir, 0755, true);
            }
            //保存图片
            $imgPath = $imgDir . '/' . buildFileName().'.jpg';
            $frame->save('.'.$imgPath);
            echo $imgPath;die;
            $cos = new Cos();
            $res = $cos->uploadStaticFile($imgPath,'.'.$imgPath);
            if($res['msg']!='success'){
                return '';
            }
            unlink('.'.$imgPath);

            return $imgPath;
        }catch (\Exception $e){
            return '';
        }
    }

    /**
     * 获取音视频基本信息
     */
    public function getVideoInfo($file)
    {
        $command = sprintf('F:\work\ffmpeg-7.0.1\bin\ffmpeg -i "%s" 2>&1', $file);  //你的ffmpeg路径

        ob_start();
        passthru($command);
        $info = ob_get_contents();
        ob_end_clean();

        $data = array();
        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
            $data['duration'] = $match[1]; //播放时间
            $arr_duration = explode(':', $match[1]);
            $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
            $data['start'] = $match[2]; //开始时间
            $data['bitrate'] = $match[3]; //码率(kb)
        }
        if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
            $data['vcodec'] = $match[1]; //视频编码格式
            $data['vformat'] = $match[2]; //视频格式
            $data['resolution'] = $match[3]; //视频分辨率
            $arr_resolution = explode('x', $match[3]);
            $data['width'] = $arr_resolution[0];
            @$data['height'] = $arr_resolution[1];
        }
        if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
            $data['acodec'] = $match[1]; //音频编码
            $data['asamplerate'] = $match[2]; //音频采样频率
        }
        if (isset($data['seconds']) && isset($data['start'])) {
            $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
        }
        $data['size'] = filesize($file); //文件大小
        return $data;
    }
}

4.本地测试

 public function testVideo()
    {
        $path = base_path()."/test.mp4";
        $media = new Media();
//        $img_url = $media->videoCoverImage($path);
        $video_len = $media->videoDuration($path);
//        $videoInfo = $media->getVideoInfo($path);
        dump($video_len . '秒');
    }

获取视频时长:

 

posted @ 2024-07-03 11:14  流浪2024  阅读(93)  评论(0编辑  收藏  举报