我有两个相机输入进入 OSX 应用程序,我正在尝试使用 AVCaptureMovieFileOutput 保存它们。他们的视频很快就会不同步。经过最小测试后,它们可以关闭 1 到 5 秒。经过一个小时的测试后,它们将关闭 20 秒。我觉得必须有某种简单的解决方案来保持两个输出同步。我们尝试对会话和输出使用相同的设备,但遇到了同样的问题。我们尝试将 fps 降至 15,但仍然没有运气。
设置输出
func assignDeviceToPreview(captureSession: AVCaptureSession, device: AVCaptureDevice, previewView: NSView, index: Int){
captureSession.stopRunning()
captureSession.beginConfiguration()
//clearing out old inputs
for input in captureSession.inputs {
let i = input as! AVCaptureInput
captureSession.removeInput(i)
}
let output = self.outputs[index]
output.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
//removing old outputs
for o in captureSession.outputs{
if let oc = o as? AVCaptureStillImageOutput{
captureSession.removeOutput(oc)
print("removed image out")
}
}
//Adding input
do {
try captureSession.addInput(AVCaptureDeviceInput(device:device))
let camViewLayer = previewView.layer!
camViewLayer.backgroundColor = CGColorGetConstantColor(kCGColorBlack)
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = camViewLayer.bounds
previewLayer.autoresizingMask = [.LayerWidthSizable, .LayerHeightSizable]
camViewLayer.addSublayer(previewLayer)
let overlayPreview = overlayPreviews[index]
overlayPreview.frame.origin = CGPoint.zero
previewView.addSubview(overlayPreview)
//adding output
captureSession.addOutput(output)
if captureSession == session2{
let audio = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
do {
let input = try AVCaptureDeviceInput(device: audio)
captureSession.addInput(input)
}
}
} catch {
print("Failed to add webcam as AV input")
}
captureSession.commitConfiguration()
captureSession.startRunning()
}
开始录制
func startRecording(){
startRecordingTimer()
let base = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let appFolder = "Sessions"
let sessionFolder = "session_" + session.UUID
let path = base+"/"+appFolder+"/"+sessionFolder
do{
try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
}catch{
print("issue creating folder")
}
for fileOutput in fileOutputs{
let fileName = "cam\(String(fileOutputs.indexOf(fileOutput)!))" + ".mov"
let fileURL = NSURL.fileURLWithPathComponents([path, fileName])
fileURLs.append(fileURL!)
print(fileURL?.absoluteString)
var captureConnection = fileOutput.connections.first as? AVCaptureConnection
captureConnection!.videoMinFrameDuration = CMTimeMake(1, 15)
captureConnection!.videoMaxFrameDuration = CMTimeMake(1, 15)
if fileOutput == movieFileOutput1{
fileOutput.setOutputSettings([AVVideoScalingModeKey: AVVideoScalingModeResize, AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: 1280, AVVideoHeightKey: 720], forConnection: captureConnection)
}else{
fileOutput.setOutputSettings([AVVideoScalingModeKey: AVVideoScalingModeResizeAspect, AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: 640, AVVideoHeightKey: 360], forConnection: captureConnection)
}
captureConnection = fileOutput.connections.first as? AVCaptureConnection
print(fileOutput.outputSettingsForConnection(captureConnection))
fileOutput.startRecordingToOutputFileURL(fileURL, recordingDelegate: self)
print("start recording")
}
}