Piping process output to new process

懵懂的女人 提交于 2020-01-11 07:29:12

问题


I am having issues piping the output from the rtmpdump process to ffmpeg and I believe the issue is my process is stealing the output of rtmpdump.

In my research I have heard of trying to use a cmd.exe process and run rtmpdump.exe as a /C command within that, but this issue is I lose reference to the rtmpdump.exe process that is spawned from that, and I need to be able to manage multiple rtmpdump processes within my program and selectively kill certain ones at times.

I initially tried something simple like this:

var p = new Process();
p.StartInfo.FileName = "rtmpdump.exe";
p.StartInfo.Arguments = "-v -r rtmp://somehost.com/app-name -o - | ffmpeg.exe -loglevel quiet -i - -c:v copy -c:a libvo_aacenc -b:a 128k \"test.mp4\"";

This does not work at all.

with "cmd.exe" as initial process:

var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C rtmpdump.exe -v -r rtmp://somehost.com/app-name -o - | ffmpeg.exe -loglevel quiet -i - -c:v copy -c:a libvo_aacenc -b:a 128k \"test.mp4\"";

This gets me closer to what I need it starts a rtmpdump process and redirects to ffmpeg, but now "p" will reference a non existent "cmd.exe" process that ran the command to start rtmpdump then "cmd.exe" terminates.

My only concern is being able to keep reference of the rtmpdump.exe processes created. ffmpeg will self terminate after rtmpdump closes its process can be ignored.

Edit: If the question wasn't clear. I am trying to pipe the output of rtmpdump to ffmpeg. normal way of doing it as part of the command argument are not working using the redirection operator |. and using a "cmd.exe" process does not work as needed.


回答1:


Came up with a simple solution.

Using the the CMD process as your starting process.

var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C rtmpdump.exe -v -r rtmp://somehost.com/app-name -o - |       ffmpeg.exe -loglevel quiet -i - -c:v copy -c:a libvo_aacenc -b:a 128k \"test.mp4\"";
test.Start();

And using this bit of code right after starting the process to get the last created rtmpdump process.

Process[] allDumps = Process.GetProcessesByName("rtmpdump"); // get all rtmp processes
if (allDumps.Any())
{
    Process newestProcess = allDumps.OrderByDescending(x => x.StartTime).First(); // get the last one created     
    // Add the newly captured process to your list of processes for use later.
}



回答2:


I am learning as much as I am answering here.

It seems there is not an easy way to manage multiple processes with the same name (int this case rtmpdump.exe).

Instead of using process name or PID, there seems another way by using the console command START and giving different window title name to it started by START.

In the command console, you would type in something like:

C:\>start "dumpProc01" rtmpdump.exe -v -r .......
C:\>start "dumpProc02" rtmpdump.exe -v -r .......
C:\>start "dumpProc03" rtmpdump.exe -v -r .......

And kill one specific process with the taskkill command. For example:

C:\>taskkill /fi "windowtitle eq dumpProc01"

To apply START to your process creation, the process argument would be:

// from this
p.StartInfo.Arguments = "/C rtmpdump.exe ....

// to this
p.StartInfo.Arguments = "/C start \"dumpProc01\" rtmpdump.exe -v -r ....

And for killing a specific process you would make a taskkill process:

Process kp = new Process();
kp.StartInfo.FileName = "taskkill.exe";
kp.StartInfo.Arguments = "/fi \"windowtitle eq dumpProc01\" ";
kp.Start();

You can give /min option for the start command like start /min ... to minimize windows.



来源:https://stackoverflow.com/questions/23795044/piping-process-output-to-new-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!