如何将捕获的视频直接保存到Firebase与本地?

时间:2017-12-27 19:40:53

标签: swift firebase video firebase-storage

我构建了一个使用自定义相机录制视频和拍照的应用。我已成功将照片保存到Firebase存储,但我无法弄清楚如何访问该视频以将其保存在那里。

这是我的代码:

recordViewController

func setupCaptureSession() {
    captureSession.sessionPreset = AVCaptureSession.Preset.high
}

func setupDevice() {
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
    let devices = deviceDiscoverySession.devices

    for device in devices {
        if device.position == AVCaptureDevice.Position.back {
            backCamera = device
        } else if device.position == AVCaptureDevice.Position.front {
            frontCamera = device
        }
    }

    currentCamera = backCamera
}

func setupInputOutput() {
    do {
        let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
        captureSession.addInput(captureDeviceInput)
        videoFileOutput = AVCaptureMovieFileOutput()
        captureSession.addOutput(videoFileOutput!)
    } catch {
        print(error)
    }
}

func setupPreviewLayer() {
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    cameraPreviewLayer?.frame = self.view.frame
    self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}

func startRunningCaptureSession() {
    captureSession.startRunning()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "playVideo" {
        let videoPlayerViewController = segue.destination as! AVPlayerViewController
        let videoFileURL = sender as! URL
        videoPlayerViewController.player = AVPlayer(url: videoFileURL)
    }
}

// MARK: - AVCaptureFileOutputRecordingDelegate methods
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
    if error != nil {
        print(error)
        return
    }

    performSegue(withIdentifier: "playVideo", sender: outputFileURL)
}

// Actions

@IBAction func recordButtonTapped(_ sender: Any) {
    if !isRecording {
        isRecording = true

        let outputPath = NSTemporaryDirectory() + "output.mov"
        let outputFileURL = URL(fileURLWithPath: outputPath)
        videoFileOutput?.startRecording(to: outputFileURL, recordingDelegate: self)
        recordButton.isSelected = true
    } else {
        isRecording = false

        recordButton.isSelected = false
        recordButton.layer.removeAllAnimations()
        videoFileOutput?.stopRecording()
    }
}

录制视频后,它会将其本地保存到临时位置。我不知道如何找到当前位置路径并将其更改为Firebase存储路径。

0 个答案:

没有答案