Search code examples
xcodeswiftvideosaveavcapturesession

xcode Swift Save Recorded Video as mov or mp4


I have been having a hard time searching for a way to save a recorded video using AVCaptureSession

Here is my code:

    @IBOutlet weak var tv: UIView!
    var session:AVCaptureSession?
    var output: AVCaptureVideoDataOutput?
    var previewLayer: AVCaptureVideoPreviewLayer?
    var out:AVCaptureMovieFileOutput!
    var outPath:NSURL?

...on the view didload function

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let filemgr = NSFileManager.defaultManager()
        let documentsURL = filemgr.URLsForDirectory(.DesktopDirectory, inDomains: .UserDomainMask)[0]
        outPath = documentsURL.URLByAppendingPathComponent("temp")

        session = AVCaptureSession()
        session!.sessionPreset = AVCaptureSessionPresetLow

        let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        var deviceInput:AVCaptureDeviceInput?

        do{
            try deviceInput = AVCaptureDeviceInput(device: camera)
        }catch{
        }
        session?.addInput(deviceInput)
        previewLayer = AVCaptureVideoPreviewLayer(session: session)
        let frame = CGRect(x: 0, y: 0, width: self.tv.frame.width, height: self.tv.frame.height)
        tv.layer.addSublayer(previewLayer!)
        previewLayer?.frame = frame
        session?.startRunning()

record button action

        let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
        out = AVCaptureMovieFileOutput()
        session?.addOutput(out)
        out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate)

...stop button action

        out.stopRecording()

..and delegate functions

    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outPath: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!){
        print("record fin")
        return
    }
    func captureOutput(captureOutPut: AVCaptureFileOutput!, didStartRecordingTOOutputFileAtURL outPath: NSURL!, fromConnections connections: [AnyObject]!){
        print("record start")
        return
    }

Thanks for any help :)

EDIT: Here are some additional details

There are no errors or warning in the code and the app runs just fine The camera preview shows but the problem is on saving the recorded video, when the out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate) is called, it does not save any file


Solution

  • in case somebody happens to have the same problem as mine, here is the solution

    add this code inside the delegate function didfinishrecording

        let library = PHPhotoLibrary.sharedPhotoLibrary()
        library.performChanges({
            PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outPath)!
            }, completionHandler: {success, error in
                NSLog("Finished saving asset. %@", (success ? "Success." : error!))
        })
    

    regards