How to get video output from avfoundation camera

743 views Asked by At

Here i am trying to record video and process output buffers , i am using avfoundation camera to record video and collecting output at sample buffer delegate method and displaying that output in image view.every thing works find but the thing how to save video from here can any one please help me.

- (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

There are 2 answers

0
Laz On

You have to setup AVAssetWriter with Audio/Video inputs and append your sample buffers. You can checkout this sample code, RosyWriterVideoProcessor class https://developer.apple.com/library/ios/samplecode/RosyWriter/Listings/Classes_RosyWriterVideoProcessor_m.html#//apple_ref/doc/uid/DTS40011110-Classes_RosyWriterVideoProcessor_m-DontLinkElementID_8

0
Sapar Friday On

I write sample project with Swift 5 AVFoundation. Photo and video camera in one controller. With switcher between front and back camera. Check it by this link

Video output code from project:

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
    let showMediaController = ShowMediaController()
    showMediaController.url = outputFileURL
    navigationController?.pushViewController(showMediaController, animated: true)
}

In ShowMediaConroller I take url of new video and can play it:

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