【问题标题】:get naturalSize and list resolution video m3u8获取 naturalSize 和列表分辨率视频 m3u8
【发布时间】:2018-09-28 04:39:27
【问题描述】:

我播放视频 m3u8。

我尝试使用let videoAssetSource = AVAsset(url: videoURL)videoAssetSource.tracks(withMediaType: .video).count 总是返回 0。 当我使用链接 mp4 时,这是成功的。

如何获得列表质量链接 m3u8 支持并在播放视频时更改质量。

【问题讨论】:

标签: ios swift http-live-streaming


【解决方案1】:

您必须为分辨率创建自己的相应模型,但这样的代码应该可以工作。

    /// Downloads the stream file and converts it to the raw playlist.
    /// - Parameter completion: In successful case should return the `RawPlalist` which contains the url with which was the request performed
    /// and the string representation of the downloaded file as `content: String` parameter.
    func getPlaylist(from url: URL, completion: @escaping (Result<RawPlaylist, Error>) -> Void) {
        task = URLSession.shared.dataTask(with: url) { data, response, error in
            if let error = error {
                completion(.failure(error))
            } else if let data = data, let string = String(data: data, encoding: .utf8) {
                completion(.success(RawPlaylist(url: url, content: string)))
            } else {
                completion(.failure(PlayerException.MEDIA_ERR_DECODE)) // Probably an MP4 file.
            }
        }
        task?.resume()
    }

    /// Iterates over the provided playlist contetn and fetches all the stream info data under the `#EXT-X-STREAM-INF"` key.
    /// - Parameter playlist: Playlist object obtained from the stream url.
    /// - Returns: All available stream resolutions for respective bandwidth.
    func getStreamResolutions(from playlist: RawPlaylist) -> [StreamResolution] {
        var resolutions = [StreamResolution]()
        playlist.content.enumerateLines { line, shouldStop in
            let infoline = line.replacingOccurrences(of: "#EXT-X-STREAM-INF", with: "")
            let infoItems = infoline.components(separatedBy: ",")
            let bandwidthItem = infoItems.first(where: { $0.contains(":BANDWIDTH") })
            let resolutionItem = infoItems.first(where: { $0.contains("RESOLUTION")})
            if let bandwidth = bandwidthItem?.components(separatedBy: "=").last,
               let numericBandwidth = Double(bandwidth),
               let resolution = resolutionItem?.components(separatedBy: "=").last?.components(separatedBy: "x"),
               let strignWidth = resolution.first,
               let stringHeight = resolution.last,
               let width = Double(strignWidth),
               let height = Double(stringHeight) {
                resolutions.append(StreamResolution(maxBandwidth: numericBandwidth,
                                                    averageBandwidth: numericBandwidth,
                                                    resolution: CGSize(width: width, height: height)))
            }
        }
        return resolutions
    }
}

【讨论】:

  • 什么是RawPlaylist、StreamResolution? Xcode 找不到这些项目。
  • @SamiulIslamSami 只是我将获取的数据映射到的自定义类。您可以在上面的代码 sn-p 中看到这些对象的构造函数。
【解决方案2】:

您需要为播放器项目上的属性轨道订阅观察者:

//Define this variable globally
var observers:[NSKeyValueObservation]? = [NSKeyValueObservation]()

//Find tracks
let videoAssetSource = AVAsset(url: videoURL)
let playerItem = AVPlayerItem(asset: videoAssetSource)

let tracksObserver = self.playerItem.observe(\.tracks, options: [.old, .new]) { (item, change) in
     for track in item.tracks {
        let _assetTrack:AVAssetTrack? = track.assetTrack
        if let assetTrack = _assetTrack {
           if assetTrack.mediaType == .video {
              //we found a video track
           } 
        }
     }
}

//Keep observer reference
observers?.append(tracksObserver)

我正在使用 Swift 4 基于块的键值观察器,但如果需要,您可以使用 observeValue(forKeyPath:…)。

【讨论】:

  • hls/m3u8 为 tracks 返回一个空数组
猜你喜欢
  • 1970-01-01
  • 2015-11-02
  • 2017-05-09
  • 2011-07-05
  • 1970-01-01
  • 2013-02-27
  • 2012-10-25
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多