以下是一套完整的RTSP流转RTMP推流功能的开发源码,就简简单单几行代码,就可以完成原来ffmpeg很复杂的调用流程,而且还可以集成在自己的应用程序中调用,不需要再单独一个ffmpeg的进程来调用,方法很简单:
cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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:admin12345@112.112.222.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@112.112.222.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);
getchar();
EasyAVFilter_Start(avFilterHandle, 1, 8, 10);//注意,文件转码不需要循环读取,第二个参数从1改成0
getchar();
// 停止流程,回收EasyAVFilter对象
EasyAVFilter_Stop(avFilterHandle);
EasyAVFilter_Release(&avFilterHandle);
return 0;
}
以上完整程序工程可在EasyDarwin官网下载到:www.easydarwin.org