Skip to content

Commit

Permalink
更新 NewTimedCaption
Browse files Browse the repository at this point in the history
  • Loading branch information
tangshimin committed Sep 19, 2024
1 parent 6932f66 commit 057159d
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/main/kotlin/player/NewTimedCaption.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package player

import kotlinx.serialization.Serializable
import org.mozilla.universalchardet.UniversalDetector
import subtitleFile.FormatSRT
import java.io.File
import java.nio.charset.Charset

@Serializable
data class NewCaption(var start: Int, var end: Int, var content: String) {
Expand All @@ -13,25 +15,30 @@ data class NewCaption(var start: Int, var end: Int, var content: String) {


class NewTimedCaption(subtitleFile: File) {
private var captionList: List<NewCaption>
private var captionMap: Map<Int,NewCaption> = emptyMap()
private var captionList: List<NewCaption> = emptyList()
private var currentIndex = 0

init {
require(subtitleFile.exists()) { "Subtitle file does not exist" }

val encoding = UniversalDetector.detectCharset(subtitleFile)
val charset = if(encoding != null){
Charset.forName(encoding)
}else{
Charset.defaultCharset()
}
val formatSRT = subtitleFile.inputStream().use {
FormatSRT().parseFile(subtitleFile.name, it, Charsets.UTF_8)
FormatSRT().parseFile(subtitleFile.name, it, charset)
}

captionMap = formatSRT.captions.map {
it.key to NewCaption(
start = it.value.start.mseconds,
end = it.value.end.mseconds,
content = it.value.content
)
}.toMap()

captionList= captionMap.values.toList()
captionList= formatSRT.captions.values.map {
NewCaption(
start = it.start.mseconds,
end = it.end.mseconds,
content = it.content
)
}

}

Expand All @@ -57,6 +64,17 @@ class NewTimedCaption(subtitleFile: File) {
return currentIndex
}

}
fun isEmpty(): Boolean {
return captionList.isEmpty()
}

fun isNotEmpty(): Boolean {
return captionList.isNotEmpty()
}

fun clear(){
captionList = emptyList()
currentIndex = 0
}

}

0 comments on commit 057159d

Please sign in to comment.