• 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:admin@112.112.112.112: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:admin12345@124.112.228.212: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:admin12345@124.112.228.212: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:admin12345@124.112.228.212: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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    以上完整程序工程可在EasyDarwin官网下载到:www.easydarwin.org
    easyavfilter

  • 相关阅读:
    深入Linux下的GCC编译器:从入门到精通
    Golang 并发 Channel的用法
    @Qualifier注解的原理
    后端学习 -gRPC
    冥想第四百八十九天
    无胁科技-TVD每日漏洞情报-2022-9-20
    基于OpenDaylight和OVSDB搭建VxLAN网络
    手把手推导Back Propagation
    新功能&案例分享丨DolphinDB 与你相约上海,报名限时开放!
    ValueError: need at least one array to concatenate
  • 原文地址:https://blog.csdn.net/xiejiashu/article/details/132757941