feat: git init
This commit is contained in:
33
pkg/output/formatter.go
Normal file
33
pkg/output/formatter.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"transcribe/internal/whisper"
|
||||
)
|
||||
|
||||
// Formatter interface for converting transcription results to various output formats
|
||||
type Formatter interface {
|
||||
Format(result *whisper.TranscriptionResult) (string, error)
|
||||
}
|
||||
|
||||
// FormatType represents the output format type
|
||||
type FormatType string
|
||||
|
||||
const (
|
||||
FormatText FormatType = "text"
|
||||
FormatSRT FormatType = "srt"
|
||||
FormatJSON FormatType = "json"
|
||||
)
|
||||
|
||||
// NewFormatter creates a formatter for the given format type
|
||||
func NewFormatter(format FormatType) Formatter {
|
||||
switch format {
|
||||
case FormatSRT:
|
||||
return &SRTFormatter{}
|
||||
case FormatJSON:
|
||||
return &JSONFormatter{}
|
||||
case FormatText:
|
||||
fallthrough
|
||||
default:
|
||||
return &TextFormatter{}
|
||||
}
|
||||
}
|
||||
19
pkg/output/json.go
Normal file
19
pkg/output/json.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"transcribe/internal/whisper"
|
||||
)
|
||||
|
||||
// JSONFormatter formats transcription results as JSON
|
||||
type JSONFormatter struct{}
|
||||
|
||||
// Format converts transcription result to JSON format
|
||||
func (f *JSONFormatter) Format(result *whisper.TranscriptionResult) (string, error) {
|
||||
data, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
49
pkg/output/srt.go
Normal file
49
pkg/output/srt.go
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
41
pkg/output/text.go
Normal file
41
pkg/output/text.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user