Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale video view based on the aspect ratio

Tags:

ios

swift

webrtc

I want to scale a video stream on devices with different screen sizes. The sender's video size can be different also.

The code what I have till now:

public func videoView(_ videoView: RTCEAGLVideoView, didChangeVideoSize size: CGSize) {
        // scale by height
        let w = renderer.bounds.height * size.width / size.height
        let h = renderer.bounds.height
        let x = (w - renderer.bounds.width) / 2
        renderer.frame = CGRect(x: -x, y: 0, width: w, height: h)
    }

The renderer is a view where the video is rendered in. This scales, but not in the right way. Some part of the video is lost, the height is fine. But the width is cropped what I understand.

Is it possible to achieve a solution that displays the video full screen (on the receiver side) without losing its aspect ratio?

like image 880
da1lbi3 Avatar asked Apr 22 '17 13:04

da1lbi3


People also ask

Is 1920x1080 the same as 16:9?

1920 x 1080 is a 16:9 aspect ratio. By default, smartphones, DSLRs, and most modern camcorders record video at 1920 x 1080.

What aspect ratio should I use for video?

What Is the Best Aspect Ratio for Video? 16:9 is the best aspect ratio for video due to its acceptance as the international standard and because a majority of the world uses devices with screens matching the 16:9 ratio. Even popular video streaming platforms like Netflix and YouTube use 16:9 for videos.


1 Answers

Try using AVMakeRect(aspectRatio:insideRect). This method finds the minimum rect that preserves the aspectRatio inside the specified rect.

You can read more about it here

In general you will specify your UIScreen bounds as an insideRect: parameter and the size of your media in aspectRatio. There might be a case where the rect would be slightly smaller than your UIScreen size. In that case i suggest using technique called letter boxing

like image 168
Mr. Hedgehog Avatar answered Oct 28 '22 04:10

Mr. Hedgehog