windows ffmpeg7 通过rtsp拉取h265裸流

本文介绍了如何使用libavcodec库在C语言中从RTSP流中抓取H265视频数据,并将其保存到本地文件的过程,包括配置选项、打开输入流、查找视频流以及读取并写入帧数据。
摘要由CSDN通过智能技术生成

点击下边那个链接会转到github

下载完成后,添加include、lib到工程。

添加头文件:

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libswscale/swscale.h"
}

代码:

AVDictionary* options = NULL;
av_dict_set(&options, "buffer_size", "1024000", 0); //设置缓存大小,1080p可将值跳到最大
av_dict_set(&options, "rtsp_transport", "tcp", 0); //以tcp的方式打开,
av_dict_set(&options, "stimeout", "5000000", 0); //设置超时断开链接时间,单位us
av_dict_set(&options, "max_delay", "500000", 0); //设置最大时延

AVFormatContext* pFormatCtx = avformat_alloc_context(); //用来申请AVFormatContext类型变量并初始化默认参数,申请的空间

//打开网络流或文件流
if (avformat_open_input(&pFormatCtx, pUrl, NULL, &options) != 0){
	printf("Couldn't open input stream.\n");
	return ;
}

//获取视频文件信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0){
	printf("Couldn't find stream information.\n");
	return ;
}

//查找码流中是否有视频流
int video_stream_index = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
	if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
		video_stream_index = i;
	}
}
if (video_stream_index == -1) {
	printf("video_stream_index = -1\n");
	return;
}
AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket)); // 申请空间,存放的每一帧数据 (h264、h265)

FILE* pFile = fopen("test.h265", "wb");

//
while (true){
	if (av_read_frame(pFormatCtx, packet) >= 0){
		if (packet->stream_index == video_stream_index){
			fwrite(packet->data, 1, packet->size, pFile);
		}

		av_packet_unref(packet);
	}
}

fclose(pFile);
av_free(packet);
avformat_close_input(&pFormatCtx);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值