开始写博客!!今天第一个:FFmpeg解码网络rtsp流的一般流程和这几天遇到的问题,尤其是avformat_input_open解析错误的网络串流长时间不返回!

AVFormatContext* m_pFormatCtx;  

AVCodecContext * m_pCodecCtx;   

AVCodec* m_pCodec; 

AVFrame* m_pFrame; 

AVPacket m_AVPkt;

// 注册库
 av_register_all();
 avformat_network_init();  

//打开文件或者是网络串流
avformat_open_input(&m_pFormatCtx,pcURL,NULL,NULL);
/***************************************************************************************/
在函数avformat_open_input函数中,我们解析错误的网络缠流会出现函数长时间不返回的现象

要解决
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 C++ 解码 RTSP 视频需要使用 FFmpeg 库。以下是一个简单的示例代码: ``` extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> } int main() { av_register_all(); avformat_network_init(); AVFormatContext* pFormatCtx = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; AVPacket packet; AVFrame* pFrame = NULL; const char* url = "rtsp://example.com/stream"; if (avformat_open_input(&pFormatCtx, url, NULL, NULL) != 0) { return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { return -1; } int videoStream = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { return -1; } pCodecCtx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar); pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { return -1; } if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { return -1; } pFrame = av_frame_alloc(); while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { if (avcodec_send_packet(pCodecCtx, &packet) < 0) { break; } while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { // 处理解码后的图像数据 } } av_packet_unref(&packet); } avformat_close_input(&pFormatCtx); avcodec_free_context(&pCodecCtx); av_frame_free(&pFrame); return 0; } ``` 上述代码实现了 RTSP 视频解码,并可以通过处理解码后的图像数据实现各种功能。需要注意的是,该代码仅为示例代码,实际使用时需要根据具体情况进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值