C-调用-ffmpeg.exe-执行音视频混流合并
目录
C++ 调用 ffmpeg.exe 执行音视频混流合并;
ffmpeg.exe文件可以去官网下载:
官网似乎不提供32位dll文件的下载了,但是可以下载exe文件来直接调用;
#include <iostream>
#include <io.h>
#include <direct.h>
#include <cstdlib>
using namespace std;
/// <summary>
/// C++ 调用 ffmpeg.exe 执行音视频混流合并
/// </summary>
/// 输入的命令中路径参数不需要加双引号,此方法会自动添加双引号以排除system()路径空格的问题
/// <param name="_pathVedio">视频文件路径</param>
/// <param name="_pathAudio">音频文件路径</param>
/// <param name="_pathOutput">输出目录</param>
/// <param name="_pathFfmpegExe">ffmpeg.exe文件路径</param>
extern "C" _declspec(dllexport)
bool combineVedioAudio(string _pathVedio, string _pathAudio, string _nameOut, string _pathOutput, string _pathFfmpegExe)
{
string outPutFilePath = _pathOutput + "\\" + _nameOut;// _最终的输出文件的完整路径
//参数检错
{
if (0 != _access(_pathAudio.c_str(), 0))//音频文件不存在
return false;
if (0 != _access(_pathVedio.c_str(), 0))//视频文件不存在
return false;
if (0 != _access(_pathFfmpegExe.c_str(), 0))//ffmpeg.exe文件不存在
return false;
if (0 != _access(_pathOutput.c_str(), 0))//输出目录不存在,则创建目录
{
if (0 != _mkdir(_pathOutput.c_str()))
return false;
}
}
// 备注 -- system命令中含有空格时,会导致命令错误理解,将其视作多个目录名字,将其用引号包含起来可以解决,即:a 改为 "a"
{
_pathOutput = "\"" + outPutFilePath+ "\"";
_pathAudio = "\"" + _pathAudio + "\"";
_pathVedio = "\"" + _pathVedio + "\"";
_pathFfmpegExe = "\"" + _pathFfmpegExe + "\"";
}
//组合system命令
string command =
_pathFfmpegExe
+ " -i " + _pathVedio
+ " -i " + _pathAudio
+ " -codec copy " + _pathOutput;
//备注::多对双引号时,system,会去掉首尾 的双引号;所以在命令头部和尾部各自加一个双引号:
command = "\"" + command + "\"";
//执行命令
// cout << "\n\n" + command + "\n\n" << endl;
system(command.c_str());
//检查执行结果
if ( 0 != _access(outPutFilePath.c_str(),0) )
return false;
else
return true;
}
vs2019,添加头文件
‘删掉该头文件后可以继续使用
调整了一下参数,方便移植调用
#pragma once
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;
/// <summary>
/// C++ 调用 ffmpeg.exe 执行音视频混流合并
/// </summary>
/// 输入的命令中路径参数不需要加双引号,此方法会自动添加双引号以排除system()路径空格的问题
/// <param name="pathVedio">视频文件路径</param>
/// <param name="pathAudio">音频文件路径</param>
/// <param name="pathOutput">输出目录</param>
/// <param name="pathFfmpegExe">ffmpeg.exe文件路径</param>
extern "C" _declspec(dllexport)
bool combineVedioAudio(char* pathVedio, char* pathAudio, char* nameOut, char* pathOutput, char* pathFfmpegExe)
{
string outPutFilePath;// _最终的输出文件的完整路径
//参数整理
string _pathVedio(pathVedio);
string _pathAudio(pathAudio);
string _nameOut(nameOut);
string _pathOutput(pathOutput);
string _pathFfmpegExe(pathFfmpegExe);
outPutFilePath = _pathOutput + "\\" + _nameOut;
//参数检错
{
if (0 != _access(_pathAudio.c_str(), 0))//音频文件不存在
{
cout << "音频文件不存在" << endl;
return false;
}
if (0 != _access(_pathVedio.c_str(), 0))//视频文件不存在
{
cout << "视频文件不存在" << endl;
return false;
}
if (0 != _access(_pathFfmpegExe.c_str(), 0))//ffmpeg.exe文件不存在
{
cout << "ffmpeg.exe文件不存在" << endl;
return false;
}
if (0 != _access(_pathOutput.c_str(), 0))//输出目录不存在,则创建目录
{
if (0 != _mkdir(_pathOutput.c_str()))
{
cout << "输出目录不存在,且创建失败" << endl;
return false;
}
}
}
// 备注 -- system命令中含有空格时,会导致命令错误理解,将其视作多个目录名字,将其用引号包含起来可以解决,即:a 改为 "a"
{
_pathOutput = "\"" + outPutFilePath + "\"";
_pathAudio = "\"" + _pathAudio + "\"";
_pathVedio = "\"" + _pathVedio + "\"";
_pathFfmpegExe = "\"" + _pathFfmpegExe + "\"";
}
//组合system命令
string command =
_pathFfmpegExe
+ " -i " + _pathVedio
+ " -i " + _pathAudio
+ " -codec copy " + _pathOutput;
//备注::多对双引号时,system,会去掉首尾 的双引号;所以在命令头部和尾部各自加一个双引号:
command = "\"" + command + "\"";
//执行命令
cout << "\n\n" + command + "\n\n" << endl;
system(command.c_str());
//检查执行结果
if (0 != _access(outPutFilePath.c_str(), 0))
return false;
else
return true;
}