swift2 如何在保存和裁剪后将前摄像头记录旋转90度?

irtuqstp  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(216)

我得到的问题与前置摄像头视频录制。它是旋转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)
        }
    }
}
lf5gs5x2

lf5gs5x21#

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

相关问题