博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

php ffmpeg 视频返回某秒画面以及视频剪辑保存

/**

* description: 返回视频裁剪 选择秒的画面
* create by: 八月情
* create time: 2021-01-15 15:30
* environment: localhost
*/

复制代码
    public function actionLookoppic()
    {
        $id = Yii::$app->request->get('id', null);
        $duration_time = Yii::$app->request->get('duration_time', null);
        $model = $this->findModel($id);
        $ffmpeg = FFMpeg::create(array(
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',//linux 安装ffmpeg的位置 可以通过 which ffmpeg 进行查看
            'ffprobe.binaries' => '/usr//bin/ffprobe',//linux 安装ffprobe的位置 可以通过 which ffprobe 进行查看
            'timeout' => 3600, // The timeout for the underlying process
            'ffmpeg.threads' => 12,   // The number of threads that FFMpeg should use
        ), $logger);

        $video = $ffmpeg->open($model->base_url);

        $video_file = $model->base_url;
        $ffprobe_prep = FFProbe::create([
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',//linux 安装ffmpeg的位置 可以通过 which ffmpeg 进行查看
            'ffprobe.binaries' => '/usr//bin/ffprobe',//linux 安装ffprobe的位置 可以通过 which ffprobe 进行查看
            ]);
        $first = $ffprobe_prep->streams($video_file)->videos()->first();
        $time = $first->get('duration', null);//获取视频长度
        $start = $first->get('start', null);//接受视频的时间
        $start_time = gmstrftime('%H:%M:%S', $start);//将长度转换为秒数
        $frame = $video->frame(TimeCode::fromSeconds($start_time)); //提取第几秒的图像
        $frame->save($model->id.'image.jpg');
        move_uploaded_file(
            $_SERVER['DOCUMENT_ROOT']."/backend/".$model->id."image.jpg",
            $_SERVER['DOCUMENT_ROOT']."/attachment/ffmpeg_images/");
            unlink($_SERVER['DOCUMENT_ROOT']."/backend/".$model->id."image.jpg");
        return ResultHelper::json(1, 'ok', ['base_url' => Yii::$app->request->hostInfo . "/attachment/ffmpeg_images/".$model->id."image.jpg"]);
    }
复制代码

/**
* description: 裁剪视频
* create by: 八月情
* create time: 2021-01-15 17:42
* environment: localhost
*/

复制代码
public function actionShearedit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);

        $ffmpeg = FFMpeg::create(array(
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',
            'ffprobe.binaries' => '/usr//bin/ffprobe',
            'timeout' => 3600, // The timeout for the underlying process
            'ffmpeg.threads' => 12,   // The number of threads that FFMpeg should use
        ), $logger);

        $video = $ffmpeg->open($model->base_url);

        $video_file = $model->base_url;

        $ffprobe_prep = FFProbe::create([
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',
            'ffprobe.binaries' => '/usr//bin/ffprobe',
            ]);
        $first = $ffprobe_prep->streams($video_file)->videos()->first();
         $time = $first->get('duration');//获取视频长度
        $start = Yii::$app->request->get('startTime', null);//开始时间
        $end = Yii::$app->request->get('endTime', null);//结束时间
        $start_time = gmstrftime('%H:%M:%S', $start);//开始时间
        $process_time = $end - $start;//持续时间
//        $shell = 'ffmpeg -ss  00:00:02 -i ./asdf.mkv -to 01:20:30 -c copy ./asdf.mkv'; 标准格式

preg_match("/^(https:\/\/)?([^\/]+)/i", $video_file, $matches);

        $shell = 'ffmpeg -ss  ' . $start_time . ' -i $video_file -to ' . $process_time . ' -c copy '.$_SERVER['DOCUMENT_ROOT'].str_replace($matches[0],'',$video_file);
       
        shell_exec($shell);

        return 1;
    }
复制代码

 

posted @   八月情  阅读(277)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示