I am developing an iOS application with HLS streaming. I am using AVPlayer playerWithURL
to play the video. Is there any way I can get the actual video dimensions (width and height) from the stream or player?
AVPlayerLayer videoRect
returns the video dimension of the video player layer not the actual video.
[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo]
returns zero object array for this streaming url so that I can't use [track naturalSize]
.
Is there any other option that I can get the width and height of the video?
In case of HLS streaming video, the code does not work.
[_player.currentItem.asset tracks] is an empty array.
So "[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo]" also returns nil.
I solved the same problem with you in different ways.
I got the image from streaming video and calculated the resolution to get the width and height.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.