utils: vtt: test + rewrite (#1284)

* utils: vtt: add tests

In lieu of documentation.

* utils: vtt: rewrite for correctness and simplicity

Now handles fractional seconds and negative values correctly.
This commit is contained in:
stashist
2021-05-25 03:25:26 +02:00
committed by GitHub
parent 3d93f7f0fe
commit fc9d70f702
2 changed files with 71 additions and 33 deletions

View File

@@ -1,41 +1,37 @@
package utils package utils
import ( import (
"strconv" "fmt"
"time" "math"
) )
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm) // from stdlib's time.go
func GetVTTTime(totalSeconds float64) (s string) { func norm(hi, lo, base int) (nhi, nlo int) {
totalSecondsString := strconv.FormatFloat(totalSeconds, 'f', -1, 64) if lo < 0 {
secondsDuration, _ := time.ParseDuration(totalSecondsString + "s") n := (-lo-1)/base + 1
hi -= n
// Hours lo += n * base
var hours = int(secondsDuration / time.Hour)
var n = secondsDuration % time.Hour
if hours < 10 {
s += "0"
} }
s += strconv.Itoa(hours) + ":" if lo >= base {
n := lo / base
// Minutes hi += n
var minutes = int(n / time.Minute) lo -= n * base
n = secondsDuration % time.Minute
if minutes < 10 {
s += "0"
} }
s += strconv.Itoa(minutes) + ":" return hi, lo
}
// Seconds
var seconds = int(n / time.Second) // GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
n = secondsDuration % time.Second func GetVTTTime(fracSeconds float64) string {
if seconds < 10 { if fracSeconds < 0 || math.IsNaN(fracSeconds) || math.IsInf(fracSeconds, 0) {
s += "0" return "00:00:00.000"
} }
s += strconv.Itoa(seconds)
var msec, sec, min, hour int
// videojs requires milliseconds msec = int(fracSeconds * 1000)
s += ".000" sec, msec = norm(sec, msec, 1000)
min, sec = norm(min, sec, 60)
return hour, min = norm(hour, min, 60)
return fmt.Sprintf("%02d:%02d:%02d.%03d", hour, min, sec, msec)
} }

42
pkg/utils/vtt_test.go Normal file
View File

@@ -0,0 +1,42 @@
package utils
import (
"math"
"testing"
)
func TestZeroTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(0); want != got {
t.Errorf("TestZeroTimestamp: GetVTTTime(0) = %v; want %v", got, want)
}
}
func TestValidTimestamp(t *testing.T) {
s := 0.1
if want, got := "00:00:00.100", GetVTTTime(s); want != got {
t.Errorf("TestValidTimestamp: GetVTTTime(%v) = %v; want %v", s, got, want)
}
s = ((24+1)*60+1)*60 + 1 + 0.1
if want, got := "25:01:01.100", GetVTTTime(s); want != got {
t.Errorf("TestValidTimestamp: GetVTTTime(%v) = %v; want %v", s, got, want)
}
}
// Negative timestamps are not defined by WebVTT.
func TestNegativeTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(-1); want != got {
t.Errorf("TestNegativeTimestamp: GetVTTTime(-1) = %v; want %v", got, want)
}
}
func TestInvalidTimestamp(t *testing.T) {
if want, got := "00:00:00.000", GetVTTTime(math.NaN()); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(NaN) = %v; want %v", got, want)
}
if want, got := "00:00:00.000", GetVTTTime(math.Inf(1)); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(Inf) = %v; want %v", got, want)
}
if want, got := "00:00:00.000", GetVTTTime(math.Inf(-1)); want != got {
t.Errorf("TestInvalidTimestamp: GetVTTTime(-Inf) = %v; want %v", got, want)
}
}