添加水印时,为什么我的视频会旋转?

时间:2016-08-03 17:27:58

标签: swift calayer avassetexportsession avmutablecomposition

我使用AVAssetExportSession裁剪视频并且没有问题:

func cropVideo() {

        let videoAsset = AVAsset(URL: recordedVideoURL)

        let mixComposition = AVMutableComposition()

        do {

            let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))


            //First 5 seconds
            try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, CMTimeMake(5, 1)), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: kCMTimeZero)

            //Middle 5 seconds
            let middleStart = CMTimeMake(Int64(gameTime/2), 1)
            let middleEnd = CMTimeMake(Int64(((gameTime/2) + 5)), 1)
            try videoTrack.insertTimeRange(CMTimeRangeMake(middleStart, middleEnd), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: CMTimeMake(5,1))

            //Last 5 seconds
            let endStart = CMTimeMake(Int64(gameTime-5), 1)
            let endEnd = CMTimeMake(Int64(gameTime), 1)
            try videoTrack.insertTimeRange(CMTimeRangeMake(endStart, endEnd), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: CMTimeMake(10,1))


        } catch {
            print(error)
        }

        let assetVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).last!

        let compositionVideoTrack = mixComposition.tracksWithMediaType(AVMediaTypeVideo).last!

        compositionVideoTrack.preferredTransform = assetVideoTrack.preferredTransform

        let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
        deleteFileAtURL(recordedVideoURL)
        exporter!.outputURL = recordedVideoURL
        exporter!.outputFileType = AVFileTypeMPEG4
        exporter!.shouldOptimizeForNetworkUse = true

        let startTime = CMTime(seconds: Double(0), preferredTimescale: 1000)
        let endTime = CMTime(seconds: Double(15), preferredTimescale: 1000)
        let timeRange = CMTimeRange(start: startTime, end: endTime)

        exporter!.timeRange = timeRange

        exporter!.exportAsynchronouslyWithCompletionHandler() {

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                print("done exporting video")

                self.videoOutlet.enabled = true
                self.videoOutlet.setTitle("Save Video", forState: .Normal)

            })

        }

    }

我还需要添加一个水印,当我这样做时,水印应该是它应该的位置(沿着屏幕底部),但视频旋转90度:

func cropVideo() {

    let videoAsset = AVAsset(URL: recordedVideoURL)

    let mixComposition = AVMutableComposition()

    do {

        let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))


        //First 5 seconds
        try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, CMTimeMake(5, 1)), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: kCMTimeZero)

        //Middle 5 seconds
        let middleStart = CMTimeMake(Int64(gameTime/2), 1)
        let middleEnd = CMTimeMake(Int64(((gameTime/2) + 5)), 1)
        try videoTrack.insertTimeRange(CMTimeRangeMake(middleStart, middleEnd), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: CMTimeMake(5,1))

        //Last 5 seconds
        let endStart = CMTimeMake(Int64(gameTime-5), 1)
        let endEnd = CMTimeMake(Int64(gameTime), 1)
        try videoTrack.insertTimeRange(CMTimeRangeMake(endStart, endEnd), ofTrack: videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0], atTime: CMTimeMake(10,1))


    } catch {
        print(error)
    }

    let assetVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).last!

    let compositionVideoTrack = mixComposition.tracksWithMediaType(AVMediaTypeVideo).last!

    // Watermark Effect
    let size = mixComposition.naturalSize

    // create text Layer
    let titleLayer = CATextLayer()
    titleLayer.backgroundColor = UIColor.whiteColor().CGColor
    titleLayer.string = "Dummy text"
    titleLayer.font = UIFont(name: "Helvetica", size: 28)
    titleLayer.shadowOpacity = 0.5
    titleLayer.alignmentMode = kCAAlignmentCenter
    titleLayer.frame = CGRectMake(0, 50, size.width, size.height / 6)

    let videolayer = CALayer()
    videolayer.frame = CGRectMake(0, 0, size.width, size.height)

    let parentlayer = CALayer()
    parentlayer.frame = CGRectMake(0, 0, size.width, size.height)
    parentlayer.addSublayer(videolayer)
    parentlayer.addSublayer(titleLayer)

    let layercomposition = AVMutableVideoComposition()
    layercomposition.frameDuration = CMTimeMake(1, 30)
    layercomposition.renderSize = size
    layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, inLayer: parentlayer)

    // instruction for watermark
    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)

    let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack)
    instruction.layerInstructions = NSArray(object: layerinstruction) as! [AVVideoCompositionLayerInstruction]
    layercomposition.instructions = NSArray(object: instruction) as! [AVVideoCompositionInstructionProtocol]

    compositionVideoTrack.preferredTransform = assetVideoTrack.preferredTransform

    let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
    deleteFileAtURL(recordedVideoURL)
    exporter!.outputURL = recordedVideoURL
    exporter!.outputFileType = AVFileTypeMPEG4
    exporter!.shouldOptimizeForNetworkUse = true

    let startTime = CMTime(seconds: Double(0), preferredTimescale: 1000)
    let endTime = CMTime(seconds: Double(15), preferredTimescale: 1000)
    let timeRange = CMTimeRange(start: startTime, end: endTime)

    exporter!.timeRange = timeRange

    exporter!.videoComposition = layercomposition

    exporter!.exportAsynchronouslyWithCompletionHandler() {

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            print("done exporting video")

            self.videoOutlet.enabled = true
            self.videoOutlet.setTitle("Save Video", forState: .Normal)

        })

    }

}

造成这种情况的原因是什么?

0 个答案:

没有答案