我们可以使用ffmpeg命令进行很多工作,也可以在代码中调用ffmpeg指令进行相关操作,例如推流,录屏等,线面介绍C++调用ffmpeg命令进行rtmp推流。命令如下:
ffmpeg.exe -re -i qqq.flv -c copy -f flv rtmp地址
推流前,先打开rtmp服务器。下面的代码是C++调用ffmpeg指令本地flv文件rtmp推流:
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
wstring strFfmpegPath = L"D:\\DevLib\\FFmpeg\\ffmpeg.exe"; //ffmpeg.exe所在的位置的完整路径
wstring strCmdContent = L"/c " + strFfmpegPath + L" -re -i " + L"D:\\TestFiles\\qqq.flv -c copy -f flv rtmp://192.168.227.129/live";
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = L"C:\\Windows\\System32\\cmd.exe";
ShExecInfo.lpParameters = strCmdContent.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
cout << "调用结束" << endl;
return 0;
}
代码说明:
(1)用windows API ShellExecuteEx可以调用外部exe程序,注意exe的路径。
(2)windows API有些涉及到宽字节unicode编码,建议在编程时使用unicode, wstring替代string等基本编程规则