我正在使用以下代码从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];
此答案很快。下载特定分辨率的关键是将AVAssetDownloadTaskMinimumRequiredMediaBitrateKey设置为略低于所需比特率的比特率,因为它考虑了最小比特率
if #available(iOS 10.0, *) {
if let downloadTask = hlsDownloadSession.makeAssetDownloadTask(asset: asset,
assetTitle: "samplevid",
assetArtworkData: nil,
options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 200000]){
downloadTask.resume()
}
}
这将下载比特率高于200000的媒体