Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix: incorrect subtitle timestamp caused by dash

沈浩4a8c48dee3c5
diff --git a/pkg/fasterwhisper/transcription.go b/pkg/fasterwhisper/transcription.go
index b7640cf8..23b151ee 100644
--- a/pkg/fasterwhisper/transcription.go
+++ b/pkg/fasterwhisper/transcription.go
@@ -45,21 +45,42 @@ func (c *FastwhisperProcessor) Transcription(audioFile, language, workDir string
}
var (
- transcripotionData types.TranscriptionData
- num int
+ transcriptionData types.TranscriptionData
+ num int
)
for _, segment := range result.Segments {
- transcripotionData.Text += segment.Text
+ transcriptionData.Text += strings.ReplaceAll(segment.Text, "—", " ") // 连字符处理,因为模型存在很多错误添加到连字符
for _, word := range segment.Words {
- transcripotionData.Words = append(transcripotionData.Words, types.Word{
- Num: num,
- Text: util.CleanPunction(strings.TrimSpace(word.Word)),
- Start: word.Start,
- End: word.End,
- })
- num++
+ if strings.Contains(word.Word, "—") {
+ // 对称切分
+ mid := (word.Start + word.End) / 2
+ seperatedWords := strings.Split(word.Word, "—")
+ transcriptionData.Words = append(transcriptionData.Words, []types.Word{
+ {
+ Num: num,
+ Text: util.CleanPunction(strings.TrimSpace(seperatedWords[0])),
+ Start: word.Start,
+ End: mid,
+ },
+ {
+ Num: num + 1,
+ Text: util.CleanPunction(strings.TrimSpace(seperatedWords[1])),
+ Start: mid,
+ End: word.End,
+ },
+ }...)
+ num += 2
+ } else {
+ transcriptionData.Words = append(transcriptionData.Words, types.Word{
+ Num: num,
+ Text: util.CleanPunction(strings.TrimSpace(word.Word)),
+ Start: word.Start,
+ End: word.End,
+ })
+ num++
+ }
}
}
log.GetLogger().Info("FastwhisperProcessor转录成功")
- return &transcripotionData, nil
+ return &transcriptionData, nil
}
diff --git a/pkg/openai/openai.go b/pkg/openai/openai.go
index b328d0a8..c8ad3a7f 100644
--- a/pkg/openai/openai.go
+++ b/pkg/openai/openai.go
@@ -6,6 +6,7 @@ import (
"go.uber.org/zap"
"krillin-ai/internal/types"
"krillin-ai/log"
+ "strings"
)
func (c *Client) ChatCompletion(query string) (string, error) {
@@ -54,19 +55,39 @@ func (c *Client) Transcription(audioFile, language, workDir string) (*types.Tran
transcriptionData := &types.TranscriptionData{
Language: resp.Language,
- Text: resp.Text,
+ Text: strings.ReplaceAll(resp.Text, "-", " "), // 连字符处理,因为模型存在很多错误添加到连字符
Words: make([]types.Word, 0),
}
num := 0
for _, word := range resp.Words {
- wordItem := types.Word{
- Num: num,
- Text: word.Word,
- Start: word.Start,
- End: word.End,
+ if strings.Contains(word.Word, "—") {
+ // 对称切分
+ mid := (word.Start + word.End) / 2
+ seperatedWords := strings.Split(word.Word, "—")
+ transcriptionData.Words = append(transcriptionData.Words, []types.Word{
+ {
+ Num: num,
+ Text: seperatedWords[0],
+ Start: word.Start,
+ End: mid,
+ },
+ {
+ Num: num + 1,
+ Text: seperatedWords[1],
+ Start: mid,
+ End: word.End,
+ },
+ }...)
+ num += 2
+ } else {
+ transcriptionData.Words = append(transcriptionData.Words, types.Word{
+ Num: num,
+ Text: word.Word,
+ Start: word.Start,
+ End: word.End,
+ })
+ num++
}
- num++
- transcriptionData.Words = append(transcriptionData.Words, wordItem)
}
return transcriptionData, nil