How to record video using TraceVisionSDK
Set up a video recording session and detect highlights in real-time
Overview
Video recording session allows you to record the video with the rear camera of your device and simultaneously process the video to detect highlights in real time. The session can export the full video to the camera roll and save the highlights to view later with HighlightVideoPlayer or export them to the camera roll as well.
Note
Currently camera is initialized with 16:9 1080p @ 30fps video format.Create Video Recorder Session
With TraceVision you can create a video recording session using TraceVision/createVideoRecordSession(exportFullVideo:) that you call from the shared instance of the SDK. The exportFullVideo parameter is set to true by default and the session will save the full video to the camera roll at the end of the recording.
let session = TraceVision.shared.createVideoRecordSession()
Video recording session provides the observable status object to get the current state of the recording as well as access the highlights found in the video.
Initialize the camera and preview
Before you start the recording session you need to initialize the camera and the preview to see what is being recorded. You can use the initVideoRecorder(orientation:) method to do that. You will need to pass the current orientation of the device for the camera to reflect it.
If the device is rotated and it’s orientation changes make sure to call changeVideoOrientation(_:) method to adjust the camera.
To be able to see the preview you will need to acquire the previewLayer from the session and put it into your view on the screen. Use CameraPreview SwiftUI view to easily do that.
/// The preview layer for the camera.
///
/// We set it in on `appear()` after the camera is initialized
@State
var previewLayer: AVCaptureVideoPreviewLayer? = nil
/// Observe the processing/recording status via this status object
///
/// We connect it to the actual session in `appear()` when we initialize our session
@StateObject
var videoStatus: VideoRecorderStatus = VideoRecorderStatus()
func startCamera() {
// we set our own copy of the status object here to have direct updates from the session
session.setStatusObject(videoStatus)
session.initVideoRecorder(device2cameraOrientation(UIDevice.current.orientation))
previewLayer = session.previewLayer
}
var body: some View {
ZStack {
CameraPreview(previewLayer: $previewLayer)
....
}
.onAppear() {
startCamera()
}
.onDeviceRotation { orientation in
session.changeVideoOrientation(device2cameraOrientation(orientation))
}
Start/stop the recording
To start the recording call startRecording() method. The recording is starting in the background and you can observe processing property to get the current state of the recording. The property is updated on main thread as soon as the recording starts and stops, so it is safe to use it in the UI.
To stop the recording call stopRecording() method and again observe processing to know it’s stopped completely.
func toggleRecording() {
if videoStatus.processing == true {
// Stop the video recording.
// Observe `videoStatus.processing` to become `false`
// then you'll know that it's fully stopped.
session.stopRecording()
} else {
// Start the video recording and highlights detection on the fly.
session.startRecording()
}
Stopping the camera
When you are done with the camera and the preview you can stop the camera and release the resources by calling stopVideoRecorder() method. It is advised to call this method in onDisappear of the view.
Note
It is recommended to call this method when the recording is stopped but in case it’s called during the active recording this method will stop the recording first and then shut the camera down. ...
.onDisappear() {
stopCamera()
}
func stopCamera() {
session.stopVideoRecorder()
}
Getting the highlights
The highlights are detected in real-time and you can observe the highlights property of the session status to get the highlights as soon as they are detected. The property is updated on the main thread and you can use it in the UI to display the highlights to the user.
...
.onChange(of: videoStatus.highlights) { highlights in
// Do something with the highlights
}
HowToRecordVideo Reference