以下是一套完整的视频点播功能开发源码,就简简单单几行代码,就可以完成原来ffmpeg很复杂的视频点播转码调用流程,而且还可以集成在自己的应用程序中调用,例如java、php、cgo、c++、nodejs,不需要再单独一个ffmpeg的进程来调用,方法很简单,这个对比原来用exec调用ffmpeg进程转码,从效率到可控性上都提升了不止10倍,而且代码优秀:
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实例,相当于创建一个ffmpeg.exe进程
Easy_Handle avFilterHandle = NULL;
EasyAVFilter_Create(&avFilterHandle);
// 信息回调
EasyAVFilter_SetCallback(avFilterHandle,__AVFilterCallBack,0);
// 将本地视频文件转成格式化的MP4或者HLS提供点播,参考命令:./ffmpeg.exe -fflags +genpts -hide_banner -i C://temp/La_Isla_Bonita.avi -vcodec copy -acodec aac -ac 2 -strict -2 -f hls -hls_time 16 -hls_list_size 0 C://temp/La_Isla_Bonita.m3u8
EasyAVFilter_AddInput(avFilterHandle, "C://temp/La_Isla_Bonita.avi", 1);
EasyAVFilter_AddFilter(avFilterHandle, "-fflags +genpts -hide_banner -vcodec copy -acodec aac -ac 2 -strict -2");//设置音视频转码参数
EasyAVFilter_AddFilter(avFilterHandle, "-f hls -hls_time 16 -hls_list_size 0");//设置HLS切片参数
EasyAVFilter_SetOutput(avFilterHandle, "C://temp/La_Isla_Bonita.m3u8", 0);
//验证命令
char filterCommand[256] = { 0 };
EasyAVFilter_GetFilters(avFilterHandle, filterCommand);
printf("command: %s\n", filterCommand);
getchar();
EasyAVFilter_Start(avFilterHandle, 0, 8, 10);//注意,文件转码不需要循环读取,第二个参数从1改成0
getchar();
EasyAVFilter_Stop(avFilterHandle);
EasyAVFilter_Release(&avFilterHandle);
return 0;
}
以上完整程序工程可在EasyDarwin官网下载到:www.easydarwin.org