FFmpeg saves rtsp video stream into H264, h265 files

The name FFmpeg comes from the MPEG video encoding standard. The "FF" in front of it stands for "Fast Forward." It is a set of open source computer programs that can be used to record and convert digital audio and video, and convert them into streams.

Library composition:

libavformat : used for the generation and parsing of various audio and video encapsulation formats, including functions such as obtaining the information required for decoding to generate decoding context structures and reading audio and video frames;

libavcodec : used for various types of sound/image encoding and decoding;

libavutil : Contains some public utility functions;

libswscale : used for video scene scaling and color mapping conversion;

libpostproc : used for post-effect processing;

ffmpeg : A tool provided by this project, which can be used for format conversion, decoding or instant encoding of TV cards;

ffsever : an HTTP multimedia real-time broadcast streaming server;

ffplay : It is a simple player that uses the ffmpeg library to parse and decode, and displays it through SDL;

Generally, the video streams we obtain from camera RTSP are "naked streams", which are original data streams. The obtained code stream is generally h264 or h265. Use av_read_frame() to read the data of each frame. The data is stored in the structure AVpack.

The process of saving video streams into h364 and h365 files:

(The av_register_all() function has been abandoned in versions above ffmpeg 4.0, so versions below 4.0 need to register the initial function)
avformat_alloc_context() ; used to apply for AVFormatContext type variables and initialize default parameters, and apply for space
avformat_open_input() ; open the network stream Or file stream
avformat_find_stream_info() ; Get video file information
av_malloc(sizeof(AVPacket)) ; Apply for AVPacket space to store data
av_read_frame() ; Read the (h264, h265) data of each frame and store it in the structure AVPack
fwrite () ; Write files, the suffix of h264-encoded files is written as .h264, and the suffix of h265-encoded files is written as .h265
av_free(packet) ; After writing, release the space of AVPacket
avformat_free_context() ; Function releases space
avformat_close_input() ; Close rtsp stream

Demo of using FFmpeg to save rtsp video stream into a file:

#include <stdio.h>
#include <stdlib.h>
 
#ifdef __cplusplus 
extern "C"
{
#endif
	/*Include ffmpeg header file*/
#include <libavformat/avformat.h> 
#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; i<pFormatCtx->nb_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;
}

Guess you like

Origin blog.csdn.net/xiehuanbin/article/details/133159703