重定向stdin和stdout标准输入的地方先关闭 [英] Redirecting stdin and stdout where stdin closes first

查看:315
本文介绍了重定向stdin和stdout标准输入的地方先关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上涉及到另一个问题,我有一个已经回答了。这个问题是在这里:一个进程对象重定向到标准输出另一个



标准输入

我的问题是(我认为)谁是获得输入输出程序前,应退出程序。下面是bash的相当于我在做什么的:tccat -i的/ dev / SR0 -T 1 | ffmpeg的-i - -r -t 1 1 -s 96x72 -ss 5 /tmp/wsmanage/preview_tmp/test\%03d.jpg



这只是使用一个程序所谓tccat读取DVD的首个冠军。这得到输出ffmpeg的,这是要在每1帧创建一个输出文件第二个是.jpg格式1秒长。只要它输出其帧(或两个)它的存在。这工作得很好。



下面的代码,但是,其实不然。我得到的打印线的FFmpeg的鲹,而命令行版本在短短的第二个退出。然后,它只是30或40秒左右后停止打印。 FFmpeg的不退出,我的程序永远不会继续。



我我这样做对吗?



 过程tccatProcess =新工艺(); 
tccatProcess.StartInfo.FileName =tccat;
tccatProcess.StartInfo.Arguments =的String.Format( - 我{0} -T {1},设备节点,标题);
tccatProcess.StartInfo.UseShellExecute = FALSE;
tccatProcess.StartInfo.RedirectStandardOutput = TRUE;

流程ffmpegProcess =新工艺();
串bashSafePreviewTemplate = slasher.bashSlash(previewTempDir +/test%03d.jpg);
ffmpegProcess.StartInfo.FileName =的ffmpeg;
ffmpegProcess.StartInfo.Arguments =的String.Format( - 我 - -r -t 1 1 -s {1}×{2} {-ss 3} {0},
bashSafePreviewTemplate,宽度,高度,timePosition);
ffmpegProcess.StartInfo.UseShellExecute = FALSE;
ffmpegProcess.StartInfo.RedirectStandardInput = TRUE;


尝试{
tccatProcess.Start();
ffmpegProcess.Start();

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

串线;
,而(ffmpegProcess.HasExited!)
{
ffmpegProcess.Refresh();
如果((行= tccatOutput.ReadLine())!= NULL)
{
Console.WriteLine(打印线的FFmpeg);
Console.Out.Flush();
ffmpegInput.WriteLine(线);
ffmpegInput.Flush();
}
}

Console.WriteLine(关闭tccat);
Console.Out.Flush();
tccatProcess.Close();
Console.WriteLine(Tccat封闭);
Console.Out.Flush();


}赶上(例外五){
//无趣日志代码
返回FALSE;
}


解决方案

不tccat输出二进制因为你的数据是视频和图片的工作吗?如果是的话你不应该来读/写输入/输出流,而不是直接在文本阅读器包装呢?



如果因此想要使用<一个HREF =htt​​p://stackoverflow.com/questions/1284184/can-i-put-binary-in-stdin-c/1284753#1284753> http://stackoverflow.com/questions/1284184/can-i-把二进制 - 在标准输入-C / 1284753#1284753


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 issue is (I think) that the program who is getting the input should exit before the program outputting. Here is the bash equivalent of what I'm doing: tccat -i /dev/sr0 -T 1 | ffmpeg -i - -r 1 -t 1 -s 96x72 -ss 5 /tmp/wsmanage/preview_tmp/test\%03d.jpg

This just uses a program called tccat to read the first title of a dvd. That gets output to ffmpeg, which is going to create an output file at 1 frame per second that is 1 second long in .jpg format. As soon as it outputs its frame (or two) it exists. This works fine.

The following code, however, does not. I get scads of "Printing line to ffmpeg", whereas the command line version exits in just over a second. Then, it just stops printing after 30 or 40 seconds or so. FFmpeg never exits, and my program never continues.

I'm I doing this right?

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

解决方案

Doesn't tccat output binary data since you're working with video and images? If so shouldn't you be reading/writing to the input/output streams directly rather than wrapping them in a text reader?

If so you want to be using http://stackoverflow.com/questions/1284184/can-i-put-binary-in-stdin-c/1284753#1284753

这篇关于重定向stdin和stdout标准输入的地方先关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆