iOS旋转视频AVAsset avfoundation

时间:2016-02-02 13:57:59

标签: ios swift avfoundation avasset

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)
        })
    })

1 个答案:

答案 0 :(得分:0)

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

AVMutableVideoComposition rotated video captured in portrait mode

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