mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
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:
@@ -1,41 +1,37 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"fmt"
|
||||||
"time"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// from stdlib's time.go
|
||||||
|
func norm(hi, lo, base int) (nhi, nlo int) {
|
||||||
|
if lo < 0 {
|
||||||
|
n := (-lo-1)/base + 1
|
||||||
|
hi -= n
|
||||||
|
lo += n * base
|
||||||
|
}
|
||||||
|
if lo >= base {
|
||||||
|
n := lo / base
|
||||||
|
hi += n
|
||||||
|
lo -= n * base
|
||||||
|
}
|
||||||
|
return hi, lo
|
||||||
|
}
|
||||||
|
|
||||||
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
|
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
|
||||||
func GetVTTTime(totalSeconds float64) (s string) {
|
func GetVTTTime(fracSeconds float64) string {
|
||||||
totalSecondsString := strconv.FormatFloat(totalSeconds, 'f', -1, 64)
|
if fracSeconds < 0 || math.IsNaN(fracSeconds) || math.IsInf(fracSeconds, 0) {
|
||||||
secondsDuration, _ := time.ParseDuration(totalSecondsString + "s")
|
return "00:00:00.000"
|
||||||
|
|
||||||
// Hours
|
|
||||||
var hours = int(secondsDuration / time.Hour)
|
|
||||||
var n = secondsDuration % time.Hour
|
|
||||||
if hours < 10 {
|
|
||||||
s += "0"
|
|
||||||
}
|
}
|
||||||
s += strconv.Itoa(hours) + ":"
|
|
||||||
|
|
||||||
// Minutes
|
var msec, sec, min, hour int
|
||||||
var minutes = int(n / time.Minute)
|
msec = int(fracSeconds * 1000)
|
||||||
n = secondsDuration % time.Minute
|
sec, msec = norm(sec, msec, 1000)
|
||||||
if minutes < 10 {
|
min, sec = norm(min, sec, 60)
|
||||||
s += "0"
|
hour, min = norm(hour, min, 60)
|
||||||
}
|
|
||||||
s += strconv.Itoa(minutes) + ":"
|
return fmt.Sprintf("%02d:%02d:%02d.%03d", hour, min, sec, msec)
|
||||||
|
|
||||||
// Seconds
|
|
||||||
var seconds = int(n / time.Second)
|
|
||||||
n = secondsDuration % time.Second
|
|
||||||
if seconds < 10 {
|
|
||||||
s += "0"
|
|
||||||
}
|
|
||||||
s += strconv.Itoa(seconds)
|
|
||||||
|
|
||||||
// videojs requires milliseconds
|
|
||||||
s += ".000"
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
42
pkg/utils/vtt_test.go
Normal file
42
pkg/utils/vtt_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user