Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加M3U8内容修改功能 #81

Open
refgd opened this issue Oct 3, 2024 · 0 comments
Open

添加M3U8内容修改功能 #81

refgd opened this issue Oct 3, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@refgd
Copy link

refgd commented Oct 3, 2024

功能范围

插件能力, 播放器

功能描述

如题, 以下是代码参考

fun getMediaSourceFactory(playerInfo: PlayerInfo): MediaSource.Factory {
    val httpDataSourceFactory = DefaultHttpDataSource.Factory()
        .setDefaultRequestProperties(playerInfo.header ?: emptyMap())
    val dataSourceFactory = DefaultDataSource.Factory(APP, httpDataSourceFactory)
    val streamDataSinkFactory = CacheDataSink.Factory().setCache(normalCache)
    val normalCacheDataSourceFactory = CacheDataSource.Factory()
        .setCache(normalCache)
        .setUpstreamDataSourceFactory(dataSourceFactory)
        .setCacheWriteDataSinkFactory(streamDataSinkFactory)
        .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)

    return when (playerInfo.decodeType) {
        C.CONTENT_TYPE_DASH -> DashMediaSource.Factory(normalCacheDataSourceFactory)
        C.CONTENT_TYPE_HLS -> {
            // Use custom DataSource.Factory to modify M3U8 content
            val customHlsDataSourceFactory = CustomDataSourceFactory(
                normalCacheDataSourceFactory, playerInfo.replaces
            )
            HlsMediaSource.Factory(customHlsDataSourceFactory)
        }
        else -> ProgressiveMediaSource.Factory(normalCacheDataSourceFactory)
    }
}

// Custom DataSource.Factory that modifies M3U8 content
class CustomDataSourceFactory(
    private val upstreamFactory: DataSource.Factory,
    private val replaces: List>
) : DataSource.Factory {

    override fun createDataSource(): DataSource {
        return object : DataSource {
            private var upstream: DataSource = upstreamFactory.createDataSource()

            override fun open(dataSpec: DataSpec): Long {
                return upstream.open(dataSpec)
            }

            override fun read(buffer: ByteArray, offset: Int, readLength: Int): Int {
                val result = upstream.read(buffer, offset, readLength)
                if (result == C.RESULT_END_OF_INPUT) {
                    val m3u8Content = String(buffer, StandardCharsets.UTF_8)
                    val modifiedM3u8Content = modifyM3u8Content(m3u8Content, replaces)
                    val byteArrayInputStream: InputStream = ByteArrayInputStream(
                        modifiedM3u8Content.toByteArray(StandardCharsets.UTF_8)
                    )
                    return byteArrayInputStream.read(buffer, offset, readLength)
                }
                return result
            }

            override fun getUri(): Uri? = upstream.uri

            override fun close() {
                upstream.close()
            }
        }
    }

    // Function to modify M3U8 content using the list of regex replacements
    private fun modifyM3u8Content(content: String, replaces: List>): String {
        var modifiedContent = content
        for ((regex, replacement) in replaces) {
            modifiedContent = regex.replace(modifiedContent, replacement)
        }
        return modifiedContent
    }
}
@refgd refgd added the enhancement New feature or request label Oct 3, 2024
@AyalaKaguya AyalaKaguya moved this to 未计划 in 纯纯看番 Nov 29, 2024
@AyalaKaguya AyalaKaguya moved this from 未计划 to 计划中 in 纯纯看番 Nov 29, 2024
@AyalaKaguya AyalaKaguya moved this from 计划中 to 正在进行 in 纯纯看番 Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 正在进行
Development

No branches or pull requests

1 participant