4

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.

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
TermSpar
  • 401
  • 3
  • 13
  • What is shown in a Command Prompt when you run the ffmpeg command? – Jeremy Thompson Dec 04 '18 at 00:42
  • I don't get a chance to see it. It just pops up and then closes down – TermSpar Dec 04 '18 at 00:43
  • However if I run the command just in Command Prompt it performs the merge. – TermSpar Dec 04 '18 at 01: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 https://mathewsachin.github.io/blog/2017/07/28/ffmpeg-pipe-csharp.html – Jeremy Thompson Dec 04 '18 at 01:09
  • Change my directory to what exactly? – TermSpar Dec 04 '18 at 01:11
  • Yeah that (CD'ing) might be a red-herring, please follow that article and pipe the output to a file, then [edit] your question. The error message will tell us whats wrong. – Jeremy Thompson Dec 04 '18 at 01:13
  • Whenever I try to do something like this `string output = process.StandardOutput.ReadToEnd(); MessageBox.Show(output);` it just shows up blank. – TermSpar Dec 04 '18 at 01:41
  • OK, run Process Explorer to see what the command is from C#, then change it to be the same as the manually executed cmd prompt one: https://superuser.com/questions/415360/how-do-i-find-out-command-line-arguments-of-a-running-program – Jeremy Thompson Dec 04 '18 at 01:41
  • Process Explorer doesn't seem to be picking up on any commands being passed in. It just says the latest command was `"C:\Users\Ben\Documents\Visual Studio 2017\Projects\Szy\WorkTracker\WorkTracker\bin\Debug\WorkTracker.exe" ` – TermSpar Dec 04 '18 at 01:49
  • Do you start with "cmd /k" before every command to keep the window from closing? – Jeremy Thompson Dec 04 '18 at 02:04
  • Yes, I have the command to `"cmd /k ffmpeg -i \"video.mp4\" -i \"mic.wav\" -shortest outPutFile.mp4";` – TermSpar Dec 04 '18 at 02:10
  • Dude, where is input file? Its not shown in your screenshot which has files ordered by name -i \"mic.wav" - I told you it was a CD issue.Put the video.mp4 and mic.wav files in the Debug folder. – Jeremy Thompson Dec 04 '18 at 02:18
  • I tried that, it still didn't work – TermSpar Dec 04 '18 at 02:19
  • If you track down the differences between the cmd prompt that worked manually and whats different in C# thats the answer. I dont have access to your PC but more screenshots and a [mcve] would definitely got a bigger audience. – Jeremy Thompson Dec 04 '18 at 02:27
  • Ya this is a pretty big project so it'll take a lot of work to allow one to reproduce it. I'll keep trying, thanks for your help – TermSpar Dec 04 '18 at 02:30
  • I was able to figure it out! – TermSpar Dec 04 '18 at 02:53

2 Answers2

8

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();
urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
TermSpar
  • 401
  • 3
  • 13
  • 1
    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: https://stackoverflow.com/a/36324719/495455 – Jeremy Thompson Dec 04 '18 at 04:12
  • 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. – musicinmusic Dec 16 '18 at 09:30
  • 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"" – musicinmusic Dec 16 '18 at 10:01
  • Ya, for me I had to use command prompt to run the command via adding FFMPEG to the path – TermSpar Dec 17 '18 at 18:02
0

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();
urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
  • 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. – Felipe Mateus Apr 08 '22 at 14:10