HHK1 / PryntTrimmerView

A set of tools to trim, crop and select frames inside a video
MIT License
835 stars 199 forks source link

Render Size question [question] #89

Open romaHerman opened 2 years ago

romaHerman commented 2 years ago

Hi!

Thanks for the awesome tool

I was reading a code related to the video cropping. There is one thing I can't wrap my head around

CGSize(width: 16 * videoCropView.aspectRatio.width * 18,
                                height: 16 * videoCropView.aspectRatio.height * 18)

Could you please explain why we need to multiple aspectRatio by 16 and 18 ?

and why scale is determined by dividing width on width let renderScale = renderSize.width / cropFrame.width

Thanks!

HHK1 commented 1 year ago

Hi @romaHerman

For the first question, the interesting part is the multiplication by 16. 18 here is not important, you could potentially put whatever you want. The reason is that encoding videos with width and height that are not a multiple of 16 (or 8 or 4) can lead to some issues (white or green streaks in the video some rows or column for example). More on that here:

If you know your stuff in video encoding, you could get rid of that constraint. But for that project, I thought it was simpler just to have sensible defaults

HHK1 commented 1 year ago

For the renderScale you need to map the portion of the video you want to crop into your final video size. So there is a transform applied in the video composition that just does that, and the ratio here is used in the transform.

ZUCheema commented 1 year ago

@romaHerman , 16*18 = 288 which is constant factor. We can easily calculate custom aspect ratio for needed dimensions. Here's an example for aspect ratio 16/9 or 1920x1080 dimension video. and aspect ratio is 6.667:3.75. Similarly, it would be 3.75:3.75 if required render size is 1080x1080.

let factor : CGFloat = 16*18 //288.0
let ratioHeight : Double! = postSize.height/factor //1920/288=6.667
let ratioWidth : Double! = postSize.width/factor// 1080/288=3.75

videoCropView.setAspectRatio(CGSize(width: ratioWidth, height: ratioHeight), animated: false)

make sure you setAspectRatio in viewDidLayoutSubviews or viewDidAppear.

ZUCheema commented 1 year ago

The setAspectRatio function doesn't really seem aspect ratio here. Setting it 1.0/1.0 will generate a video with 288x288 dimension. Its dimension factor instead of aspect ratio and we manually need to calculate render size as a multiplier of 288 factor. I think this function should be modified to take aspect ratio and renderSize in input and calculate actual aspect ratio internally. Like this: videoCropView.setAspectRatio(renderSize: CGSize(width: ratioWidth, height: ratioHeight), aspectRatio: CGFloat, animated: false)