S.M_Emamian
S.M_Emamian

Reputation: 17393

How to get duration a video - laravel

I want to get the duration of my videos when I upload them. To do this, I upload my videos like this:

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


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

My movie is uploaded well. now I want to get the duration of this video. I'm using 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');

but I got this error:

  (2/2) ExecutableNotFoundException

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

Upvotes: 5

Views: 13742

Answers (3)

MR_AMDEV
MR_AMDEV

Reputation: 1942

Using FFMPEG PHP library, and using FFprobe like this to get video duration in seconds:

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

Upvotes: 0

Batyrkhan Shakenov
Batyrkhan Shakenov

Reputation: 79

first install getID3 by composer require james-heinrich/getid3 then

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

Upvotes: 8

Chris
Chris

Reputation: 3486

I personally created a Video CMS and found the easiest way to be using ID3 as follows:

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;
}

Before I used ffmpeg like this:

// 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);

Both options will work perfectly, choose whichever works best for you.

Upvotes: 3

Related Questions