Save Video Using AVFoundation Swift

后端 未结 6 1541
感情败类
感情败类 2021-02-05 08:57

Hi I followed a course by Jared Davidson to create a custom camera view and save pictures using AVFoundation. https://www.youtube.com/watch?v=w0O3ZGUS3pk

However I would

6条回答
  •  醉梦人生
    2021-02-05 09:39

    You can save record your video to file by creating and adding an AVCaptureMovieFileOutput to your capture session, and making your ViewController conform to the AVCaptureFileOutputRecordingDelegate.

    This example records 5 seconds of video to a file called "output.mov" in the app's Documents directory.

    class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
    
        var captureSession = AVCaptureSession()
        var sessionOutput = AVCaptureStillImageOutput()
        var movieOutput = AVCaptureMovieFileOutput()
        var previewLayer = AVCaptureVideoPreviewLayer()
    
        @IBOutlet var cameraView: UIView!
    
        override func viewWillAppear(animated: Bool) {
            self.cameraView = self.view
    
            let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
            for device in devices {
                if device.position == AVCaptureDevicePosition.Front{
    
    
                    do{
    
                        let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)
    
                        if captureSession.canAddInput(input){
    
                            captureSession.addInput(input)
                            sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
    
                            if captureSession.canAddOutput(sessionOutput){
    
                                captureSession.addOutput(sessionOutput)
    
                                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                                previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                                previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
                                cameraView.layer.addSublayer(previewLayer)
    
                                previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
                                previewLayer.bounds = cameraView.frame
    
    
                            }
    
                            captureSession.addOutput(movieOutput)
    
                            captureSession.startRunning()
    
                            let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
                            let fileUrl = paths[0].URLByAppendingPathComponent("output.mov")
                            try? NSFileManager.defaultManager().removeItemAtURL(fileUrl)
                            movieOutput.startRecordingToOutputFileURL(fileUrl, recordingDelegate: self)
    
                            let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))
                            dispatch_after(delayTime, dispatch_get_main_queue()) {
                                print("stopping")
                                self.movieOutput.stopRecording()
                            }
                        }
    
                    }
                    catch{
    
                        print("Error")
                    }
    
                }
            }
    
        }
    
        func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
            print("FINISHED \(error)")
            // save video to camera roll
            if error == nil {
                UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path!, nil, nil, nil)
            }
        }
    
    }
    

提交回复
热议问题