如何从视频网址iOS获取可用的视频尺寸/质量?

abh*_*ran 3 video objective-c avfoundation ios avplayer

我正在使用ios中的AVPlayer创建自定义视频播放器(OBJECTIVE-C).我有一个设置按钮,点击后会显示可用的视频尺寸和音频格式.以下是设计:

在此输入图像描述

所以,我想知道:

1).如何从视频网址(而非本地视频)获取可用尺寸?

2).即使我能够获得尺寸,我可以在AVPlayer中播放时在可用尺寸之间切换吗?

任何人都可以给我一个提示吗?

小智 5

如果它不是HLS(流媒体)视频,您可以使用以下代码获取分辨率信息.

示例代码:

// player is playing
if (_player.rate != 0 && _player.error == nil)
{
    AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    if (track != nil)
    {
        CGSize naturalSize = [track naturalSize];
        naturalSize = CGSizeApplyAffineTransform(naturalSize, track.preferredTransform);

        NSInteger width = (NSInteger) naturalSize.width;
        NSInteger height = (NSInteger) naturalSize.height;
        NSLog(@"Resolution : %ld x %ld", width, height);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,对于HLS视频,上面的代码不起作用.我以不同的方式解决了这个问题.当我播放视频时,我从视频中获取了图像,并计算了它的分辨率.

以下是示例代码:

// player is playing
if (_player.rate != 0 && _player.error == nil)
{
    AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    CMTime currentTime = _player.currentItem.currentTime;
    CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil];

    NSInteger width = CVPixelBufferGetWidth(buffer);
    NSInteger height = CVPixelBufferGetHeight(buffer);
    NSLog(@"Resolution : %ld x %ld", width, height);
}
Run Code Online (Sandbox Code Playgroud)

  • 像在youtube中一样,如何在流之间切换? (2认同)