src/subs/subs.go (view raw)
1package subs
2
3import (
4 "encoding/xml"
5 "fmt"
6 "log"
7 "net/http"
8)
9
10type TimedText struct {
11 XMLName xml.Name `xml:"timedtext"`
12 Body Body `xml:"body"`
13}
14
15type Body struct {
16 Paragraphs []Paragraph `xml:"p"`
17}
18
19type Paragraph struct {
20 Text string `xml:",chardata"`
21 Start int `xml:"t,attr"` // Start time in milliseconds
22 Length int `xml:"d,attr"` // Duration in milliseconds
23}
24
25// Convert milliseconds to WebVTT timestamp format: HH:MM:SS.mmm
26func millisecondsToTimestamp(ms int) string {
27 seconds := ms / 1000
28 milliseconds := ms % 1000
29 return fmt.Sprintf("%02d:%02d:%02d.%03d", seconds/3600, (seconds%3600)/60, seconds%60, milliseconds)
30}
31
32func writeString(s string, w http.ResponseWriter) {
33 w.Write([]byte(s))
34}
35
36func Parse(inputData []byte, w http.ResponseWriter) error {
37 var timedText TimedText
38 err := xml.Unmarshal(inputData, &timedText)
39 if err != nil {
40 log.Println("Error unmarshalling XML:", err)
41 return err
42 }
43
44 // Write the WebVTT header
45 writeString("WEBVTT\n\n", w)
46
47 // Loop through the paragraphs and write them to the WebVTT file
48 for i, p := range timedText.Body.Paragraphs {
49 startTime := millisecondsToTimestamp(p.Start)
50 endTime := millisecondsToTimestamp(p.Start + p.Length)
51
52 s := fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, startTime, endTime, p.Text)
53 writeString(s, w)
54 }
55
56 return nil
57}