Redirecting stdin and stdout where stdin closes first

后端 未结 2 1448
南方客
南方客 2021-01-19 19:01

This is actually related to another question I had that was already answered. That question is here: Redirecting stdout of one process object to stdin of another

My

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 19:24

    For anyone who wants to do something similar, here is the new version with KeeperOfTheSoul's advice applied:

            Process tccatProcess = new Process();           
            tccatProcess.StartInfo.FileName = "tccat";
            tccatProcess.StartInfo.Arguments = String.Format("-i {0} -T {1}", devNode, title);
            tccatProcess.StartInfo.UseShellExecute = false;
            tccatProcess.StartInfo.RedirectStandardOutput = true;
    
            Process ffmpegProcess = new Process();
            string bashSafePreviewTemplate = slasher.bashSlash(previewTempDir + "/test%03d.jpg");
            ffmpegProcess.StartInfo.FileName = "ffmpeg";
            ffmpegProcess.StartInfo.Arguments = String.Format("-i - -r 1 -t 1 -s {1}x{2} -ss {3} {0}", 
                bashSafePreviewTemplate, width, height, timePosition);
            ffmpegProcess.StartInfo.UseShellExecute = false;
            ffmpegProcess.StartInfo.RedirectStandardInput = true;
    
            Console.WriteLine("tccat command: {0} {1}", tccatProcess.StartInfo.FileName, tccatProcess.StartInfo.Arguments);
            Console.WriteLine("ffmpeg command: {0} {1}", ffmpegProcess.StartInfo.FileName, ffmpegProcess.StartInfo.Arguments);
    
            //return true;
    
            try{
                tccatProcess.Start();
                ffmpegProcess.Start();
    
                BinaryReader tccatOutput = new BinaryReader(tccatProcess.StandardOutput.BaseStream);
                BinaryWriter ffmpegInput = new BinaryWriter(ffmpegProcess.StandardInput.BaseStream);
                int buffSize = 4096;
                byte[] buff = new byte[buffSize];
    
    
                while(!ffmpegProcess.HasExited)
                {
                    ffmpegProcess.Refresh();
                    buff = tccatOutput.ReadBytes(buffSize);
                    ffmpegInput.Write(buff);
                    ffmpegInput.Flush();
                }
    
    
                tccatProcess.Kill();
                tccatProcess.Close();
                ffmpegProcess.Close();
    
    
            }catch(Exception e){
                        //uninteresting log code
                        return false;
    
            }
    

提交回复
热议问题