gpt4 book ai didi

swift - 确定 SceneKit 中 SKVideoNode 的视频大小/纵横比

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:08:45 25 4
gpt4 key购买 nike

如何从 AVPlayer 获取视频的视频大小以设置我的节点的几何大小?

例如,我有一个具有宽度和高度的 SCNPlane

let planeGeo = SCNPlane(width: 5, height: 5)

所以现在我实例化我的视频播放器

let videoURL = NSURL(string: someURL)
let player = AVPlayer(URL: videoURL!)

和我的 SKVideoNode

let spriteKitScene = SKScene(size: CGSize(width: 1920, height: 1080))
spriteKitScene.scaleMode = .AspectFit

videoSpriteKitNode = SKVideoNode(AVPlayer: player)
videoSpriteKitNode.anchorPoint = CGPointMake(0,0)
videoSpriteKitNode.size.width = spriteKitScene.size.width
videoSpriteKitNode.size.height = spriteKitScene.size.height

spriteKitScene.addChild(videoSpriteKitNode)

planeGeo!.firstMaterial.diffuse.contents = spriteKitScene
videoSpriteKitNode.play()

所以现在我想要视频大小来将我的平面大小调整为正确的纵横比。我已经摆弄过 AVLPlayerLayer 但这总是给我 0

let avLayer = AVPlayerLayer(player: player)
print(avLayer.videoRect.width) //0
print(avLayer.videoRect.height) //0

我也试过,但效果不佳

let avLayer = AVPlayerLayer(player: player)
let layer = avLayer.sublayers![0]
let transformedBounds = CGRectApplyAffineTransform(layer.bounds, CATransform3DGetAffineTransform(layer.sublayerTransform))
print(transformedBounds.width) //0
print(transformedBounds.height) //0

最佳答案

好的,我明白了,KVO 是正确的选择。在viewDidLoad中添加:

player.currentItem?.addObserver(self, forKeyPath: "presentationSize", options: .New, context: nil)

在初始化中:

player.currentItem?.removeObserver(self, forKeyPath: "presentationSize")

然后添加:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

if keyPath == "presentationSize" {
if let item = object as? AVPlayerItem {
let size = item.presentationSize
let width = size.width
let height = size.height

//Set size of geometry here
}
}
}

2022语法的典型代码:

var py: AVPlayer?
private var pyContext = 0
...

guard let url = URL(string: "https:// .. /test.m4v") else { return }
py = AVPlayer(url: url)
someNode.geometry?.firstMaterial?.diffuse.contents = py

py?.currentItem?.addObserver(self,
forKeyPath: "presentationSize",
context: &pyContext)
...

override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {

if context == &py1Context && keyPath == "presentationSize" {
print("Found it ...")

guard let item = object as AVPlayerItem else { return }

let ps = item.presentationSize
let aspect: Float = Float(ps.width) / Float(ps.height)
someNode.geometry?.firstMaterial?
.diffuse.contentsTransform =
SCNMatrix4MakeScale( .. , .. , 1)
}
}

附录。计算正确的缩放比例很棘手

不幸的是,任何时候您处理视频时,如果流媒体内容的确切大小并不总是相同,那么塑造视频就是一个巨大的痛苦。当然你有很多考虑因素,比如是否有信箱等等。在一些简单的情况下计算看起来像这样:

// 1. You nave finally received the info on the video from the HLS stream:

let ps = item.presentationSize
let spect: Float = Float(ps.width) / Float(ps.height)

// 2. Over in your 3D code, you need to know the current facts on the mesh:

let planeW = ... width of your mesh
let planeH = ... height of your mesh
let planeAspect = planeW / planeH

您可能正在使用 Apple 提供的简单的平面方形网格,例如

var simplePlane = SCNPlane()
simplePlane.width = 2.783
simplePlane.height = 1.8723

不管怎样,您都需要网格的 w/h。然后,

// 3. In many (not all) cases, the solution is:

let final = vidAspect / planeAspect
print("we'll try this: \(final)")
yourNode.geometry?
.firstMaterial?.diffuse
.contentsTransform = SCNMatrix4MakeScale(1.0, final, 1.0)

关于swift - 确定 SceneKit 中 SKVideoNode 的视频大小/纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34928396/

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