Swift Video Resizer AVAsset [英] Swift Video Resizer AVAsset

查看:117
本文介绍了Swift Video Resizer AVAsset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code that resizes a video from 1280 x 720 to 640 x 360 But i want a resize with no crop.

有没有办法完全调整不剪裁的大小?

Is there a way to do a full resize the don't crop ?

这是代码

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.

I think what you're looking for (resize with no crop) is to SCALE the video too. Try using CGAffineTransformScale / MakeScale as well.

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

鉴于您的比率为0.5(1280/640),您可以尝试以下方法:

Given you have a ratio of 0.5 (1280 / 640) you could try this:

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

如果看起来不正确,您可能还需要按比例调整平移.

If it doesn't look right, you may also need to adjust the translate by the scale ratio too.

这篇关于Swift Video Resizer AVAsset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆