Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerView adds brightness to videos with HDR turned on

I have a video that is saved locally in the cache folder and I load it using a URL Here's the code:

I'm using AVQueuePlayer, AVPlayerViewController, and AVPlayerLooper.

        internal void LoadVideoUsingURL(string url)
        {
            // / Load asset in background thread to avoid lagging
            DispatchQueue.GetGlobalQueue(DispatchQualityOfService.Background).DispatchAsync(() =>
            {
                var avasset = AVAsset.FromUrl(NSUrl.FromFilename(url));
                avasset.LoadValuesAsynchronously(new string[] { "duration", "playable" }, () =>
                {
                    DispatchQueue.MainQueue.DispatchAsync(() =>
                    {

                        var item = new AVPlayerItem(avasset);
                        if (this.queuePlayer.CurrentItem != item)
                        {
                            this.queuePlayer.ReplaceCurrentItemWithPlayerItem(item);
                            this.looperPlayer = AVPlayerLooper.FromPlayer(this.queuePlayer, item);
               
  
                        }

                    });
                });
            });
        }

        private void LoadView()
        {
            try
            {
                queuePlayer.Volume =SoundON? 1.0f:0;
                playerViewController.ShowsPlaybackControls = false;
                queuePlayer.ActionAtItemEnd = AVPlayerActionAtItemEnd.None;
                playerViewController.VideoGravity = AVLayerVideoGravity.Resize;
                playerViewController.View.Layer.MasksToBounds = true;
                ContentView.AddSubview(playerViewController.View);
                playerViewController.Player = queuePlayer;
                ContentView.AddSubview(BlackBackground);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public virtual void StartPlaying()
        {
         
            if (queuePlayer.CurrentItem != null && !isPlaying)
            {

                if (queuePlayer.Status == AVPlayerStatus.ReadyToPlay)
                {
                    queuePlayer.Play();
                    queuePlayer.Volume = Volume;
                    isPlaying = true;
                }
                else
                {
                    this.queuePlayer.CurrentItem.AddObserver(this, new NSString("status"),
NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial, StatusObservationContext.Handle);
                }

            }

        }

Everything is working fine except for the videos with HDR turned on having too much brightness. when I play it on QuickTime it's normal. Is there a way to disable HDR or convert HDR to SDR

enter image description here

like image 561
Ahmed Mohamed Anas Avatar asked Oct 23 '25 17:10

Ahmed Mohamed Anas


1 Answers

I seem to have found a way to disable HDR in previews and exports. The result is quite good.

let playerItem = AVPlayerItem(asset: composition)
let videoComposition = AVMutableVideoComposition(propertiesOf: composition)
videoComposition.colorPrimaries = AVVideoColorPrimaries_SMPTE_C
videoComposition.colorTransferFunction = AVVideoTransferFunction_SMPTE_ST_2084_PQ
videoComposition.colorYCbCrMatrix = AVVideoYCbCrMatrix_ITU_R_601_4
playerItem.videoComposition = videoComposition
like image 175
user3032246 Avatar answered Oct 26 '25 07:10

user3032246