我正在开发一个带有 HLS 流的 iOS 应用程序。我正在使用 AVPlayer playerWithURL
播放视频。有什么方法可以从流或播放器获取实际的视频尺寸(宽度和高度)?
AVPlayerLayer videoRect
返回视频播放器层的视频尺寸而不是实际视频。
[_player.currentItem.asset trackWithMediaType:AVMediaTypeVideo]
为此流媒体网址返回零对象数组,因此我无法使用[track naturalSize]
。
还有其他选项可以获取视频的宽度和高度吗?
最佳答案
对于 HLS 流视频,该代码不起作用。
[_player.currentItem.asset Tracks] 是一个空数组。
因此“[_player.currentItem.asset trackWithMediaType: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);
关于ios - iOS中HLS的视频宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120640/