Swift Video Resizer AVAsset

匆匆过客 提交于 2020-01-21 05:21:07

问题


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 ?

Here's the code

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)
            })
        })
    }

回答1:


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

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.




回答2:


In Swift 4

let playerLayer = AVPlayerLayer(player: player)
let transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
playerLayer.setAffineTransform(transform)

Doc



来源:https://stackoverflow.com/questions/35402633/swift-video-resizer-avasset

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