EasyAVFilter代码示例之将摄像机RTSP流录像成mp4文件分段存储

以下是一套完整的RTSP流转mp4分段存储功能的开发源码,就简简单单几行代码,就可以完成原来ffmpeg很复杂的调用流程,而且还可以集成在自己的应用程序中调用,例如java、php、cgo、c++、nodejs,不需要再单独一个ffmpeg的进程来调用,方法很简单:

#include 
#include 
#include 

#include "EasyAVFilterAPI.h"

#ifdef _WIN32
#pragma comment(lib,"EasyAVFilter.lib")
#endif

Easy_Handle fRTSPHandle = 0;			
int Easy_APICALL __AVFilterCallBack(void* userPtr, EASY_AV_FILTER_STATE_T status, int progress, int errCode, const char *errMsg)
{
	//各种状态的回调,例如拉流状态/推流状态/转码状态/媒体信息
	return 0;
}

int main(int argc, char** argv)
{
	// 创建EasyAVFilter实例
	Easy_Handle avFilterHandle = NULL;
	EasyAVFilter_Create(&avFilterHandle);
	// 信息回调
	EasyAVFilter_SetCallback(avFilterHandle,__AVFilterCallBack,0);
	// 将网络流录制成本地MP4或者HLS录像,参考命令:./ffmpeg.exe -re -rtsp_transport tcp -i rtsp://admin:[email protected]:554/ch1/main/av_stream -c:v copy -c:a aac -f segment -segment_list ./20230829150000.m3u8 -segment_time 300 -strftime 1 rec_%Y-%m-%d_%H-%M-%S.ts
	EasyAVFilter_AddInput(avFilterHandle, "rtsp://admin:[email protected]:554/ch1/main/av_stream", 1);
	EasyAVFilter_AddFilter(avFilterHandle, "-vcodec copy -acodec aac");
	EasyAVFilter_AddFilter(avFilterHandle, "-f segment -segment_time 8 -reset_timestamps 1 -strftime 1");
	EasyAVFilter_AddFilter(avFilterHandle, "-segment_list C://temp/20230829150000.m3u8");//是否需要列表文件,可要可不要
	EasyAVFilter_SetOutput(avFilterHandle, "C://temp/output_%Y-%m-%d_%H-%M-%S.mp4", 0);

	// 将本地视频文件转成格式化的MP4或者HLS提供点播,参考命令:./ffmpeg.exe -re -rtsp_transport tcp -i rtsp://admin:[email protected]:554/ch1/main/av_stream -vcodec copy -acodec aac -ac 2 -strict -2 -f flv rtmp://172.81.216.155:3519/live/IbMkUXeVR?sign=SxMk8X6VRz
	EasyAVFilter_AddInput(avFilterHandle, "rtsp://admin:[email protected]:554/ch1/main/av_stream", 1);
	EasyAVFilter_AddFilter(avFilterHandle, "-vcodec copy -acodec aac -ac 2 -strict -2");
	EasyAVFilter_AddFilter(avFilterHandle, "-f flv");
	EasyAVFilter_SetOutput(avFilterHandle, "rtmp://172.81.216.155:3519/live/IbMkUXeVR?sign=SxMk8X6VRz", 0);//H.265 support

	char filterCommand[256] = { 0 };
	EasyAVFilter_GetFilters(avFilterHandle, filterCommand);
	printf("command: %s\n", filterCommand);

	EasyAVFilter_Start(avFilterHandle, 1, 8, 10);//注意,文件转码不需要循环读取,第二个参数从1改成0

	getchar();
	EasyAVFilter_Stop(avFilterHandle);

	getchar();
	EasyAVFilter_Release(&avFilterHandle);

	return 0;
}

以上完整程序工程可在EasyDarwin官网下载到:www.easydarwin.org
EasyAVFilter代码示例之将摄像机RTSP流录像成mp4文件分段存储_第1张图片

你可能感兴趣的:(ffmpeg,rtsp录像,rtsp,rtsp转mp4,rtsp分段录像,rtsp流服务端录像)