Mike Schmidt
Mike Schmidt

Reputation: 1065

How to Capture Video from Session in Swift?

I have a standard view controller that uses a capture session and previews what the camera is seeing. Now that I have that, I would like the button that I have to perform an action (in the saveVideo action at the bottom) and start recording video. What kind of code do I need to actually save the frames of the output?

import UIKit
import AVFoundation

class ViewController: UIViewController {


    var captureSession = AVCaptureSession();
    var sessionOutput = AVCaptureVideoDataOutput();
    var previewLayer = AVCaptureVideoPreviewLayer();


    override func viewDidAppear(_ animated: Bool) {
        captureSession.startRunning()
    }


    override func viewWillAppear(_ animated: Bool) {
         let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)

    for device in (deviceDiscoverySession?.devices)!{
        if(device.position == AVCaptureDevicePosition.back){

            do{
                let input = try AVCaptureDeviceInput(device: device )

                if captureSession.canAddInput(input){

                    captureSession.addInput(input)

                }

               // sessionOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange as UInt32)]

                sessionOutput.alwaysDiscardsLateVideoFrames = true

                if(captureSession.canAddOutput(sessionOutput) == true){
                    captureSession.addOutput(sessionOutput)

                    let previewLayer: AVCaptureVideoPreviewLayer = {
                        let preview =  AVCaptureVideoPreviewLayer(session: self.captureSession)
                        preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
                        preview?.position = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
                        preview?.videoGravity = AVLayerVideoGravityResize
                        return preview!
                    }()

                    view.layer.insertSublayer(previewLayer, at: 0)



                }

                captureSession.commitConfiguration()

            }

            catch{
                print("Error")
            }


        }

    }


}

    @IBAction func saveVideo(_ sender: Any) {
    }
}

Upvotes: 0

Views: 330

Answers (1)

Gurdev Singh
Gurdev Singh

Reputation: 2165

You need to set the delegate of your AVSession to your ViewController which will then invoke the delegate methods which will be triggered on different states of your Session.

For more information see https://developer.apple.com/reference/avfoundation/avcapturesession

Specifically captureOutput delegate method will be called from your delegate in your ViewController.

Upvotes: 1

Related Questions