gpt4 book ai didi

iOS旋转视频AVAsset avfoundation

转载 作者:行者123 更新时间:2023-11-28 08:46:31 29 4
gpt4 key购买 nike

Example

你好,

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

我无法使用 videocompisition 获取 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)
})
})

最佳答案

通过以下代码解决了旋转转换问题:

AVMutableVideoComposition rotated video captured in portrait mode

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

关于iOS旋转视频AVAsset avfoundation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155450/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com