如何从avfoundation相机获取视频输出

时间:2013-08-30 06:10:32

标签: iphone ios avfoundation avcapturesession avcapture

这里我正在尝试录制视频和处理输出缓冲区,我正在使用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;

   }

2 个答案:

答案 0 :(得分:0)

您必须使用音频/视频输入设置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

答案 1 :(得分:0)

我用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中,我获取新视频的网址并可以播放:

@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
    }
}