#include "qffmpeg.h"
#include <QDateTime>
#include <QDebug>
QFFmpeg::QFFmpeg(QObject *parent) :
QObject(parent)
{
videoStreamIndex=-1;
av_register_all();//注册库中所有可用的文件格式和解码器
avformat_network_init();//初始化网络流格式,使用RTSP网络流时必须先执行
pAVFormatContext = avformat_alloc_context();//申请一个AVFormatContext结构的内存,并进行简单初始化
pAVFrame=av_frame_alloc();
}
QFFmpeg::~QFFmpeg()
{
avformat_free_context(pAVFormatContext);
av_frame_free(&pAVFrame);
sws_freeContext(pSwsContext);
}
bool QFFmpeg::Init()
{
//视频延时问题???
// pAVFormatContext->max_interleave_delta > 0;
// pAVFormatContext->max_interleave_delta = 1;
// AVInputFormat *iformat = new AVInputFormat;
// *iformat->
AVDictionary *opts = NULL;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
av_dict_set(&opts, "stimeout", "3000000", 0);
//打开视频流
int result=avformat_open_input(&pAVFormatContext, url.toStdString().c_str(),NULL,&opts);
//int result=avformat_open_input(&pAVFormatContext, url.toStdString().c_str(),NULL,NULL);
if (result<0){
qDebug()<<"打开视频流失败";
return false;
}
//获取视频流信息
result=avformat_find_stream_info(pAVFormatContext,NULL);
if (result<0){
qDebug()<<"获取视频流信息失败";
return false;
}
//获取视频流索引
videoStreamIndex = -1;
for (uint i = 0; i < pAVFormatContext->nb_streams; i++) {
if (pAVFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex==-1){
qDebug()<<"获取视频流索引失败";
return false;
}
//获取视频流的分辨率大小
pAVCodecContext = pAVFormatContext->streams[videoStreamIndex]->codec;
videoWidth=pAVCodecContext->width;
videoHeight=pAVCodecContext->height;
avpicture_alloc(&pAVPicture,AV_PIX_FMT_RGB24,videoWidth,videoHeight);
AVCodec *pAVCodec;
//获取视频流解码器
pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id);
pSwsContext = sws_getContext(videoWidth,videoHeight,AV_PIX_FMT_YUV420P,videoWidth,videoHeight,AV_PIX_FMT_RGB24,SWS_BICUBIC,0,0,0);
//打开对应解码器
// //测试解决延时问题
// AVDictionary *param = NULL;
// //264
// av_dict_set(¶m,"preset","superfast",0);
// av_dict_set(¶m,"tune","zerolatency",0);
// //265
// av_dict_set(¶m,"x265-params","qp=20",0);
// av_dict_set(¶m,"preset","ultrafast",0);
// av_dict_set(¶m,"tune","zero-latency",0);
// result=avcodec_open2(pAVCodecContext,pAVCodec,¶m);
result=avcodec_open2(pAVCodecContext,pAVCodec,NULL);
if (result<0){
qDebug()<<"打开解码器失败";
return false;
}
qDebug()<<"初始化视频流成功";
return true;
}
void QFFmpeg::Play()
{
//一帧一帧读取视频
int frameFinished=0;
while (true){
if (av_read_frame(pAVFormatContext, &pAVPacket) >= 0){
if(pAVPacket.stream_index==videoStreamIndex){
qDebug()<<"开始解码"<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
avcodec_decode_video2(pAVCodecContext, pAVFrame, &frameFinished, &pAVPacket);
if (frameFinished){
mutex.lock();
sws_scale(pSwsContext,(const uint8_t* const *)pAVFrame->data,pAVFrame->linesize,0,videoHeight,pAVPicture.data,pAVPicture.linesize);
//发送获取一帧图像信号
QImage image(pAVPicture.data[0],videoWidth,videoHeight,QImage::Format_RGB888);
emit GetImage(image);
mutex.unlock();
}
}
}
av_free_packet(&pAVPacket);//释放资源,否则内存会一直上升
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
testFfmpeg1.zip (95个子文件)
testFfmpeg1
qffmpeg.h 1KB
testFfmpeg1.pro.user.df0ed44 63KB
ffmpeg
include
libavfilter
version.h 3KB
avfilter.h 56KB
buffersink.h 7KB
avfiltergraph.h 975B
buffersrc.h 5KB
asrc_abuffer.h 3KB
avcodec.h 4KB
libswscale
version.h 2KB
swscale.h 12KB
libavutil
md5.h 2KB
version.h 4KB
rational.h 4KB
avassert.h 2KB
parseutils.h 7KB
timestamp.h 3KB
audio_fifo.h 4KB
old_pix_fmts.h 14KB
murmur3.h 1KB
base64.h 2KB
pixfmt.h 28KB
bswap.h 3KB
aes.h 2KB
random_seed.h 1KB
file.h 2KB
dict.h 6KB
macros.h 1KB
imgutils.h 8KB
samplefmt.h 10KB
fifo.h 5KB
stereo3d.h 4KB
blowfish.h 2KB
common.h 14KB
buffer.h 10KB
eval.h 5KB
pixdesc.h 10KB
intreadwrite.h 18KB
hmac.h 3KB
replaygain.h 2KB
timecode.h 5KB
intfloat_readwrite.h 2KB
mem.h 14KB
crc.h 3KB
frame.h 22KB
avstring.h 12KB
ripemd.h 2KB
intfloat.h 2KB
avutil.h 8KB
xtea.h 2KB
error.h 5KB
channel_layout.h 9KB
sha512.h 2KB
ffversion.h 128B
cpu.h 5KB
bprint.h 8KB
sha.h 2KB
attributes.h 4KB
log.h 10KB
opt.h 33KB
lzo.h 2KB
lfg.h 2KB
adler32.h 2KB
downmix_info.h 3KB
mathematics.h 5KB
avconfig.h 211B
time.h 1KB
audioconvert.h 82B
libavformat
version.h 3KB
avformat.h 97KB
avio.h 18KB
libavcodec
version.h 5KB
xvmc.h 6KB
dxva2.h 2KB
vaapi.h 4KB
avfft.h 3KB
old_codec_ids.h 10KB
vda.h 4KB
avcodec.h 174KB
vdpau.h 6KB
lib
libswscale.dll.a 23KB
libavutil.dll.a 274KB
libavfilter.dll.a 57KB
libavcodec.dll.a 189KB
libavformat.dll.a 105KB
frmmain.ui 2KB
rtspthread.h 364B
rtspthread.cpp 155B
testFfmpeg1.pro.user 82KB
frmmain.h 429B
frmmain.cpp 1KB
main.cpp 177B
testFfmpeg1.pro 1KB
qffmpeg.cpp 4KB
testFfmpeg1.pro.user.155481f 82KB
共 95 条
- 1
局外狗
- 粉丝: 64
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 旅行活动的微信小程序页面
- 第三节课,后端代码优化 & 结束
- 基于matlab实现的10自由度齿轮传动系统动力学模型,通过调用ode45进行求解,考虑了轴承刚度和阻尼.rar
- 基于matlab实现齿轮系统非线性动力学模型和动力学求解 齿轮非线性动力学研究.rar
- 基于matlab实现的 流体力学有限元NS方程,有限元离散,得到速度场、压力场,流体力学.rar
- 基于matlab实现的GROWTH2.0是一个地球物理反演工具
- 基于matlab实现的matlab法求解椭圆轴承支承的刚性jeffcott转子的振动方程
- 基于matlab实现的sfs算法对目标的多角度图像进行三维重构.rar
- 基于matlab实现的VRPWT离散粒子群优化解决带时间窗的车辆路径问题.rar
- 基于matlab实现的齿轮系统非线性动力学模型和动力学求解 齿轮非线性动力学研究.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0