从iOS中的HLS下载特定分辨率

时间:2019-03-14 05:08:12

标签: ios objective-c video-streaming hls avasset

我正在使用以下代码从HLS下载视频。它工作正常,并且始终下载高分辨率。我该如何切换到想要的特定分辨率?

NSURLSessionConfiguration *urlSessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"assetDowloadConfigIdentifier"];
AVAssetDownloadURLSession *avAssetDownloadSession = [AVAssetDownloadURLSession sessionWithConfiguration:urlSessionConfiguration assetDownloadDelegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURL *assetURL = [NSURL URLWithString:@"https://a4i6y2k6.stackpathcdn.com/vistvorigin/smil:4b0d690b7b3bc8ac5da2049f50c80794c762423e.smil/playlist.m3u8"];
AVURLAsset *hlsAsset = [AVURLAsset assetWithURL:assetURL];

if (@available(iOS 10.0, *)) {
    AVAssetDownloadTask *avAssetDownloadTask = [avAssetDownloadSession assetDownloadTaskWithURLAsset:hlsAsset assetTitle:@"downloadedMedia" assetArtworkData:nil options:videoSettings];
    AVKeyValueStatus status = [hlsAsset statusOfValueForKey:@"tracks" error:nil];if (AVKeyValueStatusLoaded != status) {     NSArray *keys = [NSArray arrayWithObject:@"tracks"];     [hlsAsset loadValuesAsynchronouslyForKeys:keys                          completionHandler:^{
    }];}
[avAssetDownloadTask resume];

1 个答案:

答案 0 :(得分:2)

此答案很快。下载特定分辨率的关键是将 AVAssetDownloadTaskMinimumRequiredMediaBitrateKey 设置为略低于所需比特率的比特率,因为它考虑了最小比特率

if #available(iOS 10.0, *) {
        if let downloadTask = hlsDownloadSession.makeAssetDownloadTask(asset: asset,
                                                                    assetTitle: "samplevid",
                                                                    assetArtworkData: nil,
                                                                    options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 200000]){


            downloadTask.resume() 
 }
}

这将下载比特率高于200000的媒体