Swift 视频缩放器 AVAsset
Swift Video Resizer AVAsset
我有这段代码可以将视频从 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。
鉴于您的比率为 0.5 (1280 / 640),您可以试试这个:
let t3 = CGAffineTransformScale(t2, 0.5, 0.5)
transformer.setTransform(t3, atTime: kCMTimeZero)
如果看起来不对,您可能还需要通过比例调整平移。
在Swift4
let playerLayer = AVPlayerLayer(player: player)
let transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
playerLayer.setAffineTransform(transform)
我有这段代码可以将视频从 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。
鉴于您的比率为 0.5 (1280 / 640),您可以试试这个:
let t3 = CGAffineTransformScale(t2, 0.5, 0.5)
transformer.setTransform(t3, atTime: kCMTimeZero)
如果看起来不对,您可能还需要通过比例调整平移。
在Swift4
let playerLayer = AVPlayerLayer(player: player)
let transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
playerLayer.setAffineTransform(transform)