FFmpeg编解码的那些事(2)

2024-05-30 20:12
文章标签 ffmpeg 编解码

本文主要是介绍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)

这篇关于FFmpeg编解码的那些事(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1016808

相关文章

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

ffmpeg面向对象-待定

1.常用对象 rtsp拉流第一步都是avformat_open_input,其入参可以看下怎么用: AVFormatContext *fmt_ctx = NULL;result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL); 其中fmt_ctx 如何分配内存的?如下 int avformat_open_input(

FFmpeg系列-视频解码后保存帧图片为ppm

在正常开发中遇到花屏时怎么处理呢?可以把解码后的数据直接保存成帧图片保存起来,然后直接看图片有没有花屏来排除是否是显示的问题,如果花屏,则代表显示无问题,如果图片中没有花屏,则可以往显示的方向去排查了。 void saveFrame(AVFrame* pFrame, int width, int height, int iFrame){FILE *pFile;char szFilename[

【IPV6从入门到起飞】4-RTMP推流,ffmpeg拉流,纯HTML网页HLS实时直播

【IPV6从入门到起飞】4-RTMP推流,ffmpeg拉流,纯HTML网页HLS实时直播 1 背景2 搭建rtmp服务器2.1 nginx方案搭建2.1.1 windows 配置2.1.2 linux 配置 2.2 Docker方案搭建2.2.1 docker 下载2.2.2 宝塔软件商店下载 3 rtmp推流3.1 EV录屏推流3.2 OBS Studio推流 4 ffmpeg拉流转格式

ffmpeg使用安装使用教程(命令行-Python)

安装教程 https://blog.csdn.net/yuanmomoya/article/details/141992114 ffmpeg转换操作视频十分的占用cpu,会把cpu打满,线上使用的话需要注意下 命令行操作 一、视频转码 将视频从一种格式转换为另一种格式: ffmpeg -i input_video.mp4 output_video.avi 这将把输入的 MP4

Linux speex音频库-音频数据编解码

speex音频数据编解码 speex简述speex encoder(编码器)speex decoder(解码器)denoise vad (降噪,语音活性检测) speex简述 speex官网 Speex: A Free Codec For Free Speech Overview Speex is an Open Source/Free Software patent-fre