如何获取视频的时长 - Laravel

3

我希望在上传视频时获取视频持续时间。为此,我按如下方式上传视频:

$video = Carbon::now()->timestamp . '_' .
            $request->file('video')->getClientOriginalName();


        $request->file('video')->move(
            $this->getCorrectPathOnServerAndLocal('/assets/videos/videos'), $video
        );

我的电影已经上传成功。 现在我想获取这个视频的时长。 我正在使用PHP-FFMpeg

composer require php-ffmpeg/php-ffmpeg

$ffprobe = FFProbe::create(); //error
        dd("test");
        $duration = $ffprobe
            ->format($this->getCorrectPathOnServerAndLocal('/assets/videos/videos').$video) // extracts file informations
            ->get('duration');

但是我遇到了这个错误:
  (2/2) ExecutableNotFoundException

Unable to load FFProbe
in FFProbeDriver.php (line 50)
at FFProbeDriver::create(array(), null)in FFProbe.php (line 207)

你安装了FFMpeg吗?如果已经安装,你是否将其添加到系统PATH中了? - Mustafa Akçakaya
如果我想在服务器端使用这个库,它能正常工作吗? - S.M_Emamian
@S.M_Emamian 您的服务器上未安装 FFMpeg - DolDurma
3个回答

6

首先通过composer require james-heinrich/getid3安装getID3, 然后...

$getID3 = new \getID3;
$file = $getID3->analyze($video_path);
$duration = date('H:i:s.v', $file['playtime_seconds']);

2

我个人创建了一个视频内容管理系统,发现使用ID3标签是最简单的方法,具体如下:

public function getDuration($full_video_path)
{
    $getID3 = new \getID3;
    $file = $getID3->analyze($full_video_path);
    $playtime_seconds = $file['playtime_seconds'];
    $duration = date('H:i:s.v', $playtime_seconds);

    return $duration;
}

我之前使用ffmpeg的方式如下:

// Get the video duration
$parameters = "2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//";
$cmd_duration_ffmpeg = "$ffmpeg_path -i $full_video_path $parameters";
$duration = shell_exec($cmd_duration_ffmpeg);

两种选项都可以完美地工作,选择对你最好的那个。


1
我遇到了“Class 'getID3' not found”错误 @Chris - nitinsridar

0

使用FFMPEG PHP库,并像这样使用FFprobe获取视频时长(以秒为单位):

    $ffprobe = FFProbe::create();
    $duration = $ffprobe->format($request->file('video'))->get('duration');
    $duration = explode(".", $duration)[0];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接