Swift和NSTask挂起

时间:2014-09-07 12:23:14

标签: xcode swift nstask

我是Mac World的新手。我正在使用Swift,我正在尝试一次运行一个外部进程。 现在一切正常,因为它被调试,我的意思是:在附带调试器的Xcode中运行。

我没有改变任何东西,并尝试在终端窗口中运行它,从它在" Debug"夹。现在外部进程启动但挂起。有一些STDERR输出,我已经关闭了。外部任务有DiskIO。

let video : NSTask = NSTask()
video.launchPath = "./ffmpeg"
video.arguments = ["-i", "\(item)", "-c:v","copy", "-bsf:v", "h264_mp4toannexb", "-an", "-y", "-loglevel", "quiet", "\(path).h264"]

//also tried without the following two lines
video.standardError = NSFileHandle.fileHandleWithStandardError()
video.standardOutput = NSFileHandle.fileHandleWithStandardOutput()

video.launch()
video.waitUntilExit()

是:我将所有内容复制到当前路径,以便执行起作用。它从终端开始运行但挂起。

现在问题出现了:为什么?!我在这做错了什么?简单的解决方案是始终在Xcode中运行它,但正如您可能想象的那样,使用命令行工具非常不方便。

1 个答案:

答案 0 :(得分:5)

您需要从/dev/null重定向标准输入。 ffmpeg有一个打开的stdin,正在等待该管道上的更多数据。

video.standardInput = NSFileHandle.fileHandleWithNullDevice()

请注意,您不需要对standardErrorstandardOutput进行分配。这些是默认设置。

这适用于Xcode,因为调试器会关闭stdin。