ffmpeg获取rtsp h265,用ffmpeg从RTSP服务器拉流并保存各种格式文件

ffmpeg:FFmpeg的名称来自MPEG视频编码标准,前面的“FF”代表“Fast Forward,是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

库的组成:

libavformat:用于各种音视频封装格式的生成和解析,包括获取解码所需信息以生成解码上下文结构和读取音视频帧等功能;

libavcodec:用于各种类型声音/图像编解码;

libavutil:包含一些公共的工具函数;

libswscale:用于视频场景比例缩放、色彩映射转换;

libpostproc:用于后期效果处理;

ffmpeg:该项目提供的一个工具,可用于格式转换、解码或电视卡即时编码等;

ffsever:一个 HTTP 多媒体即时广播串流服务器;

ffplay:是一个简单的播放器,使用ffmpeg 库解析和解码,通过SDL显示;

一般我们从摄像头RTSP获取得到视频流都是“裸流”,也就是原始数据流。得到的码流一般是h264,或者h265,用av_read_frame()来读取每一帧的数据,数据是存放在结构体AVpack里面。

把视频流保存成h264、h265文件的流程:

( av_register_all() 函数在ffmpeg4.0以上版本已经被废弃,所以4.0以下版本就需要注册初始函数)

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

avformat_open_input();打开网络流或文件流

avformat_find_stream_info();获取视频文件信息

av_malloc(sizeof( AVPacket ));  申请 AVPacket 空间,以便存放数据

av_read_frame();读取每一帧的(h264、h265)数据,存放在结构体AVPack里面

fwrite();写入文件,h264编码的文件的后缀写.h264,h265编码的文件的后缀写.h265,

av_free( packet );  写完之后释放  AVPacket 的空间

avformat_free_context( );函数释放空间

avformat_close_input();关闭rtsp流

用FFmpeg将rtsp视频流保存成文件的demo:

#include #include #ifdef __cplusplus

extern "C"

{

#endif

/*Include ffmpeg header file*/

#include #ifdef __cplusplus

}

#endif

int main()

{

AVFormatContext *pFormatCtx = NULL;

AVDictionary *options = NULL;

AVPacket *packet = NULL;

char filepath[] = "rtsp://172.168.0.161:554/11";

//av_register_all(); //函数在ffmpeg4.0以上版本已经被废弃,所以4.0以下版本就需要注册初始函数

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); //设置最大时延

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

//打开网络流或文件流

if (avformat_open_input(&pFormatCtx, filepath, NULL, &options) != 0)

{

printf("Couldn't open input stream.\n");

return 0;

}

//获取视频文件信息

if (avformat_find_stream_info(pFormatCtx, NULL)<0)

{

printf("Couldn't find stream information.\n");

return 0;

}

//查找码流中是否有视频流

int videoindex = -1;

unsigned i = 0;

for (i = 0; inb_streams; i++)

if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)

{

videoindex = i;

break;

}

if (videoindex == -1)

{

printf("Didn't find a video stream.\n");

return 0;

}

packet = (AVPacket *)av_malloc(sizeof(AVPacket)); // 申请空间,存放的每一帧数据 (h264、h265)

FILE *fpSave;

fpSave = fopen("geth264_test.h264", "wb");

//这边可以调整i的大小来改变文件中的视频时间,每 +1 就是一帧数据

for (i = 0; i < 200; i++)

{

if (av_read_frame(pFormatCtx, packet) >= 0)

{

if (packet->stream_index == videoindex)

{

fwrite(packet->data, 1, packet->size, fpSave);

}

av_packet_unref(packet);

}

}

fclose(fpSave);

av_free(packet);

avformat_close_input(&pFormatCtx);

return 0;

}

猜你喜欢

转载自blog.csdn.net/xiehuanbin/article/details/133162374