TermSpar
TermSpar

Reputation: 441

Combine audio and video files with FFMPEG in C#

I'm currently trying to combine two files, one audio, and one video, in my C# program using FFMPEG with the following code:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "ffmpeg.exe";
startInfo.Arguments = "ffmpeg -i video.mp4 -i mic.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4";
using (Process exeProcess = Process.Start(startInfo))
{
    exeProcess.WaitForExit();
}

My debug folder is set up like this: enter image description here

So I don't get any runtime errors, however, it just doesn't create the final output.mp4 file that I need it to.

Upvotes: 6

Views: 7960

Answers (2)

urmat abdykerimov
urmat abdykerimov

Reputation: 452

For some reason answer given by TermSpar didn't work for me at all, code that worked for me:

ProcessStartInfo startInfo = new()
{
    CreateNoWindow = false,
    FileName = @"C:\Users\User\Documents\ffmpeg.exe",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    Arguments = string.Format(" -i {0} -i {1} -shortest {2} -y", @"C:\images\video.avi", @"C:\images\voice.wav", @"C:\images\result.avi")
};

using Process exeProcess = Process.Start(startInfo);

//debug
string StdOutVideo = exeProcess.StandardOutput.ReadToEnd();
string StdErrVideo = exeProcess.StandardError.ReadToEnd();
//

exeProcess.WaitForExit();
exeProcess.Close();

Upvotes: 1

TermSpar
TermSpar

Reputation: 441

I ended up solving it by doing the following:

string args = "/c ffmpeg -i \"video.mp4\" -i \"mic.wav\" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = @"" + outputPath;
startInfo.Arguments = args;
using Process exeProcess = Process.Start(startInfo)

exeProcess.WaitForExit();

Upvotes: 9

Related Questions