3

The below code i am using to download video from HLS. it's working good and always downloading high resolution. how can i switch to specific resolution which i want?

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];
Code cracker
  • 3,009
  • 6
  • 32
  • 58
  • before download you need to do like parse your.m3u8 URL, it gives OP as like [this](https://stackoverflow.com/questions/35030171/download-video-with-m3u8-url-in-ios-app?rq=1), in here based on the resolution you need to start your download – Anbu.Karthik Mar 14 '19 at 05:22
  • You don't need to parse the m3u8 actually for download, by default, the highest media bitrate will be selected for download. If you pass the options parameter it'll use that value as NSNumber in bps. Usually we know our options but anyway we need those media options from API, but unfortunately, API doesn't share if you use HLS. So sad :( – mgyky Jun 18 '20 at 13:09

1 Answers1

2

This answer is in swift. The key to download specific resolution is to set AVAssetDownloadTaskMinimumRequiredMediaBitrateKey to a bitrate slightly below the required bitrate since it takes the minimum bitrate into account

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


            downloadTask.resume() 
 }
}

This downloads the media with bitrate higher than 200000

Kathir
  • 87
  • 5
  • Will it not download the higher version? – abh Jun 01 '20 at 07:18
  • 1
    @babar By default, the highest media bitrate will be selected for download. If you pass the options parameter like above The lowest media bitrate greater than or equal to this value will be selected. Value should be a NSNumber in bps. If no suitable media bitrate is found, the highest media bitrate will be selected. – mgyky Jun 18 '20 at 13:04