Merge audio and video in c#? [duplicate] - 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:
So I don't get any runtime errors, however, it just doesn't create the final output.mp4 file that I need it to.

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

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

Related

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:
So I don't get any runtime errors, however, it just doesn't create the final output.mp4 file that I need it to.
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();
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();

c# Process exe with arguments gives error

I sort of stuck into what should be a small issue. I need to run two different processes but the second failures.
My first process is:
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "xxxx.xxxx.cmd";
string arguments = "-Sxxxx -f -d --port xxxx > c:\\xxxx\\tmp\\bug.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
No worries here. Everything works but the second.
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "WebDaemon.exe";
string arguments = "-debug -i WebDaemon.xxxx.1234 --ServerPort=4444 --MultipleServerPorts=1 --CGIPort=4500 --CGICallbackPort=4600 --MinServerProcs=2 --MaxServerProcs=2 > c:\\xxxx\\tmp\\WebDaemonTrace.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Gives an error. The process created and shows up in taskmanager, but after 5 seconds it disappears. Furthermore i can read in a debug message that:
"commandline argument '>' is unknown"
I cannot seem to find the problem. Open to any suggestions :-)

How to run a command tool exe in C# web application?

I am using graphicmagic exe to execute a command using command prompt. I have added the graphicmagic exe in my application root folder. I want to execute this exe and pass the arguments through c#. How to do this? I have tried the below code:
Method: 1
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = AppDomain.CurrentDomain.BaseDirectory + "\\gm1.3.5\\gm",
Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 'D:\\Image\\FFv1\\dpx1\\1.dpx' 'D:\\Image\\FFv1\\tiff1\\1.tiff'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
}
Method: 2
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm";
startInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"D:\\Image\\FFv1\\dpx1\\1.dpx\" \"D:\\Image\\FFv1\\tiff1\\1.tiff\"";
proc.StartInfo = startInfo;
proc.Start();
But both of its not working. Please suggest a way to execute exe file and pass commands.
It should be the privilege issue.
Try to adjust the identity in your app pool to LocalSystem.
This one is working fine:
using System.Diagnostics;
Process p = new Process();
p.StartInfo.FileName = #"D:\Executable\Projects\MMF\gm1.3.5\gm.exe";
p.StartInfo.Arguments = "convert -define dpx:colorspace=rgb -define tiff:bits-per-sample=16 \"C:\\Documents and Settings\\software\\Desktop\\DPX_Test\\3.dpx\" \"C:\\TEMP_21May2015103216\\3.tiff\"";
p.Start();
p.WaitForExit();

getting a blank cmd window by running command line from C#

I am trying to open command line in the c folder, from C#.
the expectation is to see this in the command line window:
C:>
but instead i am getting a blank cmd window.
this is the code:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Console.ReadKey();
WaitForExit is what you are looking for.
EDIT:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"c:\",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
This will halt the execution of all statements after WaitForExit. The time you close the command window, the statements following WaitForExit will be executed.
i think you're looking for this
first we'll create a process for CMD.exe and then passes "/K cd \". "/K" will "CMD.exe" to receive parameter and stay open, while "cd \" will take us to "C:/" which is your requirement
System.Diagnostics.Process.Start("CMD.exe", "/K \"cd /\"");
Console.ReadKey();
I believe if you use the /K Argument when executing the command, you should have a command prompt running at C:\
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"C:\";
startinfo.Arguments = "/K";
startinfo.UseShellExecute = false;
Process.Start(startinfo);
Or
Process command = new Process();
command.StartInfo.UseShellExecute = false;
command.StartInfo.WorkingDirectory = #"C:\";
command.StartInfo.Arguments = "/K";
command.StartInfo.FileName = "cmd.exe";
command.Start();
The /K argument executes the cmd.exe command and keeps the window open :)

How to run commands on Mingw from other process with C#?

I am trying to execute commands on Mingw from other process with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH-TO-MINGW\mingwenv.cmd";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
StreamWriter str = exeProcess.StandardInput;
str.WriteLine("ls");
exeProcess.WaitForExit();
}
but this code just lunches Mingw and does not input command.
Do I miss something or it is not possible to do?
Thanks
Update
Based on Jason Huntleys answer, solution for me looks like this (I am using OMNeT++ simulator so directories are based on it)
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH_TO_SIMULATOR\omnetpp-4.3\msys\bin\sh.exe";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
using (StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("cd PATH_TO_SIMULATOR/omnetpp-4.3");
str.Flush();
str.WriteLine("ls");
str.Flush();
}
exeProcess.WaitForExit();
}
I suspect c# is launching your mingw command in a CMD prompt. You need to spawn your process within a bash shell. Try wrapping your command with "bash -l -c 'ls'" or "bash -c 'ls'". Make sure bash is in your PATH, and be sure you quote command arguments, if any. I've had to use this method when I spawn bash commands from popen in python. I know diff language, but could be related.
I imagine the code will look similar to this. I haven't tested in C#:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "bash.exe";
startInfo.Arguments = "-l -c 'ls -l /your/msys/path'";
# Or other examples with windows path:
# startInfo.Arguments = "-l -c 'ls -l /c/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:\\your\\path'";
process.StartInfo = startInfo;
process.Start();
you should do
str.Flush();
so the command you've writen is passed to the process.
also you should use using statement when dealing with streams
using (Process exeProcess = Process.Start(startInfo))
{
using(StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("ls");
str.Flush();
exeProcess.WaitForExit();
}
}

Categories