50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package output
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"transcribe/internal/whisper"
|
|
)
|
|
|
|
// SRTFormatter formats transcription results as SRT subtitles
|
|
type SRTFormatter struct{}
|
|
|
|
// Format converts transcription result to SRT format
|
|
func (f *SRTFormatter) Format(result *whisper.TranscriptionResult) (string, error) {
|
|
var builder strings.Builder
|
|
|
|
for i, seg := range result.Segments {
|
|
// Subtitle number (1-indexed)
|
|
builder.WriteString(fmt.Sprintf("%d\n", i+1))
|
|
|
|
// Timestamps in SRT format: HH:MM:SS,mmm --> HH:MM:SS,mmm
|
|
startTime := formatSRTTimestamp(seg.Start)
|
|
endTime := formatSRTTimestamp(seg.End)
|
|
builder.WriteString(fmt.Sprintf("%s --> %s\n", startTime, endTime))
|
|
|
|
// Text with optional speaker label
|
|
text := strings.TrimSpace(seg.Text)
|
|
if seg.Speaker != "" {
|
|
text = fmt.Sprintf("[%s] %s", seg.Speaker, text)
|
|
}
|
|
builder.WriteString(text)
|
|
builder.WriteString("\n\n")
|
|
}
|
|
|
|
return strings.TrimSuffix(builder.String(), "\n"), nil
|
|
}
|
|
|
|
// formatSRTTimestamp converts seconds to SRT timestamp format (HH:MM:SS,mmm)
|
|
func formatSRTTimestamp(seconds float64) string {
|
|
totalMs := int64(seconds * 1000)
|
|
ms := totalMs % 1000
|
|
totalSeconds := totalMs / 1000
|
|
s := totalSeconds % 60
|
|
totalMinutes := totalSeconds / 60
|
|
m := totalMinutes % 60
|
|
h := totalMinutes / 60
|
|
|
|
return fmt.Sprintf("%02d:%02d:%02d,%03d", h, m, s, ms)
|
|
}
|