录制一秒钟视频Swift?

时间:2017-01-19 00:33:10

标签: ios swift avfoundation

我正在使用AVCaptureFileOutputRecordingDelegate来录制视频。我想保存一秒钟的视频,然后更改文件路径并将其下一秒保存在其他地方。

这可能吗?我需要设置某种计时器吗?下面是我的记录功能中的代码:

 let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
 let outputPath = "\(documentsPath)/output.mov"
 outputURL = NSURL(fileURLWithPath: outputPath)
 output.startRecording(toOutputFileURL: outputURL as URL!, recordingDelegate: self)

以下是我设置输出记录控制器和所有内容的代码:

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)

                    output = AVCaptureMovieFileOutput()
                    let maxDuration = CMTimeMakeWithSeconds(180, 30)
                    output.maxRecordedDuration = maxDuration
                    captureSession.addOutput(output)



                }

                captureSession.commitConfiguration()

            }

            catch{
                print("Error")
            }


        }

    }

    let deviceDiscoverySession2 = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInMicrophone], mediaType: AVMediaTypeAudio, position: AVCaptureDevicePosition.unspecified)

    for device in (deviceDiscoverySession2?.devices)!{


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

            if captureSession.canAddInput(input){

                captureSession.addInput(input)

            }



        captureSession.commitConfiguration()

        }

        catch{
            print("Error")
        }





}

}

1 个答案:

答案 0 :(得分:1)

在Mac OS X AVCaptureMovieFileOutput上可以做你想做的事。来自超类AVCaptureFileOutput中的评论:

  

文件输出可以使用startRecordingToOutputFileURL:recordingDelegate:方法开始录制到新文件。在Mac OS X上连续调用此方法时,可以动态更改输出文件,而不会丢失媒体样本。

在iOS上,您必须回退到为每个细分创建,开始和停止的AVCaptureVideoDataOutputAVCaptureAudioDataOutputAVAssetWriter组合。

这不是最简单的任务。你想要实现什么目标?您是否可以录制到单个文件,然后在后处理阶段将其拆分为多个片段?