forked from recloudstream/cloudstream
Update M3u8Helper.kt
This commit is contained in:
parent
7dd62737e6
commit
dbc36d1388
1 changed files with 13 additions and 14 deletions
|
@ -10,10 +10,10 @@ import kotlin.math.pow
|
||||||
class M3u8Helper {
|
class M3u8Helper {
|
||||||
private val ENCRYPTION_DETECTION_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),")
|
private val ENCRYPTION_DETECTION_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),")
|
||||||
private val ENCRYPTION_URL_IV_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),URI=\"([^\"]+)\"(?:,IV=(.*))?")
|
private val ENCRYPTION_URL_IV_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),URI=\"([^\"]+)\"(?:,IV=(.*))?")
|
||||||
private val QUALITY_REGEX = Regex("""#EXT-X-STREAM-INF:.*(?:RESOLUTION=\d+x(\d+))?.*\n(.*)""")
|
private val QUALITY_REGEX = Regex("""#EXT-X-STREAM-INF:.*(?:RESOLUTION=\d+x(\d+))?.*\s(.*)""")
|
||||||
private val TS_EXTENSION_REGEX = Regex("""(.*\.ts.*)""")
|
private val TS_EXTENSION_REGEX = Regex("""(.*\.ts.*)""")
|
||||||
|
|
||||||
private fun absoluteExtensionDetermination(url: String): String? {
|
fun absoluteExtensionDetermination(url: String): String? {
|
||||||
val split = url.split("/")
|
val split = url.split("/")
|
||||||
val gg: String = split[split.size - 1].split("?")[0]
|
val gg: String = split[split.size - 1].split("?")[0]
|
||||||
return if (gg.contains(".")) {
|
return if (gg.contains(".")) {
|
||||||
|
@ -60,9 +60,8 @@ class M3u8Helper {
|
||||||
private fun selectBest(qualities: List<M3u8Stream>): M3u8Stream? {
|
private fun selectBest(qualities: List<M3u8Stream>): M3u8Stream? {
|
||||||
val result = qualities.sortedBy {
|
val result = qualities.sortedBy {
|
||||||
if (it.quality != null && it.quality <= 1080) it.quality else 0
|
if (it.quality != null && it.quality <= 1080) it.quality else 0
|
||||||
}.reversed().filter {
|
}.filter {
|
||||||
it.streamUrl.contains(".m3u8")
|
listOf("m3u", "m3u8").contains(absoluteExtensionDetermination(it.streamUrl))
|
||||||
// listOf("m3u", "m3u8").contains(absoluteExtensionDetermination(it.streamUrl))
|
|
||||||
}
|
}
|
||||||
return result.getOrNull(0)
|
return result.getOrNull(0)
|
||||||
}
|
}
|
||||||
|
@ -73,8 +72,8 @@ class M3u8Helper {
|
||||||
return split.joinToString("/")
|
return split.joinToString("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isCompleteUrl(url: String): Boolean {
|
private fun isNotCompleteUrl(url: String): Boolean {
|
||||||
return url.contains("https://") && url.contains("http://")
|
return !url.contains("https://") && !url.contains("http://")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun m3u8Generation(m3u8: M3u8Stream): List<M3u8Stream> {
|
fun m3u8Generation(m3u8: M3u8Stream): List<M3u8Stream> {
|
||||||
|
@ -85,7 +84,7 @@ class M3u8Helper {
|
||||||
for (match in QUALITY_REGEX.findAll(response.text)) {
|
for (match in QUALITY_REGEX.findAll(response.text)) {
|
||||||
var (quality, m3u8Link) = match.destructured
|
var (quality, m3u8Link) = match.destructured
|
||||||
if (absoluteExtensionDetermination(m3u8Link) == "m3u8") {
|
if (absoluteExtensionDetermination(m3u8Link) == "m3u8") {
|
||||||
if (!isCompleteUrl(m3u8Link)) {
|
if (isNotCompleteUrl(m3u8Link)) {
|
||||||
m3u8Link = "$m3u8Parent/$m3u8Link"
|
m3u8Link = "$m3u8Parent/$m3u8Link"
|
||||||
}
|
}
|
||||||
yieldAll(
|
yieldAll(
|
||||||
|
@ -118,7 +117,7 @@ class M3u8Helper {
|
||||||
)
|
)
|
||||||
|
|
||||||
fun hlsYield(qualities: List<M3u8Stream>, startIndex: Int = 0): Iterator<HlsDownloadData> {
|
fun hlsYield(qualities: List<M3u8Stream>, startIndex: Int = 0): Iterator<HlsDownloadData> {
|
||||||
if (qualities.isEmpty()) return listOf(HlsDownloadData(byteArrayOf(), 0, 0, true)).iterator()
|
if (qualities.isEmpty()) return listOf(HlsDownloadData(byteArrayOf(), 1, 1, true)).iterator()
|
||||||
|
|
||||||
var selected = selectBest(qualities)
|
var selected = selectBest(qualities)
|
||||||
if (selected == null) {
|
if (selected == null) {
|
||||||
|
@ -145,7 +144,7 @@ class M3u8Helper {
|
||||||
ENCRYPTION_URL_IV_REGEX.find(m3u8Data)!!.destructured // its safe to assume that its not going to be null
|
ENCRYPTION_URL_IV_REGEX.find(m3u8Data)!!.destructured // its safe to assume that its not going to be null
|
||||||
encryptionUri = match.component2()
|
encryptionUri = match.component2()
|
||||||
|
|
||||||
if (!isCompleteUrl(encryptionUri)) {
|
if (isNotCompleteUrl(encryptionUri)) {
|
||||||
encryptionUri = "${getParentLink(secondSelection.streamUrl)}/$encryptionUri"
|
encryptionUri = "${getParentLink(secondSelection.streamUrl)}/$encryptionUri"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +157,7 @@ class M3u8Helper {
|
||||||
val allTsList = allTs.toList()
|
val allTsList = allTs.toList()
|
||||||
val totalTs = allTsList.size
|
val totalTs = allTsList.size
|
||||||
if (totalTs == 0) {
|
if (totalTs == 0) {
|
||||||
return listOf(HlsDownloadData(byteArrayOf(), 0, 0, true)).iterator()
|
return listOf(HlsDownloadData(byteArrayOf(), 1, 1, true)).iterator()
|
||||||
}
|
}
|
||||||
var lastYield = 0
|
var lastYield = 0
|
||||||
|
|
||||||
|
@ -167,8 +166,8 @@ class M3u8Helper {
|
||||||
val tsByteGen = sequence {
|
val tsByteGen = sequence {
|
||||||
loop@ for ((index, ts) in allTs.withIndex()) {
|
loop@ for ((index, ts) in allTs.withIndex()) {
|
||||||
val url = if (
|
val url = if (
|
||||||
isCompleteUrl(ts.destructured.component1())
|
isNotCompleteUrl(ts.destructured.component1())
|
||||||
) ts.destructured.component1() else "$relativeUrl/${ts.destructured.component1()}"
|
) "$relativeUrl/${ts.destructured.component1()}" else ts.destructured.component1()
|
||||||
val c = index + 1 + startIndex
|
val c = index + 1 + startIndex
|
||||||
|
|
||||||
while (lastYield != c) {
|
while (lastYield != c) {
|
||||||
|
@ -198,6 +197,6 @@ class M3u8Helper {
|
||||||
}
|
}
|
||||||
return tsByteGen.iterator()
|
return tsByteGen.iterator()
|
||||||
}
|
}
|
||||||
return listOf(HlsDownloadData(byteArrayOf(), 0, 0, true)).iterator()
|
return listOf(HlsDownloadData(byteArrayOf(), 1, 1, true)).iterator()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue