首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取naturalSize和列表分辨率视频m3u8

获取naturalSize和列表分辨率视频m3u8
EN

Stack Overflow用户
提问于 2018-09-28 12:39:28
回答 2查看 1.4K关注 0票数 1

我玩视频m3u8。

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

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

EN

回答 2

Stack Overflow用户

发布于 2021-01-21 17:36:12

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

代码语言:javascript
复制
    /// 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
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-09-28 15:06:53

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

代码语言:javascript
复制
//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:…)如果你愿意的话。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52548401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档