当前位置: 首页 > news >正文

FFmpeg编解码的那些事(2)

软解码

1.构建输入AVFormatContext

    AVFormatContext* avFormatContext = avformat_alloc_context();int res = avformat_open_input(&avFormatContext,filename.toUtf8().data(), NULL, NULL);//打开文件if (res < 0) {qCritical()<<__FUNCTION__<<__LINE__<<"error "<<res<<"in avformat_open_input";return res;}

2.查找视频信息流

    res = avformat_find_stream_info(avFormatContext, nullptr);//取出流信息if (res < 0){return res;}//查找视频流和音频流av_dump_format(avFormatContext, 0,filename.toUtf8().data(), 0);//列出输入文件的相关流信息for (unsigned int i = 0; i < avFormatContext->nb_streams; i++){if (avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){if(video_index==-1){video_index = i;}}}m_width   = avFormatContext->streams[video_index]->codec->width;m_height  = avFormatContext->streams[video_index]->codec->height;

3.初始化软解码

    AVCodecContext *avCodecContext = avFormatContext->streams[video_index]->codec;AVCodec* m_codec = avcodec_find_decoder(avCodecContext->codec_id);if (avcodec_open2(avCodecContext, m_codec, NULL) < 0){return false;}

4.初始化SwsContext图像转换上下文

    img_convert_ctx = sws_getContext(m_width,m_height,avCodecContext->pix_fmt,m_width,m_height,AV_PIX_FMT_BGRA,SWS_BICUBIC,NULL, NULL, NULL);

5.循环读取数据

    while (true){int ret = av_read_frame(avFormatContext, avPacket);if (ret < 0){qDebug()<< "read end";break;}else if (avPacket->stream_index == video_index){decode_to_photo(avPacket);}av_packet_unref(avPacket);}void decode_to_photo(const AVPacket *avPacket)
{int ret = avcodec_send_packet(avCodecContext, avPacket);if (ret < 0) {char error[1024];av_strerror(ret, error, 1024);qDebug() << "发送解码失败:" << error;}while (true){ret = avcodec_receive_frame(avCodecContext, avFrame);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){//qDebug() << "avcodec_receive_frame end:" << ret;break;}else if (ret < 0){//qDebug()<< "获取解码数据失败"<<ret;return;}else{QImage pixLogo2;int m_rotat = getRotateAngle(avFormatContext->streams[video_index]);if(dec_st == AV_SOFTWARE_DECODER){sws_scale(img_convert_ctx,(const unsigned char* const*)avFrame->data,avFrame->linesize, 0,avFrame->height,bgrFrame->data,bgrFrame->linesize);QImage pixLogo(bgrFrame->data[0],bgrFrame->width,bgrFrame->height,bgrFrame->width * 4, QImage::Format_ARGB32);QMatrix matrix;matrix.rotate(m_rotat);pixLogo2 = pixLogo.transformed(matrix,Qt::FastTransformation);/*这里可以进行必要的图片处理操作*/}curcount++;av_frame_unref(sw_frame);av_frame_unref(avFrame);}}
}

tips:

根据pts来计算一桢在整个视频中的时间位置

timestamp(秒) = pts * av_q2d(st->time_base) 
timestamp(毫秒) = pts * av_q2d(st->time_base) * 1000 

计算视频长度的方法

time(秒) = st->duration * av_q2d(st->time_base)

相关文章:

  • 包和final
  • HaloDB 的 Oracle 兼容模式
  • 一个月速刷leetcodeHOT100 day13 二叉树结构 以及相关简单题
  • 解决vue版本不一致导致不能正常编译
  • 学习笔记——动态路由协议——OSPF(OSPF基本术语)
  • Sylvester矩阵、子结式、辗转相除法的三者关系(第二部分)
  • 【leetcode1944--队列中可以看到的人数】
  • Linux 系统配置修改时间时区
  • OrangePi AIpro 快速上手初体验——接口、样例和目标检测
  • 5.29_Java程序流程控制
  • Linux网络编程: udp,tcp协议原理
  • 如何解压忘记了密码的加密zip压缩包?这两个方法收藏好!
  • 把自己的垃圾代码发布到官方中央仓库
  • 自定义数据集上的3D目标检测:使用OpenPCDet训练CenterPointPillar模型
  • Vue速成学习笔记
  • 【剑指offer】让抽象问题具体化
  • 【译】理解JavaScript:new 关键字
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • docker python 配置
  • Go 语言编译器的 //go: 详解
  • JavaScript学习总结——原型
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • laravel with 查询列表限制条数
  • magento 货币换算
  • Mysql优化
  • node-glob通配符
  • Travix是如何部署应用程序到Kubernetes上的
  • 闭包--闭包之tab栏切换(四)
  • 第2章 网络文档
  • 京东美团研发面经
  • 你真的知道 == 和 equals 的区别吗?
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 如何用vue打造一个移动端音乐播放器
  • 一道面试题引发的“血案”
  • 自定义函数
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • ​1:1公有云能力整体输出,腾讯云“七剑”下云端
  • #### golang中【堆】的使用及底层 ####
  • #Linux(make工具和makefile文件以及makefile语法)
  • #考研#计算机文化知识1(局域网及网络互联)
  • $(selector).each()和$.each()的区别
  • $.each()与$(selector).each()
  • (1) caustics\
  • (1)Android开发优化---------UI优化
  • (10)STL算法之搜索(二) 二分查找
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • .htaccess 强制https 单独排除某个目录
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .java 9 找不到符号_java找不到符号
  • .Net Core 微服务之Consul(二)-集群搭建
  • .net core控制台应用程序初识
  • .NET Standard 支持的 .NET Framework 和 .NET Core
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)