stash/pkg/utils/vtt.go

38 lines
719 B
Go
Raw Normal View History

2019-02-09 12:30:49 +00:00
package utils
import (
"fmt"
"math"
2019-02-09 12:30:49 +00:00
)
// 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
2019-02-09 12:30:49 +00:00
}
if lo >= base {
n := lo / base
hi += n
lo -= n * base
2019-02-09 12:30:49 +00:00
}
return hi, lo
}
2019-02-09 12:30:49 +00:00
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
func GetVTTTime(fracSeconds float64) string {
if fracSeconds < 0 || math.IsNaN(fracSeconds) || math.IsInf(fracSeconds, 0) {
return "00:00:00.000"
2019-02-09 12:30:49 +00:00
}
var msec, sec, mnt, hour int
msec = int(fracSeconds * 1000)
sec, msec = norm(sec, msec, 1000)
mnt, sec = norm(mnt, sec, 60)
hour, mnt = norm(hour, mnt, 60)
return fmt.Sprintf("%02d:%02d:%02d.%03d", hour, mnt, sec, msec)
2019-02-09 12:30:49 +00:00
}