[英]Video width and height of HLS in iOS
我正在使用HLS流開發iOS應用程序。 我正在使用AVPlayer playerWithURL
播放視頻。 有什么辦法可以從流或播放器中獲取實際的視頻尺寸(寬度和高度)?
AVPlayerLayer videoRect
返回視頻播放器層的視頻尺寸,而不是實際視頻。
[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo]
為此流URL返回零對象數組,因此我無法使用[track naturalSize]
。
我還有其他選擇可以獲取視頻的寬度和高度嗎?
在HLS流視頻的情況下,該代碼不起作用。
[_player.currentItem.asset軌道]是一個空數組。
因此,“ [_ player.currentItem.assettracksWithMediaType:AVMediaTypeVideo]”也返回nil。
我以不同的方式與您解決了相同的問題。
我從流媒體視頻中獲取了圖像,並計算了分辨率以獲取寬度和高度。
if ((_player.rate != 0) && (_player.error == nil)) // player is playing
{
AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
if (track != nil) // not HLS
{
CGSize naturalSize = [track naturalSize];
naturalSize = CGSizeApplyAffineTransform(naturalSize, track.preferredTransform);
_videoWidth = (NSInteger) naturalSize.width;
_videoHeight = (NSInteger) naturalSize.height;
}
else // HLS!!
{
CMTime currentTime = _player.currentItem.currentTime;
CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil];
_videoWidth = CVPixelBufferGetWidth(buffer);
_videoHeight = CVPixelBufferGetHeight(buffer);
}
}
NSLog(@"Resolution : %ld x %ld", _videoWidth, _videoHeight);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.