gpt4 book ai didi

ios - Swift 视频缩放器 AVAsset

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:50 24 4
gpt4 key购买 nike

我有这段代码可以将视频从 1280 x 720 调整为 640 x 360但我想要一个没有裁剪的调整大小。

有没有办法在不裁剪的情况下完全调整大小?

这是代码

class func resizer(inputURL : NSURL , completion: (outPutURL : NSURL?) -> Void ){
let videoAsset = AVAsset(URL: inputURL) as AVAsset
let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack

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

let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSizeMake(360,640)
videoComposition.frameDuration = CMTimeMake(1, 30)

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

let transformer : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

let t1 = CGAffineTransformMakeTranslation(360, 0)
let t2 = CGAffineTransformRotate(t1, CGFloat(M_PI_2))
// let t2 = CGAffineTransformMakeRotation(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 tempDir = NSTemporaryDirectory().stringByAppendingString(formatter.stringFromDate(date))
let d = "\(tempDir).mp4"
let outputURL = NSURL(fileURLWithPath: d)

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(), {
completion(outPutURL: outputURL)
})
})
}

最佳答案

我认为您正在寻找的(不裁剪调整大小)也是对视频进行缩放。也尝试使用 CGAffineTransformScale/MakeScale。

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/#//apple_ref/c/func/CGAffineTransformMakeScale

假设您的比率为 0.5 (1280/640),您可以尝试这样做:

let t3 = CGAffineTransformScale(t2, 0.5, 0.5)
transformer.setTransform(t3, atTime: kCMTimeZero)

如果看起来不对,您可能还需要通过比例调整平移。

关于ios - Swift 视频缩放器 AVAsset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402633/

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