ios - 保存并裁剪后如何将前置摄像头录制旋转90度?

标签 ios swift video-capture swift2

我在使用前置摄像头视频录制时遇到问题。保存视频后向左旋转90度。我想将视频向右旋转 90 度。

这是预览层代码

func createPreviewLayer()
{
    // previewLayer = nil
    previewLayer = AVCaptureVideoPreviewLayer(session:self.captureSession)
    previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
    previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
    previewLayer?.frame = CGRectMake(0,0, self.view.frame.width, self.view.frame.height)
    self.view.layer.addSublayer(previewLayer!)
    //Do any additional setup after loading the view, typically from a nib.
    // cameraPosion = !cameraPosion

    dispatch_async(dispatch_get_main_queue(), {

        // let orientation : AVCaptureVideoOrientation =  AVCaptureVideoOrientation.Portrait
        // self.previewLayer?.connection.videoOrientation = orientation
        //  (previewLayer.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = orientation

    })

    captureSession.startRunning()
    let image = UIImage(named: "camera") as UIImage?
    // recordButton.frame = CGRectMake((self.view.frame.width/2)-25, self.view.frame.height-70, 50, 50)
    recordButton.frame = CGRectMake(self.view.frame.width-75, (self.view.frame.height/2)-37.5, 75, 75)
    lblCountDownMessage.frame=CGRectMake((self.view.frame.width/2)-70, 0, 150, 60)

    lblCountDown.frame=CGRectMake((self.view.frame.width/2)-20, (self.view.frame.height/2)-30, 40, 60)
    lblCountDown.textAlignment=NSTextAlignment.Center
    lblCountDown.textColor = UIColor.redColor()
    lblCountDownMessage.textColor = UIColor.redColor()
    lblCountDownMessage.textAlignment=NSTextAlignment.Center
    var toggleCameraButton = UIButton()
    let imageToggleCam = UIImage(named: "switch") as UIImage?
    toggleCameraButton.frame = CGRectMake(self.view.frame.width-65, 10, 50, 40)
    toggleCameraButton.setImage(imageToggleCam, forState: .Normal)
    recordButton.setImage(image, forState: .Normal)
    self.view.addSubview(recordButton)
    self.view.addSubview(lblCountDown)
    self.view.addSubview(lblCountDownMessage)
    self.view.addSubview(toggleCameraButton)
    lblCountDown.text = "3"
    let  font = UIFont(name: "Helvetica", size: 50.0)
    lblCountDown.font=font
    lblCountDown.hidden = false
    lblCountDownMessage.text = "Recording starts in"
    lblCountDownMessage.hidden = false

    recordButton.addTarget(self, action: "recordButtonOnClick", forControlEvents: UIControlEvents.TouchUpInside)
    toggleCameraButton.addTarget(self, action: "toggleCameraBackFront", forControlEvents: UIControlEvents.TouchUpInside)
}

这是用于修剪视频的代码片段。它工作正常,但节省了 90 度左旋转。

这是视频修剪代码:

func trimVideo(sourceURL: NSURL, destinationURL: NSURL, trimPoints: TrimPoints, completion: TrimCompletion?) {
    assert(sourceURL.fileURL)
    assert(destinationURL.fileURL)

    let options = [ AVURLAssetPreferPreciseDurationAndTimingKey: true ]
    let asset = AVURLAsset(URL: sourceURL, options: options)
    let preferredPreset = AVAssetExportPresetPassthrough
    if verifyPresetForAsset(preferredPreset, asset: asset) {
        let composition = AVMutableComposition()
        let videoCompTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
        let audioCompTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

        let assetVideoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first as! AVAssetTrack
        let assetAudioTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeAudio).first as! AVAssetTrack

        var compError: NSError?

        var accumulatedTime = kCMTimeZero
        for (startTimeForCurrentSlice, endTimeForCurrentSlice) in trimPoints {
            let durationOfCurrentSlice = CMTimeSubtract(endTimeForCurrentSlice, startTimeForCurrentSlice)
            let timeRangeForCurrentSlice = CMTimeRangeMake(startTimeForCurrentSlice, durationOfCurrentSlice)

            videoCompTrack.insertTimeRange(timeRangeForCurrentSlice, ofTrack: assetVideoTrack, atTime: accumulatedTime, error: &compError)
            audioCompTrack.insertTimeRange(timeRangeForCurrentSlice, ofTrack: assetAudioTrack, atTime: accumulatedTime, error: &compError)

            if compError != nil {
                NSLog("error during composition: \(compError)")
                if let completion = completion {
                    completion(compError)
                }
            }

            accumulatedTime = CMTimeAdd(accumulatedTime, durationOfCurrentSlice)
        }

        let exportSession = AVAssetExportSession(asset: composition, presetName: preferredPreset)
        exportSession.outputURL = destinationURL
        exportSession.outputFileType = AVFileTypeAppleM4V
        exportSession.shouldOptimizeForNetworkUse = true

        removeFileAtURLIfExists(destinationURL)

        exportSession.exportAsynchronouslyWithCompletionHandler({ () -> Void in
            if let completion = completion {
                completion(exportSession.error)
            }
        })
    } else {
        NSLog("Could not find a suitable export preset for the input video")
        let error = NSError(domain: "org.linuxguy.VideoLab", code: -1, userInfo: nil)
        if let completion = completion {
            completion(error)
        }
    }
}

最佳答案

将 AVCaptureDevice 和 avcapturesession 设为 nil,并根据 bool 标志设置前后摄像头。

关于ios - 保存并裁剪后如何将前置摄像头录制旋转90度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500697/

相关文章:

ios - 为什么 Y 平面和 CbCr 平面报告的大小不同?

ios - 自动布局的 UILabel 高度不起作用

iphone - iphone 中的 Adwhirl 集成问题

ios - 如何在 IOS Delphi 上的 TWebbrowser 中设置用户代理

swift - 我的代码有什么问题?方法不能是@objc

c++ - OpenCV VideoCapture IP 摄像机重新连接

ios - 移动设备上的 HTML5 媒体捕获 - 视频时长

ios - Swift 启动时的 BLE 扫描

iOS CPU 事件突然停止

ios - 无法将类型 '[(string, string)]' 的值分配给类型 '[string : string]'