I want to call ffmpeg to get the duration of a video file. When using the command in OSX Terminal everything works fine:
ffmpeg -i MyVideo.MOV 2>&1 | grep "Duration"
I get this:
Duration: 00:01:23.53, start: 0.000000, bitrate: 39822 kb/s
It is perfect for me. But now I tried this call from within my code:
func shell(launchPath: String, arguments: [String]) -> String
{
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
do {
try task.run()
// task.launch() till 10.12, but now catchable!
} catch let error as NSError {
print(error.localizedDescription)
return ""
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
This code works fine for all other external commands. But here I get error:
[NULL @ 0x107808800] Unable to find a suitable output format for '2>&1'
2>&1: Invalid argument
I defined the arguments for ffmpeg like this:
let arguments = ["-i", video.path, "2>&1", "|", "grep \"Duration\"" ]
Even if I put them all in one argument as a larger string, it doesn't work. Using "pipe:1" instead of "2>&1" and rest of arguments results also in an error.
Any idea, how I get it to work?