ffmpeg c#asp video conversion - c#

I am trying to convert one flv video to mp4 video. Why this
following code results error? it gives exception as "No process is
associated with this object." The parameters
"Path_FFMPEG"="E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\ffmpeg\\bin\\ffmpeg.exe" and "strParam"="-i E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\Videos\\cars1.flv -same_quant E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\Videos\\ConvertedFiles\\cars1.mp4"
Process ffmpeg = new Process();
ProcessStartInfo ffmpeg_StartInfo = new ProcessStartInfo(Path_FFMPEG, strParam);
ffmpeg_StartInfo.UseShellExecute = false;
ffmpeg_StartInfo.RedirectStandardError = true;
ffmpeg_StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo = ffmpeg_StartInfo;
ffmpeg_StartInfo.CreateNoWindow = true;
ffmpeg.EnableRaisingEvents = true;
ffmpeg.Start();
ffmpeg.WaitForExit();
ffmpeg.Close();
ffmpeg.Dispose();
ffmpeg = null;

try this code (assuming your mp4 is named video):
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = //PHYSICAL path to ffmpeg (use \\ instead of \);
proc.StartInfo.Arguments = "-i video.flv video.mp4
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
proc.WaitForExit();
proc.Close();

Related

Executing a process with Process.Start() using the path of the process

I'm doing this:
public static void ExecProcess(String path, string filename)
{
Process proc = new Process();
proc.StartInfo.FileName = path + "nst.exe";
proc.StartInfo.Arguments = filename;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
var stringa = proc.StandardOutput.ReadToEnd();
proc.Close();
}
The problem is my process is using the path of my C# application and not its path. So nst.exe is in C:\Desktop but when I call it with the code above the execution path became C:\\Documents\VisualStudio\MyProject\Debug\.
How can I execute the process in his path?
[EDIT]
This is how I call the method:
public void EseguiOttimizzatore()
{
OttimizzatoreService.ExecProcess(#"C:\Users\Developer\Desktop\", _idPlanning.ToString() + ".dat");
}
Set the WorkingDirectory property of StartInfo:
proc.StartInfo.WorkingDirectory = #"C:\Users\Developer\Desktop\";

How to use wevtutil.exe command lines in a Process?

This is two functions im using the wevtutil in both functions this arguments worked in a bat file but not working here i cant find any of the text files created in the contentDirectory.
Something is wrong with the Arguments i guess.
private void SystemEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe system /rd:true /f:text> eventsys.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
private void AppEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe application /rd:true /f:text> eventapp.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
What is wrong with the arguments ?
your cmd.exe is missing a /c agument. type cmd.exe /? in a termainal for more info.
you could just change the Arg line too:
proc.StartInfo.Arguments = "/c wevtutil qe application /rd:true /f:text";
but really I guess you want to do something like this instead.
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "wevtutil";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "qe application /rd:true /f:text";
Then redirect stdout/stderr to allow you to process the output in C#.

Run command line code programmatically using C#

I'm using this code run in windows command prompt..
But I need this done programmatically using C# code
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pdf "connection
Strings" "C:\Users\XXX\Desktop\connection string\DNN"
try this
ExecuteCommand("Your command here");
call it using process
public void ExecuteCommand(string Command)
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
}
You may use the Process.Start method:
Process.Start(
#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",
#"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN"""
);
or if you want more control over the shell and be able to capture for example the standard output and error you could use the overload taking a ProcessStartInfo:
var psi = new ProcessStartInfo(#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe")
{
Arguments = #"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(psi);
You should be able to do that using a process
var proc = new Process();
proc.StartInfo.FileName = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe ";
proc.StartInfo.Arguments = string.Format(#"{0} ""{1}""" ""{2}""","-pdf","connection Strings" ,"C:\Users\XXX\Desktop\connection string\DNN");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string outPut = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();

strange behaviour on another process via Process.Start(startInfo) -- continue

The original location (strange behaviour on another process via Process.Start(startInfo)) doesn't allow me post test code properly. I have to start a new question here.
Our C#(V3.5) needs to call a C++ executable to process a raw data file and generate result files for us.
It worked before with the following code (without ReadToEnd() or ReadLine() call):
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Now the input raw data file changed (bigger than before). Our call to that executable will hang forever. Based on advice, I added ReadToEnd() or ReadLine() but it will hang on those calls. So I have to either use startInfo.UseShellExecute = true; or set
startInfo.UseShellExecute = false;
// and with
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardOutput = false;
Don't know why?
Following are cases I tested:
working case:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardOutput = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
//startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
correctionProcess.WaitForExit();
}
working case:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.RedirectStandardError = false;//can be commented out
startInfo.RedirectStandardOutput = false;//can be commented out
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
correctionProcess.WaitForExit();
}
NOT working case (using ReadLine()):
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
//startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
while (!correctionProcess.HasExited)
{
Log.logItem(LogType.Performance, "read errorMsg ####", "DPTM::correctData()", "");
string errorMsg = correctionProcess.StandardError.ReadLine(); // <-- Hangs here
Log.logItem(LogType.Performance, "read errorMsg", "DPTM::correctData()", "errorMsg=" + errorMsg);
string outputMsg = correctionProcess.StandardOutput.ReadLine();
Log.logItem(LogType.Performance, "read outputMsg", "DPTM::correctData()", "outputMsg=" + outputMsg);
Thread.Sleep(100);
}
}
NOT working case (using ReadToEnd()):
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
//startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
while (!correctionProcess.HasExited)
{
Log.logItem(LogType.Performance, "read errorMsg ####", "DPTM::correctData()", "");
string errorMsg = correctionProcess.StandardError.ReadToEnd(); // <-- Hangs here!
Log.logItem(LogType.Performance, "read errorMsg", "DPTM::correctData()", "errorMsg=" + errorMsg);
string outputMsg = correctionProcess.StandardOutput.ReadToEnd();
Log.logItem(LogType.Performance, "read outputMsg", "DPTM::correctData()", "outputMsg=" + outputMsg);
Thread.Sleep(100);
}
}
NOT working case:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
//startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
Log.logItem(LogType.Performance, "read errorMsg ####", "DPTM::correctData()", "");
string errorMsg = correctionProcess.StandardError.ReadToEnd(); // <-- Hangs here!
Log.logItem(LogType.Performance, "read errorMsg", "DPTM::correctData()", "errorMsg=" + errorMsg);
string outputMsg = correctionProcess.StandardOutput.ReadToEnd();
Log.logItem(LogType.Performance, "read outputMsg", "DPTM::correctData()", "outputMsg=" + outputMsg);
correctionProcess.WaitForExit();
}
you are deadlocked on the error/output buffer. Your second process is waiting for the buffer to free up while you are waiting for that process to finish. You need to asynchronously read output/error streams to prevent that.
Looking at your MSDN sample: wow that sample is really confusing... It would've been easier to follow if they just limited it to std out or std error.
You need to first set data handler
correctionProcess.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
and then call
p.BeginOutputReadLine();
Just like comment [confusingly] advises do no try to read output there. Use handler instead.
private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs data)
{
if (data.Data != null)
{
// append data.Data to your internal buffer (StringBuilder?)
}
}

how to run batch file from C# in the background

How can I run a batch file from C# but in the backgound without the command prompt windows being displayed.
I use this Process.Start(batch.bat"); but this will display the command prompt.
Any idea?
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = "setSecDLL.bat";
si.UseShellExecute = false;
System.Diagnostics.Process.Start(si);
You can specify that with a Startinfo:
var si = new System.Diagnostics.ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = "test.cmd";
System.Diagnostics.Process.Start( si);

Categories

Resources