feat: Video Codec Option for recording video (#645)

* add video codec value

* add types

* use `recommendedVideoSettings` method instead

* lint

* refactor for better readability

* add a method to get available codecs (ios)

* imrove tsDoc description of the videoCodec option

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>

* ios format

Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>
This commit is contained in:
Paul Rostorp
2021-12-30 10:47:23 +01:00
committed by GitHub
parent 7460560323
commit d96c5863c9
5 changed files with 73 additions and 3 deletions

View File

@@ -108,12 +108,18 @@ extension CameraView: AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAud
return
}
var videoCodec: AVVideoCodecType?
if let codecString = options["videoCodec"] as? String {
videoCodec = AVVideoCodecType(withString: codecString)
}
// Init Video
guard let videoSettings = videoOutput.recommendedVideoSettingsForAssetWriter(writingTo: fileType),
guard let videoSettings = self.recommendedVideoSettings(videoOutput: videoOutput, fileType: fileType, videoCodec: videoCodec),
!videoSettings.isEmpty else {
callback.reject(error: .capture(.createRecorderError(message: "Failed to get video settings!")))
return
}
// get pixel format (420f, 420v, x420)
let pixelFormat = CMFormatDescriptionGetMediaSubType(videoInput.device.activeFormat.formatDescription)
self.recordingSession!.initializeVideoWriter(withSettings: videoSettings,
@@ -256,6 +262,14 @@ extension CameraView: AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAud
}
}
private func recommendedVideoSettings(videoOutput: AVCaptureVideoDataOutput, fileType: AVFileType, videoCodec: AVVideoCodecType?) -> [String: Any]? {
if videoCodec != nil {
return videoOutput.recommendedVideoSettings(forVideoCodecType: videoCodec!, assetWriterOutputFileType: fileType)
} else {
return videoOutput.recommendedVideoSettingsForAssetWriter(writingTo: fileType)
}
}
private var isReadyForNewEvaluation: Bool {
let lastPerformanceEvaluationElapsedTime = DispatchTime.now().uptimeNanoseconds - lastFrameProcessorPerformanceEvaluation.uptimeNanoseconds
return lastPerformanceEvaluationElapsedTime > 1_000_000_000