读取视频帧

打开视频解码器

完整main函数
//qt的头文件
#include "xplay.h"
#include
//c++的头文件
#include
using namespace std;
//预处理指令导入库
#pragma comment(lib,"avformat.lib") // 文件格式 依赖库
#pragma comment(lib, "avutil.lib") // 工具类库 获取错误信息的库
#pragma comment(lib, "avcodec.lib") // 编码格式
// 引用c语言的头文件
extern"C" {
#include
/***************************************************************
f9 可设置断点
注释:ctrl + k c
取消注释:ctrl + k u
***************************************************************/
int main(int argc, char *argv
av_register_all(); //注册所有的格式
char *path = "video.mp4"; //设置路径 这个视频是放在我的bin目录下面的
AVFormatContext *ic = NULL; //存放打开视频或者流的信息
int re = avformat_open_input(&ic, path, 0, 0); //打开文件流
if (re != 0) //打开失败
char buf
1024
= { 0 };
av_strerror(re, buf, sizeof(buf)); // 输出错误的信息
printf("open %s failed: %s\n",path, buf );
getchar();
return -1;
// 获取视频的时长 单位s
int totalSec = ic->duration / AV_TIME_BASE;
printf("file totleSec is %d-%d\n",totalSec/60,totalSec%60);
// 打开视频解码器
int videoStream = 0;
for (int i = 0; i < ic->nb_streams; i++) // 遍历视频流
AVCodecContext *enc = ic->streams
->codec; // 解码器的值
if (enc->codec_type == AVMEDIA_TYPE_VIDEO) // 判断是不是一个视频
videoStream = i;
AVCodec *codec = avcodec_find_decoder(enc->codec_id);// 查找解码器 返回的是解码器的id
if (!codec)
printf("video code not find!\n");
return -1;
int err = avcodec_open2(encwhatsapp网页版, codec, NULL);// 打开这个解码器
if (err != 0)
char buf
1024
= {0};
av_strerror(err,bufwhatsapp网页版登录, sizeof(buf));
printf(buf);
return -2;
printf("open codec success!\n");
// 读视频的信息
for (;;)
// 一帧数据
AVPacket pkt;// 一个结构体存放视频信息
re = av_read_frame(ic, &pkt);
if (re != 0) break;
printf("pts = %lld\n",pkt.pts);// pts 存放的时间戳 来控制进度
av_packet_unref(&pkt);
avformat_close_input(&ic); //释放
QApplication a(argcwhatsapp web, argv);
Xplay w;
w.show();
return a.exec();
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



