You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装依赖与二进制文件仍报错:FFMpeg\Format\video\X264 类未找到

问题:LaravelFFMpeg处理视频时提示找不到FFMpeg\Format\video\X264类

在Laravel项目中使用LaravelFFMpeg进行视频处理,已确认相关依赖包存在于vendor目录,容器内也安装了FFmpeg二进制文件,但仍遇到错误:

Error: Class "FFMpeg\Format\video\X264" not found

以下是PHP实现代码:

<?php

namespace App\Services;
 
use App\Http\Traits\UploadFiles;
use App\Models\OtherVideoFormat;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;

class VideoServiceSecond
{
    use UploadFiles, SetPath;

    protected $otherVideoFormat, $data;

    public function __construct(array $data, $method = 'create')
    {
        $this->otherVideoFormat = app(OtherVideoFormat::class);
        $this->data = $data;

        if ($method == 'create')
            $this->convertFormatVideoFirst();
        else
            $this->convertFormatVideoFirstUpdate();
    }

    public function convertFormatVideoFirst()
    {
        FFMpeg::openUrl($this->data['url_signed'])
            ->export()
            ->toDisk('public')
            ->inFormat(new \FFMpeg\Format\video\X264)
            ->resize(config('dimensions-videos.second-width'), config('dimensions-videos.second-height'))
            ->save('video-resize-' . config('dimensions-videos.second-width') . 'x'
                . config('dimensions-videos.second-height') . config('files-extensions.video'));

        $response = $this->uploadFileConvert(
            Storage::disk('public')
                ->get("video-resize-" . config('dimensions-videos.second-width') . 'x'
                    . config('dimensions-videos.second-height') . config('files-extensions.video')),
            "video-resize-" . config('dimensions-videos.second-width') . 'x'
                . config('dimensions-videos.second-height') . '-' . Str::uuid() . config('files-extensions.video')
        );

        $this->data['url'] = $response;
        $this->data['url_signed'] = $this->getAuthorizationToDownloadFile($this->data['url']);
        $this->data['created_at'] = Carbon::now()->toDateString();
        $this->data['updated_at'] = Carbon::now()->toDateString();
        $this->data['video_id'] = $this->data['video_id'];
        $this->data['format'] = config('dimensions-videos.second-width') . 'x'
            . config('dimensions-videos.second-height');

        FFMpeg::openUrl($this->data['url_signed'])
            ->getFrameFromSeconds(config('time-capture-thumb.seconds'))
            ->export()
            ->toDisk('public')
            ->save("video-thumb-" . config('dimensions-thumb.second-width') . 'x'
                . config('dimensions-thumb.second-height') . config('files-extensions.thumb'));

        $url_file = $this->uploadPublicFileCompress(
            Storage::disk('public')
                ->get("video-thumb-" . config('dimensions-thumb.second-width') . 'x'
                    . config('dimensions-thumb.second-height') . config('files-extensions.thumb')),
            "video-thumb-" . config('dimensions-thumb.second-width') . 'x'
                . config('dimensions-thumb.second-height') . '-' . Str::uuid() . config('files-extensions.thumb')
        );

        $this->removeImagePublicDisk("video-thumb-" . config('dimensions-thumb.second-width') . 'x'
            . config('dimensions-thumb.second-height') . config('files-extensions.thumb'));
        $this->removeImagePublicDisk("video-resize-" . config('dimensions-videos.second-width') . 'x'
            . config('dimensions-videos.second-height') . config('files-extensions.video'));

        $this->data['thumbnail_url'] = $url_file;

        $this->otherVideoFormat->create($this->data);
    }
?>

错误原因及解决方法

  • 命名空间大小写错误:X264类的正确命名空间是FFMpeg\Format\Video\X264(Video首字母大写),你代码中使用的\FFMpeg\Format\video\X264是小写的video,这会导致类无法被找到。修正后代码应改为:
    new \FFMpeg\Format\Video\X264()
    
  • 建议引入类而非全局调用:在文件顶部添加use语句,避免拼写错误:
    use FFMpeg\Format\Video\X264;
    
    之后直接使用new X264()即可。
  • 自动加载缓存问题:Laravel的自动加载缓存可能未更新,执行以下命令清除缓存:
    composer dump-autoload
    php artisan optimize:clear
    
  • 依赖版本不兼容:检查php-ffmpeg/php-ffmpeg与LaravelFFMpeg的版本适配性,可尝试更新依赖:
    composer update php-ffmpeg/php-ffmpeg
    

内容的提问来源于stack exchange,提问作者Despossivel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 03:00:52