【问题标题】:How to get the available video dimensions/quality from a video url iOS?如何从视频 url iOS 中获取可用的视频尺寸/质量?
【发布时间】:2016-04-13 07:01:15
【问题描述】:

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

所以,我想知道:

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

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

谁能给我一个提示?

【问题讨论】:

    标签: ios objective-c video avfoundation avplayer


    【解决方案1】:

    如果不是HLS(流式)视频,可以通过以下代码获取Resolution信息。

    示例代码:

    // 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);
        }
    }
    

    但是,对于 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);
    }
    

    【讨论】:

    • 在 youtube 中切换流怎么样?
    【解决方案2】:

    正如您所提到的,它不是本地视频,您可以调用一些网络服务来返回该特定视频的可用视频尺寸。之后将 URL 更改为其他可用视频并搜索到当前位置。

    Refer This

    【讨论】:

    • 感谢您的回复。但是如果是 Hls 视频,可能会有不同的分辨率可用。实际上 iOS 会根据我猜的网络速度自动处理它们。但我问我们是否可以检索那些数据并在它们之间切换。我说的是单个视频 url。单个 url 上可能有不同的流。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2016-04-26
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多