【问题标题】:php-ffmpeg get video durationphp-ffmpeg 获取视频时长
【发布时间】:2017-10-10 21:35:25
【问题描述】:

当我尝试使用 php-ffmpeg 包装器和 ffprobe 获取视频的持续时间时,我得到一个巨大的对象而不仅仅是持续时间。

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

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

所以它指向正确的文件,并且持续时间列在下面的巨型对象中

[[-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] => 
                    )

            )

但显然 ->get('duration') 不会返回持续时间。

我也试过

$ffprobe
->streams($this->videoFile) // extracts streams informations
->videos()                      // filters video streams
->first()                       // returns the first video stream
->get('duration');

【问题讨论】:

    标签: ffmpeg ffmpeg-php


    【解决方案1】:
    $ffprobe
        ->streams($this->videoFile)
        ->videos()                   
        ->first()                  
        ->get('duration');
    

    返回持续时间。所以我必须将该命令存储到一个变量中。获取持续时间的正确方法是:

    $duration = $ffprobe
               ->streams($this->videoFile)
               ->videos()                   
               ->first()                  
               ->get('duration');
    

    【讨论】:

    • $videoFile = 'addicted.mp4'; $duration = $ffprobe ->streams($videoFile) ->videos() ->first() ->get('duration');回声$持续时间;为什么收不到视频信息?
    • @VipulJethva 你可能需要视频文件的绝对路径。
    【解决方案2】:
    $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;
    }
    

    返回持续时间,以秒为单位。

    【讨论】:

      【解决方案3】:

      在 README.md 中写下这段代码:

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

      并且工作正常。

      【讨论】:

        【解决方案4】:

        如果有人不想使用 ffmpeg-php,请使用以下代码

        $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

        【讨论】:

          猜你喜欢
          • 2016-11-02
          • 1970-01-01
          • 2015-09-10
          • 1970-01-01
          • 1970-01-01
          • 2011-04-28
          • 2010-12-16
          • 2016-04-09
          • 2013-02-22
          相关资源
          最近更新 更多