Slowhand's avatar

Get total (video durations) with GetID3

Dear all,

I am building a Laravel app in which I upload videos.

To recover the duration of each video, I use the following package which is very good: https://github.com/JamesHeinrich/getID3/

Where I need help is to get the total duration of all the videos and I have no idea how to handle this.

Here is an image that illustrates my research:

https://i.stack.imgur.com/XzSKP.png

public function getVideoDuration($Videofile) { $getID3 = new \getID3(); $pathVideo = 'storage/chapitres-videos/' . Auth::user()->id . '/' . $Videofile; $fileAnalyze = $getID3->analyze($pathVideo); $playtime = $fileAnalyze['playtime_string'];

    return $playtime;
}

$Videofile = $this->videoManager->videoStorage($request->file('video')); $lecture->video = $Videofile;

Thank you all and have a nice day!

0 likes
7 replies
tykus's avatar

Are you calculating it on the fly; would you be better to calculate and store the video duration with the database record (if that exists)? If you calculate and store the duration with each video then a Collection sum will work for the total duration.

Tray2's avatar

I would store that information in a field in a table on each upload then it's way easier to get in on demand without accessing the file system and every file every request.

Slowhand's avatar

Thanks Tykys.

The values are stored in the database in a table called Course.

Can you please explain me how to use the Collection sum?

It would be very helpfull !

Thank you

Slowhand's avatar

Thanks TRAY2.

Same question I asked tykus : How to use Collection sum?

I'm not a Laravel expert...

Thank you !

tykus's avatar

If you store the duration in seconds along with the video record, then:

$videos->sum(duration);

If you store the duration in a time string, e.g H:m:s then you need to convert it to a format that can be summed

Tray2's avatar

Just a tip. Try to do the sum in the sql query instead of with php after the fetch.

Slowhand's avatar

Thanks for your answers. I'll try this asap.

Please or to participate in this conversation.