ffmpeg php api,php使用FFmpeg接口获取视频的播放时长、码率、缩略图以及创建时间...

php使用FFmpeg接口获取视频的播放时长、码率、缩略图以及创建时间

创建时间:2019年6月3日(星期一) 下午5:13| 分类:未分类

| 字数:2512  | 另存为... | 打印 | 添加到日历

ada86968eb0d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

FFmpeg获得视频文件的缩略图:

function getVideoCover($file,$time,$name) {

if(empty($time))$time = '1';//默认截取第一秒第一帧

$strlen = strlen($file);

// $videoCover = substr($file,0,$strlen-4);

// $videoCoverName = $videoCover.'.jpg';//缩略图命名

//exec("ffmpeg -i ".$file." -y -f mjpeg -ss ".$time." -t 0.001 -s 320x240 ".$name."",$out,$status);

$str = "ffmpeg -i ".$file." -y -f mjpeg -ss 3 -t ".$time." -s 320x240 ".$name;

//echo $str."";

$result = system($str);

}

Fmpeg读取视频播放时长和码率

define('FFMPEG_PATH', '/usr/local/ffmpeg2/bin/ffmpeg -i "%s" 2>&1');

function getVideoInfo($file) {

$command = sprintf(FFMPEG_PATH, $file);

ob_start();

passthru($command);

$info = ob_get_contents();

ob_end_clean();

$data = array();

if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {

$data['duration'] = $match[1]; //播放时间

$arr_duration = explode(':', $match[1]);

$data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数

$data['start'] = $match[2]; //开始时间

$data['bitrate'] = $match[3]; //码率(kb)

}

if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {

$data['vcodec'] = $match[1]; //视频编码格式

$data['vformat'] = $match[2]; //视频格式

$data['resolution'] = $match[3]; //视频分辨率

$arr_resolution = explode('x', $match[3]);

$data['width'] = $arr_resolution[0];

$data['height'] = $arr_resolution[1];

}

if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {

$data['acodec'] = $match[1]; //音频编码

$data['asamplerate'] = $match[2]; //音频采样频率

}

if (isset($data['seconds']) && isset($data['start'])) {

$data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间

}

$data['size'] = filesize($file); //文件大小

return $data;

}

//用法

$video_info = getVideoInfo('video.mp4');

print_r($video_info);

?>

Fmpeg获得视频文件的总长度时间和创建时间

function getTime($file){

$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));//创建时间

//$duration = explode(":",$time);

// $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//转化为秒

return array('vtime'=>$vtime,

'ctime'=>$ctime

);

}

need-to-insert-img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取视频时长,可以使用FFmpeg.AutoGen 库中的AVFormatContext和AVCodecContext结构体中的duration成员变量。具体实现步骤如下: 1. 使用avformat_open_input函数打开视频文件,得到AVFormatContext结构体。 2. 使用avformat_find_stream_info函数获取视频流信息,填充AVFormatContext结构体。 3. 遍历AVFormatContext结构体的streams数组,找到视频流对应的AVCodecContext结构体。 4. 从AVCodecContext结构体的duration成员变量获取视频时长。 以下是示例代码: ```csharp using FFmpeg.AutoGen; // 打开视频文件 AVFormatContext* formatContext = null; int ret = ffmpeg.avformat_open_input(&formatContext, filename, null, null); if (ret < 0) { // 打开失败,处理错误 } // 获取流信息 ret = ffmpeg.avformat_find_stream_info(formatContext, null); if (ret < 0) { // 获取失败,处理错误 } // 查找视频流 AVCodecContext* codecContext = null; int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { AVStream* stream = formatContext->streams[i]; if (stream->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; codecContext = ffmpeg.avcodec_alloc_context3(null); if (codecContext == null) { // 分配失败,处理错误 } ret = ffmpeg.avcodec_parameters_to_context(codecContext, stream->codecpar); if (ret < 0) { // 转换失败,处理错误 } break; } } // 获取视频时长 long duration = (long)codecContext->duration * 1000 / ffmpeg.AV_TIME_BASE; ``` 注意:以上代码仅供参考,实际使用时应该根据具体情况进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值