Issue
我正在使用 AVFoundation 来实现一个相机,它能够在运行特殊的人工智能处理时录制视频。
拥有一个AVCaptureMovieFileOutput
(用于视频录制)and a AVCaptureVideoDataOutput
(用于处理AI)不支持同时运行(参见可以同时使用AVCaptureVideoDataOutput和AVCaptureMovieFileOutput吗? https://stackoverflow.com/q/4944083/5281431),所以我决定使用一个AVCaptureVideoDataOutput
它能够将视频录制到文件中,同时运行人工智能处理captureOutput(...)
打回来。
令我惊讶的是,这样做大大增加了 RAM 使用量58 MB to 187 MB(!!!),以及CPU3-5% to 7-12% 空闲时。在实际录制时,RAM 会增加更多(260 MB!).
我想知道我在这里做错了什么,因为我禁用了所有人工智能处理,只是比较了之间的差异AVCaptureMovieFileOutput
and AVCaptureVideoDataOutput
.
My code:
AVCaptureMovieFileOutput
Setup
if let movieOutput = self.movieOutput {
captureSession.removeOutput(movieOutput)
}
movieOutput = AVCaptureMovieFileOutput()
captureSession.addOutput(movieOutput!)
Delegate
(嗯,没有,AVCaptureMovieFileOutput
内部处理所有这些)
基准
空闲时,根本不记录:
录音时使用AVCaptureMovieFileOutput.startRecording
:
- RAM: 56 MB(如何???)
- CPU: 20-30%
AVCaptureVideoDataOutput
Setup
// Video
if let videoOutput = self.videoOutput {
captureSession.removeOutput(videoOutput)
self.videoOutput = nil
}
videoOutput = AVCaptureVideoDataOutput()
videoOutput!.setSampleBufferDelegate(self, queue: videoQueue)
videoOutput!.alwaysDiscardsLateVideoFrames = true
captureSession.addOutput(videoOutput!)
// Audio
if let audioOutput = self.audioOutput {
captureSession.removeOutput(audioOutput)
self.audioOutput = nil
}
audioOutput = AVCaptureAudioDataOutput()
audioOutput!.setSampleBufferDelegate(self, queue: audioQueue)
captureSession.addOutput(audioOutput!)
Delegate
extension CameraView: AVCaptureVideoDataOutputSampleBufferDelegate,
AVCaptureAudioDataOutputSampleBufferDelegate {
public final func captureOutput(_ captureOutput: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from _: AVCaptureConnection) {
// empty
}
public final func captureOutput(_ captureOutput: AVCaptureOutput,
didDrop buffer: CMSampleBuffer,
from _: AVCaptureConnection) {
// empty
}
}
是的,它们实际上是空方法。我的 RAM 和 CPU 使用率仍然那么高,没有在这里做任何工作。
基准
空闲时,根本不记录:
- RAM: 151-187 MB
- CPU: 7-12%
使用自定义录制时AVAssetWriter
:
为什么是AVCaptureMovieFileOutput
比空的效率高得多AVCaptureVideoDataOutput
?另外,与我的录制相比,为什么它的 RAM 在录制时根本没有上升AVAssetWriter
仅实施就消耗80 MB?
这是我的习惯AVAssetWriter
执行:RecordingSession.swift https://github.com/cuvent/react-native-vision-camera/blob/frame-processors/ios/RecordingSession.swift, and 这就是我所说的地方 https://github.com/cuvent/react-native-vision-camera/blob/a48ca839e93e6199ad731f348e19427774c92821/ios/CameraView%2BRecordVideo.swift#L16-L86.
任何帮助表示赞赏!