42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package output
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"transcribe/internal/whisper"
|
|
)
|
|
|
|
// TextFormatter formats transcription results as plain text with timestamps
|
|
type TextFormatter struct{}
|
|
|
|
// Format converts transcription result to plain text with timestamps
|
|
func (f *TextFormatter) Format(result *whisper.TranscriptionResult) (string, error) {
|
|
var builder strings.Builder
|
|
|
|
for _, seg := range result.Segments {
|
|
// Format: [MM:SS - MM:SS] [Speaker] Text
|
|
startTime := formatTextTimestamp(seg.Start)
|
|
endTime := formatTextTimestamp(seg.End)
|
|
|
|
text := strings.TrimSpace(seg.Text)
|
|
if seg.Speaker != "" {
|
|
builder.WriteString(fmt.Sprintf("[%s - %s] [%s] %s\n", startTime, endTime, seg.Speaker, text))
|
|
} else {
|
|
builder.WriteString(fmt.Sprintf("[%s - %s] %s\n", startTime, endTime, text))
|
|
}
|
|
}
|
|
|
|
return strings.TrimSuffix(builder.String(), "\n"), nil
|
|
}
|
|
|
|
// formatTextTimestamp converts seconds to MM:SS.s format
|
|
func formatTextTimestamp(seconds float64) string {
|
|
totalSeconds := int(seconds)
|
|
m := totalSeconds / 60
|
|
s := totalSeconds % 60
|
|
tenths := int((seconds - float64(totalSeconds)) * 10)
|
|
|
|
return fmt.Sprintf("%02d:%02d.%d", m, s, tenths)
|
|
}
|