I FUCKING HATE EXOPLAYER SUBTITLES
This commit is contained in:
parent
ea96291bfc
commit
87a9df4c12
9 changed files with 257 additions and 50 deletions
|
@ -1,19 +1,23 @@
|
|||
package ani.dantotsu.media
|
||||
|
||||
import android.content.Context
|
||||
import ani.dantotsu.download.DownloadedType
|
||||
import ani.dantotsu.download.DownloadsManager
|
||||
import ani.dantotsu.parsers.SubtitleType
|
||||
import ani.dantotsu.snackString
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.Request
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import java.io.File
|
||||
|
||||
class SubtitleDownloader {
|
||||
|
||||
companion object {
|
||||
//doesn't really download the subtitles -\_(o_o)_/-
|
||||
suspend fun downloadSubtitles(context: Context, url: String): SubtitleType =
|
||||
suspend fun loadSubtitleType(context: Context, url: String): SubtitleType =
|
||||
withContext(Dispatchers.IO) {
|
||||
// Initialize the NetworkHelper instance. Replace this line based on how you usually initialize it
|
||||
val networkHelper = Injekt.get<NetworkHelper>()
|
||||
|
@ -29,8 +33,8 @@ class SubtitleDownloader {
|
|||
|
||||
|
||||
val subtitleType = when {
|
||||
responseBody.contains("[Script Info]") == true -> SubtitleType.ASS
|
||||
responseBody.contains("WEBVTT") == true -> SubtitleType.VTT
|
||||
responseBody.contains("[Script Info]") -> SubtitleType.ASS
|
||||
responseBody.contains("WEBVTT") -> SubtitleType.VTT
|
||||
else -> SubtitleType.SRT
|
||||
}
|
||||
|
||||
|
@ -39,5 +43,41 @@ class SubtitleDownloader {
|
|||
return@withContext SubtitleType.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
//actually downloads lol
|
||||
suspend fun downloadSubtitle(context: Context, url: String, downloadedType: DownloadedType) {
|
||||
try {
|
||||
val directory = DownloadsManager.getDirectory(context, downloadedType.type, downloadedType.title, downloadedType.chapter)
|
||||
if (!directory.exists()) { //just in case
|
||||
directory.mkdirs()
|
||||
}
|
||||
val type = loadSubtitleType(context, url)
|
||||
val subtiteFile = File(directory, "subtitle.${type}")
|
||||
if (subtiteFile.exists()) {
|
||||
subtiteFile.delete()
|
||||
}
|
||||
subtiteFile.createNewFile()
|
||||
|
||||
val client = Injekt.get<NetworkHelper>().client
|
||||
val request = Request.Builder().url(url).build()
|
||||
val reponse = client.newCall(request).execute()
|
||||
|
||||
if (!reponse.isSuccessful) {
|
||||
snackString("Failed to download subtitle")
|
||||
return
|
||||
}
|
||||
|
||||
reponse.body.byteStream().use { input ->
|
||||
subtiteFile.outputStream().use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
snackString("Failed to download subtitle")
|
||||
e.printStackTrace()
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,9 @@ import androidx.media3.common.*
|
|||
import androidx.media3.common.C.AUDIO_CONTENT_TYPE_MOVIE
|
||||
import androidx.media3.common.C.TRACK_TYPE_VIDEO
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.common.util.Util
|
||||
import androidx.media3.datasource.DataSource
|
||||
import androidx.media3.datasource.DefaultDataSourceFactory
|
||||
import androidx.media3.datasource.HttpDataSource
|
||||
import androidx.media3.datasource.cache.CacheDataSource
|
||||
import androidx.media3.datasource.okhttp.OkHttpDataSource
|
||||
|
@ -1284,7 +1286,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
if (subtitle?.type == SubtitleType.UNKNOWN) {
|
||||
val context = this
|
||||
runBlocking {
|
||||
val type = SubtitleDownloader.downloadSubtitles(context, subtitle!!.file.url)
|
||||
val type = SubtitleDownloader.loadSubtitleType(context, subtitle!!.file.url)
|
||||
val fileUri = Uri.parse(subtitle!!.file.url)
|
||||
sub = MediaItem.SubtitleConfiguration
|
||||
.Builder(fileUri)
|
||||
|
@ -1302,8 +1304,9 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
}
|
||||
println("sub: $sub")
|
||||
} else {
|
||||
val subUri = Uri.parse((subtitle!!.file.url))
|
||||
sub = MediaItem.SubtitleConfiguration
|
||||
.Builder(Uri.parse(subtitle!!.file.url))
|
||||
.Builder(subUri)
|
||||
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
|
||||
.setMimeType(
|
||||
when (subtitle?.type) {
|
||||
|
@ -1338,9 +1341,14 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
}
|
||||
dataSource
|
||||
}
|
||||
val dafuckDataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, R.string.app_name.toString()))
|
||||
cacheFactory = CacheDataSource.Factory().apply {
|
||||
setCache(Helper.getSimpleCache(this@ExoplayerView))
|
||||
setUpstreamDataSourceFactory(dataSourceFactory)
|
||||
if (ext.server.offline) {
|
||||
setUpstreamDataSourceFactory(dafuckDataSourceFactory)
|
||||
} else {
|
||||
setUpstreamDataSourceFactory(dataSourceFactory)
|
||||
}
|
||||
setCacheWriteDataSinkFactory(null)
|
||||
}
|
||||
|
||||
|
@ -1374,7 +1382,14 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
}
|
||||
builder.build()
|
||||
} else {
|
||||
downloadedMediaItem
|
||||
val addedSubsDownloadedMediaItem = downloadedMediaItem.buildUpon()
|
||||
if (sub != null) {
|
||||
val listofnotnullsubs = immutableListOf(sub).filterNotNull()
|
||||
val addLanguage = listofnotnullsubs[0].buildUpon().setLanguage("en").build()
|
||||
addedSubsDownloadedMediaItem.setSubtitleConfigurations(immutableListOf(addLanguage))
|
||||
episode.selectedSubtitle = 0
|
||||
}
|
||||
addedSubsDownloadedMediaItem.build()
|
||||
}
|
||||
|
||||
|
||||
|
@ -1635,7 +1650,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
if (isInitialized) {
|
||||
if (exoPlayer.currentPosition.toFloat() / exoPlayer.duration > settings.watchPercentage) {
|
||||
preloading = true
|
||||
nextEpisode(false) { i -> //TODO: make sure this works for offline episodes
|
||||
nextEpisode(false) { i ->
|
||||
val ep = episodes[episodeArr[currentEpisodeIndex + i]] ?: return@nextEpisode
|
||||
val selected = media.selected ?: return@nextEpisode
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
|
@ -1806,7 +1821,6 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
|
|||
}
|
||||
|
||||
super.onDestroy()
|
||||
Glide.with(this).clear(exoPlay)
|
||||
finishAndRemoveTask()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ani.dantotsu.media.anime
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.AlertDialog
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
|
@ -24,6 +25,7 @@ import ani.dantotsu.download.video.Helper
|
|||
import ani.dantotsu.media.Media
|
||||
import ani.dantotsu.media.MediaDetailsViewModel
|
||||
import ani.dantotsu.others.Download.download
|
||||
import ani.dantotsu.parsers.Subtitle
|
||||
import ani.dantotsu.parsers.VideoExtractor
|
||||
import ani.dantotsu.parsers.VideoType
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
|
@ -302,18 +304,68 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
|
|||
val episode = media!!.anime!!.episodes!![media!!.anime!!.selectedEpisode!!]!!
|
||||
val selectedVideo =
|
||||
if (extractor.videos.size > episode.selectedVideo) extractor.videos[episode.selectedVideo] else null
|
||||
if (selectedVideo != null) {
|
||||
Helper.startAnimeDownloadService(
|
||||
requireActivity(),
|
||||
media!!.mainName(),
|
||||
episode.number,
|
||||
selectedVideo,
|
||||
null,
|
||||
media,
|
||||
episode.thumb?.url ?: media!!.banner ?: media!!.cover
|
||||
)
|
||||
|
||||
val subtitles = extractor.subtitles
|
||||
val subtitleNames = subtitles.map { it.language }
|
||||
var subtitleToDownload: Subtitle? = null
|
||||
if (subtitles.isNotEmpty()) {
|
||||
AlertDialog.Builder(context, R.style.MyPopup)
|
||||
.setTitle("Download Subtitle")
|
||||
.setSingleChoiceItems(
|
||||
subtitleNames.toTypedArray(),
|
||||
-1
|
||||
) { dialog, which ->
|
||||
subtitleToDownload = subtitles[which]
|
||||
}
|
||||
.setPositiveButton("Download") { _, _ ->
|
||||
dialog?.dismiss()
|
||||
if (selectedVideo != null) {
|
||||
Helper.startAnimeDownloadService(
|
||||
currActivity()!!,
|
||||
media!!.mainName(),
|
||||
episode.number,
|
||||
selectedVideo,
|
||||
subtitleToDownload,
|
||||
media,
|
||||
episode.thumb?.url ?: media!!.banner ?: media!!.cover
|
||||
)
|
||||
} else {
|
||||
snackString("No Video Selected")
|
||||
}
|
||||
}
|
||||
.setNegativeButton("Cancel") { dialog, _ ->
|
||||
subtitleToDownload = null
|
||||
dialog.dismiss()
|
||||
if (selectedVideo != null) {
|
||||
Helper.startAnimeDownloadService(
|
||||
currActivity()!!,
|
||||
media!!.mainName(),
|
||||
episode.number,
|
||||
selectedVideo,
|
||||
subtitleToDownload,
|
||||
media,
|
||||
episode.thumb?.url ?: media!!.banner ?: media!!.cover
|
||||
)
|
||||
} else {
|
||||
snackString("No Video Selected")
|
||||
}
|
||||
}
|
||||
.show()
|
||||
|
||||
} else {
|
||||
snackString("No Video Selected")
|
||||
if (selectedVideo != null) {
|
||||
Helper.startAnimeDownloadService(
|
||||
requireActivity(),
|
||||
media!!.mainName(),
|
||||
episode.number,
|
||||
selectedVideo,
|
||||
subtitleToDownload,
|
||||
media,
|
||||
episode.thumb?.url ?: media!!.banner ?: media!!.cover
|
||||
)
|
||||
} else {
|
||||
snackString("No Video Selected")
|
||||
}
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
|
|
|
@ -6,7 +6,9 @@ import android.os.Bundle
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import ani.dantotsu.BottomSheetDialogFragment
|
||||
|
@ -60,6 +62,7 @@ class SubtitleDialogFragment : BottomSheetDialogFragment() {
|
|||
)
|
||||
)
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
override fun onBindViewHolder(holder: StreamViewHolder, position: Int) {
|
||||
val binding = holder.binding
|
||||
if (position == 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue