前置摄像头视频录制出现问题。保存视频后将其向左旋转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标志设置您的前后摄像头。

09-28 11:56