Why does AVPlayer doesn't orient video correctly when using SceneKit?

37 views Asked by At

I'm making a video player in SceneKit using both AVPlayer and SCNNode. Basically, I'm creating an AVPlayer using a video URL, set the player into SCNPlane's diffuse contents, create an SCNNode using the plane as the geometry, and finally add the node into my SCNView's rootNode. Here's the logic that I'm using:

private func setupVideoNode() {
        videoPlayer = AVPlayer(url: videoURL)
        let videoTrack = videoPlayer!.currentItem!.asset.tracks(withMediaType: .video).first!
        let naturalSize = videoTrack.naturalSize
        let aspectRatio: CGFloat = CGFloat(naturalSize.width / naturalSize.height) * 10
        
        let videoPlane = SCNPlane(width: aspectRatio, height: 10)
        videoPlane.firstMaterial?.isDoubleSided = true
        videoPlane.firstMaterial?.diffuse.contents = videoPlayer
        videoNode = SCNNode(geometry: videoPlane)
        
        sceneView.scene?.rootNode.addChildNode(videoNode!)
    }

Unfortunately, the result looks like this:

The video node WITHOUT preferredTransform applied

As you can see here, the video is not being oriented correctly (it's rotated to landscape like this instead of the original orientation which is portrait). I tried to apply the preferredTransform to calculate the width and height of the video like this:

...
let naturalSize = videoTrack.naturalSize
let naturalSizeWithPreferredTransform = naturalSize.applying(videoTrack.preferredTransform)
let aspectRatio: CGFloat = CGFloat(naturalSizeWithPreferredTransform.width / naturalSizeWithPreferredTransform.height) * 10
...

But the result looks like this:

The video node WITH preferredTransform applied

Is there anyway to orient the AVPlayer correctly when using SceneKit like my specific use case? Any help would be appreciated. Thank you.

0

There are 0 answers