Home > OS >  Determine required disk space size before downloading HLS with ExoPlayer
Determine required disk space size before downloading HLS with ExoPlayer

Time:08-08

I use HLS protocol and I need to determine the size of the video with the selected resolution, subtitles, and audio tracks. Unfortunately, I haven't found any examples in demo-app. I could define only the video track's size by multiplying the selected track's bitrate. But the size of multiple audio tracks and subtitles is not included in this number.

CodePudding user response:

The best solution that I found

 private fun estimateTotalSize(helper: DownloadHelper, hlsAudioBitrate: Int): Long {
    var selectedSize: Long = 0
    val durationMs: Long = when (val manifest = helper.manifest) {
        is DashManifest -> {
            manifest.durationMs
        }
        is HlsManifest -> {
            manifest.mediaPlaylist.durationUs / 1000
        }
        is SsManifest -> {
            manifest.durationUs / 1000
        }
        else -> {
            0 // FIXME
        }
    }
    for (pi in 0 until helper.periodCount) {
        val mappedTrackInfo = helper.getMappedTrackInfo(pi)
        val rendererCount = mappedTrackInfo.rendererCount
        for (i in 0 until rendererCount) {
            val trackSelections: List<TrackSelection> = helper.getTrackSelections(pi, i)
            for (selection in trackSelections) {
                val format: Format = selection.getFormat(0)
                var bitrate: Int = format.bitrate
                if (bitrate <= 0) {
                    if (format.sampleMimeType != null && format.sampleMimeType!!.startsWith("audio/")) {
                        bitrate = hlsAudioBitrate
                    }
                }
                selectedSize  = bitrate * durationMs / 1000 / 8
            }
        }
    }
    return selectedSize
}
  • Related