@@ -747,15 +747,22 @@ class MacVideoPlayer {
747747 self . nativeVideoWidth = self . frameWidth
748748 self . nativeVideoHeight = self . frameHeight
749749
750+ // Build a video composition that applies the preferred transform so
751+ // that AVPlayerItemVideoOutput delivers pixel buffers already rotated
752+ // to display orientation (fixes portrait videos rendering sideways).
753+ let videoComposition : AVVideoComposition ? = self . isHLSStream
754+ ? nil
755+ : ( try ? await AVVideoComposition . videoComposition ( withPropertiesOf: asset) )
756+
750757 // Continue with player setup
751- self . setupVideoOutputAndPlayer ( with: asset)
758+ self . setupVideoOutputAndPlayer ( with: asset, videoComposition : videoComposition )
752759 } catch {
753760 print ( " Error loading video track properties: \( error. localizedDescription) " )
754761 // Use default dimensions for HLS if loading fails
755762 if self . isHLSStream {
756763 self . frameWidth = 1920
757764 self . frameHeight = 1080
758- self . setupVideoOutputAndPlayer ( with: asset)
765+ self . setupVideoOutputAndPlayer ( with: asset, videoComposition : nil )
759766 }
760767 }
761768 }
@@ -770,8 +777,14 @@ class MacVideoPlayer {
770777 nativeVideoWidth = frameWidth
771778 nativeVideoHeight = frameHeight
772779
780+ // Build a video composition that applies the preferred transform (see modern
781+ // path above for rationale). Skip for HLS streams.
782+ let videoComposition : AVVideoComposition ? = isHLSStream
783+ ? nil
784+ : AVMutableVideoComposition ( propertiesOf: asset)
785+
773786 // Continue with player setup
774- setupVideoOutputAndPlayer ( with: asset)
787+ setupVideoOutputAndPlayer ( with: asset, videoComposition : videoComposition )
775788 }
776789 }
777790 }
@@ -825,7 +838,7 @@ class MacVideoPlayer {
825838 }
826839
827840 // Helper method to setup video output and player
828- private func setupVideoOutputAndPlayer( with asset: AVAsset ) {
841+ private func setupVideoOutputAndPlayer( with asset: AVAsset , videoComposition : AVVideoComposition ? = nil ) {
829842 // Create attributes for the CVPixelBuffer (BGRA format) with IOSurface for better performance
830843 let pixelBufferAttributes : [ String : Any ] = [
831844 kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA,
@@ -837,6 +850,12 @@ class MacVideoPlayer {
837850
838851 let item = AVPlayerItem ( asset: asset)
839852
853+ // Apply the video composition (if any) so that pixel buffers delivered to
854+ // AVPlayerItemVideoOutput are pre-rotated to the display orientation.
855+ if let videoComposition = videoComposition {
856+ item. videoComposition = videoComposition
857+ }
858+
840859 // Configure for HLS if needed
841860 if isHLSStream {
842861 // Set buffer duration for HLS
0 commit comments