ffmpeg获取视频时长(秒数)

/**
 * 是否windows服务器
 *
 * @return boolean
 */
function isWinOs(){

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') 
    {
        return 1;
    }
    return 0;
}

 /**
 * 注意:此方法在Linux服务器/Windows上有效果 需安装FFmpeg
 * FFmpeg获得视频文件的总长度时间(秒数)
 * @param file 视频文件的地址
 * @param ffmpeg_path ffmpeg的绝对路径 windows服务器 必须 $ffmpeg = 'D:\\items\\ffmpeg\\bin\\ffmpeg.exe';
 */
function getVideoTime($file,$ffmpeg_path=''){

    $duration_in_seconds = 0;
    $vtime = false;
    if(isWinOs()){
        $ffmpeg_path = $ffmpeg_path?$ffmpeg_path:'D:\\tony\\ffmpeg-4.4\\bin\\ffmpeg.exe';
        // $file = 'http://xxx.com/uploads/20210603/a854b660ddbcaea11976252bcda02a8d.mp4';
        // $file = 'C:\\Users\\Administrator\\Desktop\\mp4-mp3\\yin1.mp4';
        // if (!file_exists($file)) {
        //     return ['status'=>0, 'msg'=>'视频文件不存在'];
        // }
        $commond = "{$ffmpeg_path} -i {$file} 2>&1";
        exec($commond, $str_res, $str_r);
        if (is_array($str_res)){
            foreach($str_res as $v){
                if (strpos($v, 'Duration') !== false){
                    $vtime = substr($v, stripos($v , '.') - 8, 8);//' Duration: 00:24:28.14, start: 0.000000, bitrate: 486 kb/s'    
                    break;
                }
            }
        }
    }else{
        $vtime = exec("ffmpeg -i ".$file." 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");//总长度
        // $ctime = date("Y-m-d H:i:s",filectime($file));//创建时间
        // error_log(var_export($vtime,true),3,__FILE__.'-vtime-.log');
    }
    //return $vtime; //格式 00:04:42.28
    if($vtime){
        $duration = explode(":",$vtime);
        if($duration){
            $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//转化为秒
        }
    }
    return $duration_in_seconds;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg中,获取视频时长可以通过AVFormatContext中的duration字段来实现,该字段表示音视频文件的总时长,单位是微秒(us)。如果duration为AV_NOPTS_VALUE,则表示无法获取时长。 具体实现代码如下: ```c AVFormatContext *fmt_ctx = NULL; if (avformat_open_input(&fmt_ctx, filename, NULL, NULL) != 0) { printf("Failed to open file '%s'\n", filename); return -1; } if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { printf("Failed to retrieve input stream information\n"); return -1; } int64_t duration = fmt_ctx->duration; if (duration != AV_NOPTS_VALUE) { int hours, mins, secs, us; secs = duration / AV_TIME_BASE; us = duration % AV_TIME_BASE; mins = secs / 60; secs %= 60; hours = mins / 60; mins %= 60; printf("Duration: %02d:%02d:%02d.%02d\n", hours, mins, secs, (100 * us) / AV_TIME_BASE); } else { printf("Duration: N/A\n"); } avformat_close_input(&fmt_ctx); ``` 在这个例子中,我们首先使用avformat_open_input函数打开音视频文件,然后通过avformat_find_stream_info函数获取视频文件的流信息,接着获取AVFormatContext的duration字段,如果duration不为AV_NOPTS_VALUE,则表示获取到了时长,我们将其转换为时分秒的格式进行输出。最后,我们需要调用avformat_close_input函数关闭文件并释放资源。 需要注意的是,由于duration字段的单位是微秒,因此我们需要将其转换为秒,进而转换为时分秒格式。另外,如果获取时长失败,则duration的值将为AV_NOPTS_VALUE,我们需要做特殊处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值