Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Video Resizer AVAsset

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)
            })
        })
    }
like image 739
unwosu Avatar asked Feb 15 '16 06:02

unwosu


2 Answers

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.

like image 151
Tim Bull Avatar answered Nov 20 '22 02:11

Tim Bull


In Swift 4

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

Doc

like image 2
Jack Avatar answered Nov 20 '22 01:11

Jack