iOS rotate video AVAsset avfoundation

谁都会走 提交于 2020-01-03 02:10:06

问题


Example

Hi,

Struggling to rotate this video to show in the proper orientation and fill the entire screen.

I cannot get the avasset with videocompisition but cannot get it to work correctly.

    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:


Solved the rotation converting from the code below:

AVMutableVideoComposition rotated video captured in portrait mode

Now having issues with exporting in question below if anyone knows: https://stackoverflow.com/questions/35233766/avasset-failing-to-export



来源:https://stackoverflow.com/questions/35155450/ios-rotate-video-avasset-avfoundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!