iOS旋转视频AVAsset avfoundation

问题描述 投票:1回答:1

Example

嗨,

努力旋转此视频以正确的方向显示并填满整个屏幕。

我无法通过视频冲击获得avasset但无法使其正常工作。

    let videoAsset: AVAsset = AVAsset(URL: outputFileURL) as AVAsset

    let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack

    let newHeight = CGFloat(clipVideoTrack.naturalSize.height/3*4)

    let composition = AVMutableComposition()
    composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    let videoComposition = AVMutableVideoComposition()
    var videoSize = CGSize()
    videoSize = clipVideoTrack.naturalSize
    videoComposition.renderSize = videoSize
    videoComposition.frameDuration = CMTimeMake(1, 30)

    let instruction = AVMutableVideoCompositionInstruction()

    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))

    // rotate to portrait
    let transformer:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
    let t1 = CGAffineTransformMakeTranslation(0, 0);
    let t2 = CGAffineTransformRotate(t1, CGFloat(M_PI_2));

    transformer.setTransform(t2, atTime: kCMTimeZero)
    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
    let date = NSDate()
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    let outputPath = "\(documentsPath)/\(formatter.stringFromDate(date)).mp4"
    let outputURL = NSURL(fileURLWithPath: outputPath)

    let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
    exporter.videoComposition = videoComposition

    exporter.outputURL = outputURL
    exporter.outputFileType = AVFileTypeQuickTimeMovie

    exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        dispatch_async(dispatch_get_main_queue(), {
            self.handleExportCompletion(exporter)
        })
    })
ios swift avfoundation avasset
1个回答
0
投票

解决了以下代码中的旋转转换:

AVMutableVideoComposition rotated video captured in portrait mode

如果有人知道,现在有问题导出问题: https://stackoverflow.com/questions/35233766/avasset-failing-to-export

© www.soinside.com 2019 - 2024. All rights reserved.