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:
@@ -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
|
||||
|
@@ -57,4 +57,6 @@ RCT_EXTERN_METHOD(stopRecording:(nonnull NSNumber *)node resolve:(RCTPromiseReso
|
||||
RCT_EXTERN_METHOD(takePhoto:(nonnull NSNumber *)node options:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject);
|
||||
RCT_EXTERN_METHOD(focus:(nonnull NSNumber *)node point:(NSDictionary *)point resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject);
|
||||
|
||||
RCT_EXTERN_METHOD(getAvailableVideoCodecs:(nonnull NSNumber *)node fileType:(NSString *)fileType resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject);
|
||||
|
||||
@end
|
||||
|
@@ -72,6 +72,26 @@ final class CameraViewManager: RCTViewManager {
|
||||
component.focus(point: CGPoint(x: x.doubleValue, y: y.doubleValue), promise: promise)
|
||||
}
|
||||
|
||||
@objc
|
||||
final func getAvailableVideoCodecs(_ node: NSNumber, fileType: String?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
||||
withPromise(resolve: resolve, reject: reject) {
|
||||
let component = getCameraView(withTag: node)
|
||||
guard let videoOutput = component.videoOutput else {
|
||||
throw CameraError.session(SessionError.cameraNotReady)
|
||||
}
|
||||
|
||||
var parsedFileType = AVFileType.mov
|
||||
if fileType != nil {
|
||||
guard let parsed = try? AVFileType(withString: fileType!) else {
|
||||
throw CameraError.parameter(ParameterError.invalid(unionName: "fileType", receivedValue: fileType!))
|
||||
}
|
||||
parsedFileType = parsed
|
||||
}
|
||||
|
||||
return videoOutput.availableVideoCodecTypesForAssetWriter(writingTo: parsedFileType).map(\.descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
@objc
|
||||
final func getAvailableCameraDevices(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
||||
withPromise(resolve: resolve, reject: reject) {
|
||||
|
Reference in New Issue
Block a user