I am looking for a way to save the depth information (for each frame) to a file while recording a video using the TrueDepth camera. I have found solutions for saving the depth data while taking photos, but not with video.
I currently have code for recording video and saving it to a file and capturing the depth data simultaneously. However, I don't know how to save the depth data. Basically I use AVCaptureSession and AVCaptureMovieFileOutput to save the recorded video. I found that the depth data is given by AVCaptureDepthDataOutput, but how can I save that to a file?
The main parts of the code:
let captureSession = AVCaptureSession()
let sessionOutput = AVCapturePhotoOutput()
let movieOutput = AVCaptureMovieFileOutput()
let depthDataOutput = AVCaptureDepthDataOutput()
if let device = AVCaptureDevice.default(.builtInTrueDepthCamera,
for: .video, position: .front) {
let input = try AVCaptureDeviceInput(device: device )
if captureSession.canAddInput(input){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
captureSession.addInput(input)
if captureSession.canAddOutput(sessionOutput){
captureSession.addOutput(sessionOutput)
// Code for adding previewing the video
}
}
if captureSession.canAddOutput(depthDataOutput){
captureSession.addOutput(depthDataOutput)
}
if let connection = depthDataOutput.connection(with: .depthData) {
connection.isEnabled = true
depthDataOutput.isFilteringEnabled = false
print("Depth data working")
} else {
print("No AVCaptureConnection")
}
// Capture the video to a file
captureSession.addOutput(movieOutput)
captureSession.startRunning()
// ...
// How to store the depth data from AVCaptureDepthDataOutput ?
}
Any ideas?