-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaption.go
69 lines (58 loc) · 1.39 KB
/
caption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package caps
import (
"fmt"
"math"
"strings"
)
type Caption struct {
Start *float64
End *float64
Nodes []CaptionContent
Style StyleProps
}
func (c Caption) IsEmpty() bool {
return len(c.Nodes) == 0
}
func (c Caption) Text() string {
var content strings.Builder
for _, node := range c.Nodes {
if !node.Style() {
content.WriteString(node.Content())
}
}
return content.String()
}
func (c Caption) String() string {
return fmt.Sprintf("%s --> %s\n%s", c.FormatStart(), c.FormatEnd(), c.Text())
}
func (c Caption) FormatStartWithSeparator(sep string) string {
return formatTimestamp(c.Start, sep)
}
func (c Caption) FormatStart() string {
return formatTimestamp(c.Start, ".")
}
func (c Caption) FormatEndWithSeparator(sep string) string {
return formatTimestamp(c.End, sep)
}
func (c Caption) FormatEnd() string {
return formatTimestamp(c.End, ".")
}
func formatTimestamp(timestamp *float64, sep string) string {
value := int(*timestamp / 1000)
seconds := math.Mod(float64(value)/1000, 60)
minutes := (value / (1000 * 60)) % 60
hours := value / (1000 * 60 * 60) % 24
resultTimestamp := fmt.Sprintf("%02d:%02d:%06.3f", hours, minutes, seconds)
if sep != "." {
return strings.ReplaceAll(resultTimestamp, ".", sep)
}
return resultTimestamp
}
func NewCaption(start, end *float64, nodes []CaptionContent, style StyleProps) Caption {
return Caption{
start,
end,
nodes,
style,
}
}