How to make a square video?

by Saveliy Veprev   Last Updated August 14, 2019 05:26 AM - source

Trying to make a square video through animationTool. See code below. The video is enlarged (https://i.stack.imgur.com/HscTk.jpg), how can i fix it?

let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
        let strFilePath: String = generateMergedVideoFilePath()
        try? FileManager.default.removeItem(atPath: strFilePath)
        exportSession?.outputURL = URL(fileURLWithPath: strFilePath)
        exportSession?.outputFileType = .mp4
        exportSession?.shouldOptimizeForNetworkUse = true
        let mutableVideoComposition = AVMutableVideoComposition(propertiesOf: composition)
        mutableVideoComposition.instructions = instructions
        mutableVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
        mutableVideoComposition.renderSize = CGSize(width: 1080, height: 1080)

        let parentLayer = CALayer()
        parentLayer.frame = CGRect(x: 0, y: 0, width: 1080, height: 1080)
        let videoLayer = CALayer()
        videoLayer.frame.size = videoSize
        videoLayer.position = parentLayer.position
        videoLayer.contentsGravity = .resizeAspectFill
        parentLayer.addSublayer(videoLayer)


        mutableVideoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)





Answers 1


mutableVideoComposition.renderSize = CGSize (width: 1080, height: 1080)

With this resolution, it takes the image relative to the top point, so I moved transform up

let coeConst = videoAssetWidth/videoAssetHeight
transform.translatedBy(x: -(videoAssetHeight-videoAssetHeight*coeConst)/2, y: 0)
Saveliy Veprev
Saveliy Veprev
August 14, 2019 05:25 AM

Related Questions




iOS Swift - Crop Image from bottom Programmatically

Updated December 07, 2016 08:11 AM


How to crop an image with a given angle with swift

Updated October 26, 2018 07:26 AM