avcodec_decode_video2(pCodecCtx, m_pFrame, &got_picture, &m_packet); got_picture总为0

本文详细介绍了FFmpeg中av_init_packet函数的作用及使用方法,解决因未初始化packet导致的问题。通过具体示例帮助读者理解如何正确初始化packet。
原因:没有初始化m_packet:av_init_packet(&m_packet);
#include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavutil/avutil.h> int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; int videoStream; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开视频流 if (avformat_open_input(&pFormatCtx, "tcp://localhost:8000", NULL, NULL) != 0) { return -1; // Couldn't open stream } // 获取流信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { return -1; // Couldn't find stream information } // 查找视频流 videoStream = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { return -1; // Didn't find a video stream } // 获取解码器上下文 pCodecCtx = pFormatCtx->streams[videoStream]->codec; // 找到解码器 pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { return -1; // Codec not found } // 打开解码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { return -1; // Could not open codec } // 分配视频帧 pFrame = av_frame_alloc(); // 读取帧并解码 while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { if (avcodec_send_packet(pCodecCtx, &packet) == 0) { while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { // 在这里显示视频帧 // display_frame(pFrame); } } } av_packet_unref(&packet); } // 清理 av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; }把avcodec_send_packetavcodec_receive_frame换为旧版接口
最新发布
12-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值