windows ffmpeg7 通过rtsp拉取h265裸流

点击下边那个链接会转到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);

相关推荐

  1. 通过ffmpeg实现rtsp rtmp rtmps

    2024-04-12 09:40:03       59 阅读
  2. 纯C读取文件实现解析H264每一帧数据

    2024-04-12 09:40:03       66 阅读
  3. 音视频实战---从音视频文件中提取h264

    2024-04-12 09:40:03       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 09:40:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 09:40:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 09:40:03       82 阅读
  4. Python语言-面向对象

    2024-04-12 09:40:03       91 阅读

热门阅读

  1. 基础算法学习笔记(C++)

    2024-04-12 09:40:03       133 阅读
  2. 支持向量机和感知机有什么关联?

    2024-04-12 09:40:03       148 阅读
  3. win下VScode中C++里Compile&Debug

    2024-04-12 09:40:03       118 阅读
  4. string的使用

    2024-04-12 09:40:03       41 阅读
  5. MySQL面试题系列-15

    2024-04-12 09:40:03       36 阅读
  6. 尝试CSDN语法使用-1

    2024-04-12 09:40:03       43 阅读
  7. tcp 为什么要三次握手

    2024-04-12 09:40:03       39 阅读
  8. 怎么“访问”Spring容器管理的bean?

    2024-04-12 09:40:03       41 阅读
  9. 深入理解Vue 3中的自定义Hooks

    2024-04-12 09:40:03       146 阅读
  10. 弱者忍受孤独,强者享受孤独

    2024-04-12 09:40:03       38 阅读
  11. linux磁盘知识学习

    2024-04-12 09:40:03       31 阅读