PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How to make a square video?

 July 27, 2022     crop, square, swift, video     No comments   

Issue

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)




Solution

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)


Answered By - Saveliy Veprev
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing