首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重定向stdin和stdout,其中stdin首先关闭

重定向stdin和stdout,其中stdin首先关闭
EN

Stack Overflow用户
提问于 2009-09-03 13:21:58
回答 2查看 4.1K关注 0票数 4

这实际上与我已经回答过的另一个问题有关。这个问题就在这里:Redirecting stdout of one process object to stdin of another

我的问题是(我认为)获得输入的程序应该在程序输出之前退出。下面是我正在做的bash等价物: tccat -i /dev/sr0test 1| ffmpeg -i - -r 1 -T 1 -s 96x72 -ss 5/tmp/ is管理/ -t _tmp/-ss\%03d.jpg

这仅仅使用了一个名为tccat的程序来读取dvd的第一个标题。这将输出到ffmpeg,它将以每秒1帧的速度创建一个输出文件,该文件在.jpg格式中为1秒长。一旦它输出了它的帧(或两帧),它就存在了。这可以很好地工作。

但是,下面的代码并不这样做。我得到了大量的"Printing line to ffmpeg",而命令行版本在一秒钟内就退出了。然后,它会在30秒或40秒后停止打印。FFmpeg永远不会退出,我的程序也永远不会继续。

我这样做是对的吗?

代码语言:javascript
复制
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;


try{
    tccatProcess.Start();
    ffmpegProcess.Start();

    StreamReader tccatOutput = tccatProcess.StandardOutput;
    StreamWriter ffmpegInput = ffmpegProcess.StandardInput;

    string line;
    while(!ffmpegProcess.HasExited)
    {
        ffmpegProcess.Refresh();
        if((line = tccatOutput.ReadLine()) != null)
        {
            Console.WriteLine("Printing line to ffmpeg");
            Console.Out.Flush();
            ffmpegInput.WriteLine(line);
            ffmpegInput.Flush();
        }
    }

    Console.WriteLine("Closing tccat");
    Console.Out.Flush();
    tccatProcess.Close();
    Console.WriteLine("Tccat closed");
    Console.Out.Flush();


}catch(Exception e){
    //uninteresting log code
    return false;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-03 13:40:34

tccat不输出二进制数据吗?因为你在处理视频和图像?如果是这样的话,你不应该直接读/写输入/输出流,而不是将它们包装在文本阅读器中吗?

如果是这样的话,您希望使用Can i put binary in stdin? C#

票数 0
EN

Stack Overflow用户

发布于 2009-09-05 02:06:09

对于任何想做类似事情的人,这里是应用了KeeperOfTheSoul的建议的新版本:

代码语言:javascript
复制
        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;

        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1373387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档