* docs(markers): design + implementation plans for multi-source markers & TheIntroDB contribution * fix(markers): TheIntroDB read-path correctness (TVDB, real confidence, best candidate) Honor TVDB ids in /media lookups (previously dropped — anime/TheTVDB-first libraries got no markers), decode and use the real per-segment confidence and submission_count instead of a hardcoded 0.9, and pick the most-submitted / highest-confidence candidate when several are returned. Adds httptest coverage for the introdb client and provider. Phase 1 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): multi-source dispatch, per-provider config, per-segment provenance Add marker_provider_config (per-provider fetch enable/priority + contribute gates, contribution off by default) and a cached ProviderConfigStore. Add Registry.FetchMerged: query all fetch-enabled providers concurrently and keep the best candidate per segment (submission_count, then confidence, then fetch priority), stamping each winning marker with its provider/algorithm. Thread per-segment provenance through MarkerUpdatePayload and scanner.MarkerUpdate (additive SegmentProvenance overrides) so a merged result writes correct per-segment provider/confidence/algorithm; the legacy shared columns keep a summary. The lazy-playback path now uses FetchMerged. With only TheIntroDB enabled, behavior is unchanged. Phase 2 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): TheIntroDB submission client, contribution audit, service engine Add a markers.Submitter capability and implement it on the introdb provider (POST /v3/submit, GET /v3/user/stats; key required, usage-limit aware, applies the null start/end conventions). Add the marker_contributions audit table and a value-hash-keyed ContributionStore for idempotency. Add ContributionService: resolves enabled submitter providers, gates eligibility (never re-submit online-sourced markers; auto runs require contribute_auto_local + scanner-intro above the per-provider confidence threshold), checks idempotency, submits, and records. Wired in main.go; no trigger yet (admin API and task follow). Phase 3 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(api): admin marker editing, contribution, and provider config endpoints Add the RequireAdmin marker API: GET/PUT /admin/files/{id}/markers (read with provenance; manual upsert where a segment object sets and null clears), DELETE .../markers/{segment}, POST .../contribute and GET .../contributions, plus GET/PUT /admin/markers/providers[/{provider}] and a .../validate key-check returning user stats. Manual writes go through the priority-gated UpsertMarkers (source=manual) and notify live sessions; a new FileRepository.ClearMarkers nulls a segment's columns. Validation mirrors the contribution rules. Phase 4 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): daily auto-contribution task for local intro markers Add ContributeMarkersTask (daily 04:00, after local detection): when a provider has contribute_enabled + contribute_auto_local, page through episode files with a scanner intro marker at/above the provider's confidence threshold (new ContributionStore.CandidateLocalIntroFiles keyset query) and run them through ContributionService with Auto=true. No-op when no provider opts in; idempotent and resumable across runs. Phase 5 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(intromarkers): refine chromaprint starts with dialogue cues * feat(markers): finish marker management backend * feat(web): add marker editing UI * feat(markers): use plugin marker providers * fix(markers): address PR review feedback * feat(player): show marker labels on seek hover * fix(markers): type nullable marker mutation params * feat(markers): audit marker edits and add permission --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
168 lines
4.2 KiB
Go
168 lines
4.2 KiB
Go
package subtitles
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// SubtitleCue is one timed subtitle entry. Times are absolute media positions.
|
|
type SubtitleCue struct {
|
|
Start time.Duration
|
|
End time.Duration
|
|
Lines []string
|
|
}
|
|
|
|
// ParseCues parses SRT or WebVTT subtitle bytes into cues. It tolerates both
|
|
// comma (SRT) and period (VTT) millisecond separators, a leading WEBVTT header,
|
|
// optional cue identifiers, VTT cue settings after the end timestamp, a UTF-8
|
|
// BOM, and CRLF line endings. Malformed individual cues are skipped rather than
|
|
// failing the whole document.
|
|
func ParseCues(data []byte) ([]SubtitleCue, error) {
|
|
text := string(data)
|
|
text = strings.TrimPrefix(text, "\ufeff")
|
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
|
|
|
var cues []SubtitleCue
|
|
for _, block := range strings.Split(text, "\n\n") {
|
|
block = strings.Trim(block, "\n")
|
|
if strings.TrimSpace(block) == "" {
|
|
continue
|
|
}
|
|
lines := strings.Split(block, "\n")
|
|
|
|
timingIdx := -1
|
|
for i, line := range lines {
|
|
if strings.Contains(line, "-->") {
|
|
timingIdx = i
|
|
break
|
|
}
|
|
}
|
|
if timingIdx < 0 {
|
|
continue
|
|
}
|
|
|
|
start, end, err := parseTimingLine(lines[timingIdx])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
textLines := append([]string(nil), lines[timingIdx+1:]...)
|
|
for len(textLines) > 0 && strings.TrimSpace(textLines[len(textLines)-1]) == "" {
|
|
textLines = textLines[:len(textLines)-1]
|
|
}
|
|
if len(textLines) == 0 {
|
|
continue
|
|
}
|
|
cues = append(cues, SubtitleCue{Start: start, End: end, Lines: textLines})
|
|
}
|
|
|
|
if len(cues) == 0 {
|
|
return nil, fmt.Errorf("no subtitle cues found")
|
|
}
|
|
return cues, nil
|
|
}
|
|
|
|
// SerializeSRT renders cues as a well-formed SRT document. Output always uses
|
|
// comma millisecond separators; the serving layer converts SRT to VTT on read.
|
|
func SerializeSRT(cues []SubtitleCue) []byte {
|
|
var buf bytes.Buffer
|
|
for i, cue := range cues {
|
|
fmt.Fprintf(&buf, "%d\n%s --> %s\n", i+1, formatSRTTimestamp(cue.Start), formatSRTTimestamp(cue.End))
|
|
for _, line := range cue.Lines {
|
|
buf.WriteString(line)
|
|
buf.WriteByte('\n')
|
|
}
|
|
buf.WriteByte('\n')
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func parseTimingLine(line string) (time.Duration, time.Duration, error) {
|
|
parts := strings.SplitN(line, "-->", 2)
|
|
if len(parts) != 2 {
|
|
return 0, 0, fmt.Errorf("invalid timing line")
|
|
}
|
|
start, err := parseTimestamp(strings.TrimSpace(parts[0]))
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
endField := strings.TrimSpace(parts[1])
|
|
if sp := strings.IndexAny(endField, " \t"); sp >= 0 {
|
|
endField = endField[:sp]
|
|
}
|
|
end, err := parseTimestamp(endField)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
if start < 0 || end < 0 || end < start {
|
|
return 0, 0, fmt.Errorf("invalid cue range")
|
|
}
|
|
return start, end, nil
|
|
}
|
|
|
|
// parseTimestamp accepts HH:MM:SS,mmm / HH:MM:SS.mmm / MM:SS.mmm forms.
|
|
func parseTimestamp(s string) (time.Duration, error) {
|
|
s = strings.TrimSpace(strings.Replace(s, ",", ".", 1))
|
|
colon := strings.Split(s, ":")
|
|
if len(colon) < 2 || len(colon) > 3 {
|
|
return 0, fmt.Errorf("invalid timestamp %q", s)
|
|
}
|
|
|
|
var hours int
|
|
idx := 0
|
|
if len(colon) == 3 {
|
|
h, err := strconv.Atoi(colon[0])
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid hours in %q", s)
|
|
}
|
|
hours = h
|
|
idx = 1
|
|
}
|
|
|
|
minutes, err := strconv.Atoi(colon[idx])
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid minutes in %q", s)
|
|
}
|
|
|
|
secFrac := strings.SplitN(colon[idx+1], ".", 2)
|
|
seconds, err := strconv.Atoi(secFrac[0])
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid seconds in %q", s)
|
|
}
|
|
|
|
millis := 0
|
|
if len(secFrac) == 2 {
|
|
frac := secFrac[1]
|
|
for len(frac) < 3 {
|
|
frac += "0"
|
|
}
|
|
millis, err = strconv.Atoi(frac[:3])
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid milliseconds in %q", s)
|
|
}
|
|
}
|
|
|
|
return time.Duration(hours)*time.Hour +
|
|
time.Duration(minutes)*time.Minute +
|
|
time.Duration(seconds)*time.Second +
|
|
time.Duration(millis)*time.Millisecond, nil
|
|
}
|
|
|
|
func formatSRTTimestamp(d time.Duration) string {
|
|
if d < 0 {
|
|
d = 0
|
|
}
|
|
totalMS := int64(d / time.Millisecond)
|
|
ms := totalMS % 1000
|
|
totalSec := totalMS / 1000
|
|
seconds := totalSec % 60
|
|
totalMin := totalSec / 60
|
|
minutes := totalMin % 60
|
|
hours := totalMin / 60
|
|
return fmt.Sprintf("%02d:%02d:%02d,%03d", hours, minutes, seconds, ms)
|
|
}
|