c# using pipes to get output stream

1.4k views Asked by At

I am trying to pipe youtube-dl output to ffmpeg as input, but cannot seem to get the piping part to work. In normal cmd, I can do something like

 C:\YT\youtube-dl.exe -o - https://www.youtube.com/watch?v=L4aLQ0ki9Tk | ffmpeg -i pipe:0 -f asf pipe:1 

but in c# this doesn't work. Currently, I have c# create 2 processes:

one for youtube-dl

C:\YT\youtube-dl.exe -o - https://www.youtube.com/watch?v=L4aLQ0ki9Tk

and another for ffmpeg

ffmpeg  -i {yt.StandardOutput} -f s16le -ar 48000 -ac 2 pipe:1

The problem is with the {yt.StandardOutput} (where yt is the process name of the youtube-dl process and -i specifies the input file/stream). Using pipe:0 doesn't work either and I am not sure how to link the piped output of the first to the input of the second.

2

There are 2 answers

0
Scott Chamberlain On BEST ANSWER

When doing your processing instead of starting youtube-dl.exe and ffmpeg as separate processes start the command shell itself from your code using cmd.exe /C this will allow you to use the pipe to flow the context from one program to another.

var proc = Process.Start("cmd.exe", 
                         "/C C:\YT\youtube-dl.exe -o - https://www.youtube.com/watch?v=L4aLQ0ki9Tk | ffmpeg -i pipe:0 -f asf pipe:1");
0
Stuart On

pipe:0 is stdin, so when you start ffmpeg.exe you could set RedirectStandardInput = true, then use the Process StandardInput property to get the stream to write to, you can then hook this up to the StandardOutput property of the other process with StreamReader + StreamWriter + byte array buffer.

In fact, just found an example on Github: https://github.com/theogeer/aaxtomp3/blob/b05514036dff85d9c395bfa5d9bf272f6361e729/AaxToMp3GUI/AaxToMp3GUI/Converter.cs