Skip to content

Commit

Permalink
修复:视频的时间大于24小时会报错,不能播放字幕(#44)。
Browse files Browse the repository at this point in the history
  • Loading branch information
tangshimin committed Jan 1, 2024
1 parent 88dc36a commit 60378d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/jvmMain/kotlin/player/Play.kt
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ fun play(
val caption = playTriple.first
val relativeVideoPath = playTriple.second
val trackId = playTriple.third
val start = parseTime(caption.start)
val end = parseTime(caption.end)
val start = convertTimeToSeconds(caption.start)
val end = convertTimeToSeconds(caption.end)
// 使用内部字幕轨道,通常是从 MKV 生成的词库
if(trackId != -1){
videoPlayerComponent.mediaPlayer().media()
Expand Down Expand Up @@ -414,16 +414,16 @@ fun play(
audioPlayerComponent.mediaPlayer().events().removeMediaPlayerEventListener(this)
}
})
val start = parseTime(caption.start)
val end = parseTime(caption.end)
val start = convertTimeToSeconds(caption.start)
val end = convertTimeToSeconds(caption.end)
audioPlayerComponent.mediaPlayer().media()
.play(videoPath, ":start-time=$start", ":stop-time=$end")
}

/**
* 解析时间,返回秒
* 转换时间为秒
*/
fun parseTime(time:String):Double{
fun convertTimeToSeconds(time:String):Double{
var duration = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm:ss.SSS")).toNanoOfDay().toDouble()
duration = duration.div(1000_000_000)
return duration
Expand Down
4 changes: 2 additions & 2 deletions src/jvmMain/kotlin/player/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ fun rememberDanmakuMap(
vocabulary.wordList.forEach { word ->
if (word.captions.isNotEmpty()) {
word.captions.forEach { caption ->
val startTime = floor(parseTime(caption.start)).toInt()
val startTime = floor(convertTimeToSeconds(caption.start)).toInt()
addDanmakuToMap(timeMap, startTime, word)
}
}
Expand All @@ -1444,7 +1444,7 @@ fun rememberDanmakuMap(
if ((absVideoFile.exists() && absVideoFile.absolutePath == externalCaption.relateVideoPath) ||
(relVideoFile.exists() && relVideoFile.name == File(externalCaption.relateVideoPath).name)
) {
val startTime = floor(parseTime(externalCaption.start)).toInt()
val startTime = floor(convertTimeToSeconds(externalCaption.start)).toInt()
addDanmakuToMap(timeMap, startTime, word)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jvmMain/kotlin/player/SimplePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ fun SimplePlayer(
val caption = playTriple.first
val relativeVideoPath = playTriple.second
val trackId = playTriple.third
val start = parseTime(caption.start)
val end = parseTime(caption.end)
val start = convertTimeToSeconds(caption.start)
val end = convertTimeToSeconds(caption.end)
// 使用内部字幕轨道,通常是从 MKV 生成的词库
if(trackId != -1){
videoPlayerComponent.mediaPlayer().media()
Expand Down
52 changes: 26 additions & 26 deletions src/jvmMain/kotlin/player/TimedCaption.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class TimedCaption{
clear()
captions.forEachIndexed { index, caption ->
captionList.add(caption)
val start = parseTime(caption.start)
val end = parseTime(caption.end)
val start = convertTimeToSeconds(caption.start)
val end = convertTimeToSeconds(caption.end)
val startInt = start.toInt()
val list = timeMap[startInt] ?: mutableListOf()
list.add(Triple(start,end,index))
Expand All @@ -44,13 +44,13 @@ class TimedCaption{
}
in nextCaptionStart..nextCaptionEnd -> {
val index = currentIndex + 1
previousCaptionStart = parseTime(captionList[index - 1].start).times(1000).toLong()
previousCaptionEnd = parseTime(captionList[index - 1].end).times(1000).toLong()
previousCaptionStart = convertTimeToSeconds(captionList[index - 1].start).times(1000).toLong()
previousCaptionEnd = convertTimeToSeconds(captionList[index - 1].end).times(1000).toLong()
captionStart = nextCaptionStart
captionEnd = nextCaptionEnd
if(index + 1 < captionList.size){
nextCaptionStart = parseTime(captionList[index + 1].start).times(1000).toLong()
nextCaptionEnd = parseTime(captionList[index + 1].end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(captionList[index + 1].start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(captionList[index + 1].end).times(1000).toLong()
}

return captionList[index]
Expand All @@ -61,12 +61,12 @@ class TimedCaption{
captionStart = previousCaptionStart
captionEnd = previousCaptionEnd

previousCaptionStart = parseTime(captionList[index - 1].start).times(1000).toLong()
previousCaptionEnd = parseTime(captionList[index - 1].end).times(1000).toLong()
previousCaptionStart = convertTimeToSeconds(captionList[index - 1].start).times(1000).toLong()
previousCaptionEnd = convertTimeToSeconds(captionList[index - 1].end).times(1000).toLong()

if(index + 1 < captionList.size){
nextCaptionStart = parseTime(captionList[index + 1].start).times(1000).toLong()
nextCaptionEnd = parseTime(captionList[index + 1].end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(captionList[index + 1].start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(captionList[index + 1].end).times(1000).toLong()
}

return captionList[index]
Expand All @@ -78,13 +78,13 @@ class TimedCaption{
val start = it.first.times(1000).toLong()
val end = it.second.times(1000).toLong()
if(newTime in start..end){
previousCaptionStart = parseTime(captionList[it.third - 1].start).times(1000).toLong()
previousCaptionEnd = parseTime(captionList[it.third - 1].end).times(1000).toLong()
previousCaptionStart = convertTimeToSeconds(captionList[it.third - 1].start).times(1000).toLong()
previousCaptionEnd = convertTimeToSeconds(captionList[it.third - 1].end).times(1000).toLong()
captionStart = start
captionEnd = end
if(it.third + 1 < captionList.size){
nextCaptionStart = parseTime(captionList[it.third + 1].start).times(1000).toLong()
nextCaptionEnd = parseTime(captionList[it.third + 1].end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(captionList[it.third + 1].start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(captionList[it.third + 1].end).times(1000).toLong()
}
return captionList[it.third]
}
Expand All @@ -106,8 +106,8 @@ class TimedCaption{
captionEnd = nextCaptionEnd
if(index + 1 < captionList.size){
val nextCaption = captionList[index + 1]
nextCaptionStart = parseTime(nextCaption.start).times(1000).toLong()
nextCaptionEnd = parseTime(nextCaption.end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(nextCaption.start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(nextCaption.end).times(1000).toLong()
}
currentIndex = index
println("next")
Expand All @@ -125,8 +125,8 @@ class TimedCaption{
captionEnd = end
if(it.third + 1 < captionList.size){
val nextCaption = captionList[it.third + 1]
nextCaptionStart = parseTime(nextCaption.start).times(1000).toLong()
nextCaptionEnd = parseTime(nextCaption.end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(nextCaption.start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(nextCaption.end).times(1000).toLong()
}
currentIndex = it.third
return captionList[it.third]
Expand All @@ -149,13 +149,13 @@ class TimedCaption{
val start = it.first.times(1000).toLong()
val end = it.second.times(1000).toLong()
if(newTime in start..end){
previousCaptionStart = parseTime(captionList[it.third - 1].start).times(1000).toLong()
previousCaptionEnd = parseTime(captionList[it.third - 1].end).times(1000).toLong()
previousCaptionStart = convertTimeToSeconds(captionList[it.third - 1].start).times(1000).toLong()
previousCaptionEnd = convertTimeToSeconds(captionList[it.third - 1].end).times(1000).toLong()
captionStart = start
captionEnd = end
if(it.third + 1 < captionList.size){
nextCaptionStart = parseTime(captionList[it.third + 1].start).times(1000).toLong()
nextCaptionEnd = parseTime(captionList[it.third + 1].end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(captionList[it.third + 1].start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(captionList[it.third + 1].end).times(1000).toLong()
}
return captionList[it.third]
}
Expand All @@ -181,8 +181,8 @@ class TimedCaption{
captionEnd = nextCaptionEnd
if(index + 1 < captionList.size){
val nextCaption = captionList[index + 1]
nextCaptionStart = parseTime(nextCaption.start).times(1000).toLong()
nextCaptionEnd = parseTime(nextCaption.end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(nextCaption.start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(nextCaption.end).times(1000).toLong()
}
return index
}
Expand All @@ -198,8 +198,8 @@ class TimedCaption{
captionEnd = end
if(index + 1 < captionList.size){
val nextCaption = captionList[index + 1]
nextCaptionStart = parseTime(nextCaption.start).times(1000).toLong()
nextCaptionEnd = parseTime(nextCaption.end).times(1000).toLong()
nextCaptionStart = convertTimeToSeconds(nextCaption.start).times(1000).toLong()
nextCaptionEnd = convertTimeToSeconds(nextCaption.end).times(1000).toLong()
}
return index
}
Expand Down

0 comments on commit 60378d8

Please sign in to comment.