我有一个视频保存在本地缓存文件夹中,我使用 URL 加载它,代码如下:
我正在使用 AVQueuePlayer、AVPlayerViewController 和 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);
}
}
}
除了打开 HDR 的视频亮度过高之外,一切正常。当我在 QuickTime 上播放时,这是正常的。有没有办法禁用 HDR 或将 HDR 转换为 SDR
我似乎找到了一种在预览和导出中禁用 HDR 的方法。结果还不错。
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
以下是如何修复
AVPlayer
/AVPlayerViewController
的 HDR 视频颜色映射的完整示例:
// global
private let playerViewController = AVPlayerViewController()
func playVideo(_ videoURL: URL) {
let asset = AVAsset(url: videoURL)
let playerItem = AVPlayerItem(asset: asset)
let videoComposition = AVMutableVideoComposition(propertiesOf: asset)
videoComposition.colorPrimaries = AVVideoColorPrimaries_SMPTE_C
videoComposition.colorTransferFunction = AVVideoTransferFunction_SMPTE_ST_2084_PQ
videoComposition.colorYCbCrMatrix = AVVideoYCbCrMatrix_ITU_R_601_4
playerItem.videoComposition = videoComposition
self.playerViewController.player = AVPlayer(playerItem: playerItem)
self.playerViewController.player?.play()
}