简体   繁体   中英

Swift and NSTask hangs

i am new to the Mac World. I'm using Swift and i am trying to run external processes one at a time. Now everything works fine, as long, as it is debugged, by which I mean: Run in Xcode with Debugger attached.

I do not change anything and try to run it in the terminal window, from it's place in the "Debug" folder. Now the external process starts but hangs.There is some STDERR output, which I already switched off. And there is DiskIO by the external Task.

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()

Yes: I copied everything to the current path, so that execution works. It starts but hangs when run from terminal.

Now the Question arises: WHY?! What am I doing wrong here? The easy solution would be to always run it in Xcode, but as you might imagine, that is quite inconvenient with a command line tool.

You need to redirect stdin from /dev/null . ffmpeg has an open stdin and is waiting for more data on that pipe.

video.standardInput = NSFileHandle.fileHandleWithNullDevice()

Note that you don't need your assignments to standardError and standardOutput . Those are the default settings.

This works in Xcode because the debugger closes stdin for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM