gpt4 book ai didi

iOS Swift - 如何获取本地和远程视频的宽高比?

转载 作者:行者123 更新时间:2023-12-02 02:14:16 25 4
gpt4 key购买 nike

场景:我正在应用程序内构建 WebRTC View 视频容器的高度始终为 160。

在容器的中心应显示最大高度为 160 的远程视频,宽度应缩放以尊重视频的宽高比。宽度也不能大于 View 宽度,在这种情况下,宽度将等于 View 宽度,并且高度应适应宽高比。

右上角应显示来自前置摄像头的本地视频,最大宽度为 100,高度应适应本地视频的宽高比

到目前为止我的代码:

func createPeerConnection () {
// some other code

self.localStream = self.factory.mediaStream(withStreamId: "stream")
let videoSource = self.factory.videoSource()

let devices = RTCCameraVideoCapturer.captureDevices()
if let camera = devices.last,
let format = RTCCameraVideoCapturer.supportedFormats(for: camera).last,
let fps = format.videoSupportedFrameRateRanges.first?.maxFrameRate {
let intFps = Int(fps)
self.capturer = RTCCameraVideoCapturer(delegate: videoSource)
self.capturer?.startCapture(with: camera, format: format, fps: intFps)
videoSource.adaptOutputFormat(toWidth: 100, height: 160, fps: Int32(fps))
}

let videoTrack = self.factory.videoTrack(with: videoSource, trackId: "video")
self.localStream.addVideoTrack(videoTrack)

DispatchQueue.main.async {
if self.localView == nil {
let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 105, y: 5, width: 100, height: 160))
videoView.backgroundColor = UIColor.red

self.view.addSubview(videoView)
self.localView = videoView
}
videoTrack.add(self.localView!)
}
}

func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {
self.remoteStream = stream
if let videoTrack = stream.videoTracks.first {
DispatchQueue.main.async {
if self.remoteView == nil {
let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 50, y: 0, width: 100, height: 160))
videoView.backgroundColor = UIColor.green
if let local = self.localView {
self.view.insertSubview(videoView, belowSubview: local)
} else {
self.view.addSubview(videoView)
}
self.remoteView = videoView
}
videoTrack.add(self.remoteView!)
}
}
}

我不知道如何获取本地或远程视频的宽高比。如果我有的话,我可以计算它们每个的适当宽度和高度

//使用解决方案进行编辑:

我没有找到获得确切大小的方法,但我找到了按比例渲染视频的方法我所要做的就是:

let videoView = RTCEAGLVideoView(frame: CGRect(x: self.view.frame.size.width - 105, y: 5, width: 100, height: 134))
videoView.contentMode = .scaleAspectFill

现在视频会根据容器大小自行缩放

最佳答案

您可以使用AVURLAssetCGSize来获取视频的分辨率

private func resolutionForLocalVideo(url: URL) -> CGSize? {
guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaTypeVideo).first else { return nil }
let size = track.naturalSize.applying(track.preferredTransform)
return CGSize(width: fabs(size.width), height: fabs(size.height))
}

现在,使用自然大小preferredTransform

var mediaAspectRatio: Double! // <- here the aspect ratio for video with url will be set

func initAspectRatioOfVideo(with fileURL: URL) {
let resolution = resolutionForLocalVideo(url: fileURL)

guard let width = resolution?.width, let height = resolution?.height else {
return
}

mediaAspectRatio = Double(height / width)
}

此外,您还可以找到比例因子

float xScale = destination.size.width / imageSize.width; //destination is the max image drawing area.

float yScale = destination.size.height / imageSize.height;

float scaleFactor = xScale < yScale ? xScale : yScale;

这也可以通过GPUImageMovieGPUImageCropFilterGPUImageMovieWriter来实现

关于iOS Swift - 如何获取本地和远程视频的宽高比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59380766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com