如何在保存和裁剪后立即将前置摄像头旋转 90 度?

How to rotate front camera recording 90 degree right after saving and cropping?

我遇到了前置摄像头视频录制的问题。保存视频后向左旋转 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 标志设置前后摄像头。