[这里,我正在尝试记录视频并处理输出缓冲区,我正在使用avfoundation摄像机来记录视频,并以示例缓冲区委托方法收集输出,并在图像视图中显示该输出。每件事都可以找到,但是如何从中保存视频这里可以有人请帮助我。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;{
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
UIImage *fImage = [self addText:image text:@"u r text here"];//image will be processed here
self.dispFrames.image = fImage;
}
您必须使用音频/视频输入设置AVAssetWriter并附加示例缓冲区。您可以签出此示例代码RosyWriterVideoProcessor类https://developer.apple.com/library/ios/samplecode/RosyWriter/Listings/Classes_RosyWriterVideoProcessor_m.html#//apple_ref/doc/uid/DTS40011110-Classes_RosyWriterVideoProcessor_m-DontLinkElementID_8
我用Swift 5 AVFoundation编写了示例项目。一个控制器中的照片和摄像机。在前后摄像头之间具有切换器。通过this link进行检查>
项目的视频输出代码:
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { let showMediaController = ShowMediaController() showMediaController.url = outputFileURL navigationController?.pushViewController(showMediaController, animated: true) }
在ShowMediaConroller中,我获取新视频的URL并可以播放:
@objc private func handlePlay() {
if let url = url {
player = AVPlayer(url: url)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer?.frame = view.frame
view.layer.insertSublayer(playerLayer!, at: 2)
player?.play()
playButton.isHidden = true
}
}