6

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.

15
  • What is shown in a Command Prompt when you run the ffmpeg command? Commented Dec 4, 2018 at 0:42
  • I don't get a chance to see it. It just pops up and then closes down Commented Dec 4, 2018 at 0:43
  • However if I run the command just in Command Prompt it performs the merge. Commented Dec 4, 2018 at 1:08
  • Oh to keep a Cmd Prompt open specify the /k switch. I think you might need to CD (aka Change Directory) - I see you have ffmpeg.exe in the debug folder - so it looks good. What we will get to eventually is reading the stdout from ffmpeg into C# to diagnose the root cause. See here mathewsachin.github.io/blog/2017/07/28/ffmpeg-pipe-csharp.html Commented Dec 4, 2018 at 1:09
  • Change my directory to what exactly? Commented Dec 4, 2018 at 1:11

2 Answers 2

9

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Try a few more times with the arguments in the parameter. Like I was saying there are some good examples I linked to in the SU thread. -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k are actually important. See here: stackoverflow.com/a/36324719/495455
Hey @BenCompSci, I used your code and it generates the .mp4 file but it doesn't use the audio from the .wav file at all, it has still the same audio and has a bit less size than the input video, does this work for you? What could be the problem in my case? I downloaded ffmpeg.exe file and placed it in the Debug folder.
Never mind, ended up using this and it works like a charm ""/c ffmpeg -i video.mp4 -i audio.mp3 -map 0:0 -map 1:0 -shortest output.mp4""
Ya, for me I had to use command prompt to run the command via adding FFMPEG to the path
1

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();

1 Comment

The TermSpar answer dont works for you because u are using the ffmeg.exe directory, if u using the cmd.exe the argument changes so "/c ffmpeg" isnt necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.