Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c# - Piping process output to new process

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...