php-ffmpeg get video duration
Asked Answered
D

4

6

When I try to get the duration of a video using the php-ffmpeg wrapper and ffprobe, I get a huge object instead of just the duration.

$ffprobe = FFMpeg\FFProbe::create();
    $ffprobe->format($this->videoFile)
            ->get('duration'); 

$this->videoFile is /home/admin/........./5422346433.mp4

So it points to right file and the duration is listed in the giant object down in

[[-show_format-/home/admin/web/admin.simplewebevents.com/public_html/cron/649652027.mp4][1]] => Array
                    (
                        [0] => FFMpeg\FFProbe\DataMapping\Format Object
                            (
                                [properties:FFMpeg\FFProbe\DataMapping\AbstractData:private] => Array
                                    (
                                        [filename] => /home/admin/web/admin.simplewebevents.com/public_html/cron/649652027.mp4
                                        [nb_streams] => 2
                                        [nb_programs] => 0
                                        [format_name] => mov,mp4,m4a,3gp,3g2,mj2
                                        [format_long_name] => QuickTime / MOV
                                        [start_time] => 0.000000
                                        [duration] => 5736.833333
                                        [size] => 668381267
                                        [bit_rate] => 932056
                                        [probe_score] => 100
                                        [tags] => Array
                                            (
                                                [major_brand] => mp42
                                                [minor_version] => 0
                                                [compatible_brands] => mp42mp41isomavc1
                                                [creation_time] => 2016-12-04 18:25:58
                                            )

                                    )

                            )

                        [1] => 
                    )

            )

But apparently ->get('duration') doesnt return the duration.

I've also tried with

$ffprobe
->streams($this->videoFile) // extracts streams informations
->videos()                      // filters video streams
->first()                       // returns the first video stream
->get('duration');
Demibastion answered 11/5, 2017 at 18:19 Comment(0)
D
8
$ffprobe
    ->streams($this->videoFile)
    ->videos()                   
    ->first()                  
    ->get('duration');

RETURNS the duration. So I have to store that command into a variable. The correct way to get the duration is:

$duration = $ffprobe
           ->streams($this->videoFile)
           ->videos()                   
           ->first()                  
           ->get('duration');
Demibastion answered 11/5, 2017 at 18:45 Comment(2)
$videoFile = 'addicted.mp4'; $duration = $ffprobe ->streams($videoFile) ->videos() ->first() ->get('duration'); echo $duration; Why I can't receive video information?Tansey
@VipulJethva you probably need absolute path to the video file.Demibastion
C
4

in the README.md is write this code:

$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe
    ->format('/path/to/video/mp4') // extracts file informations
    ->get('duration');             // returns the duration property

and work fine.

Chrysler answered 18/7, 2018 at 20:51 Comment(0)
S
0
$file='msd.mp4';

$time = exec("$ffmpeg -i ".$file." 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//");

echo getSecondsFromHMS($time);


function getSecondsFromHMS($time) {

    $timeArr = array_reverse(explode(":", $time));    
    $seconds = 0;
    echo '<pre>';print_r($timeArr);echo '</pre>';
    foreach ($timeArr as $key => $value) {
        if ($key > 2)
            break;
        $seconds += pow(60, $key) * $value;
    }
    return (int)$seconds;
}

It is the return duration in seconds.

Siemens answered 27/3, 2018 at 8:13 Comment(0)
H
-2

if anyone don't wanted to use ffmpeg-php then use following code

$file='http://techslides.com/demos/sample-videos/small.mp4';

$dur = shell_exec("ffmpeg -i ".$file." 2>&1");
if(preg_match("/: Invalid /", $dur)){
  return false;
}
preg_match("/Duration: (.{2}):(.{2}):(.{2})/", $dur, $duration);
if(!isset($duration[1])){
  return false;
}
$hours = $duration[1];
$minutes = $duration[2];
$seconds = $duration[3];

echo $seconds + ($minutes*60) + ($hours*60*60);

Reference

Hodgepodge answered 4/4, 2020 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.