From 230d505377409e50635acb3877e06c52bbe5b3ca Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Mon, 6 Nov 2023 12:03:14 +0100 Subject: [PATCH] Fixed VFR pts end --- decoder/y4m/y4m.go | 13 ++++++++++--- utilities/timecodes.go | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/decoder/y4m/y4m.go b/decoder/y4m/y4m.go index 0764e91..fc20f48 100644 --- a/decoder/y4m/y4m.go +++ b/decoder/y4m/y4m.go @@ -10,6 +10,7 @@ import ( "io" "strconv" "strings" + "time" ) type Decoder struct { @@ -106,10 +107,16 @@ func (s *Decoder) IsVFR() bool { // FramePTS Returns -1 if not found func (s *Decoder) FramePTS(n int) int64 { if s.IsVFR() { - if n >= len(s.timecodes) { - return -1 + pts := s.timecodes.PTS(n) + if pts == -1 { + frameCount := len(s.timecodes) - 1 + last := s.timecodes[frameCount] + if frameCount == 0 { + last = 0 + } + pts = last + (s.timecodes.FallbackDuration() * time.Duration(n-frameCount+1)).Milliseconds() } - return s.timecodes[n] + return pts } return int64(n) } diff --git a/utilities/timecodes.go b/utilities/timecodes.go index 73672c4..b1c86c7 100644 --- a/utilities/timecodes.go +++ b/utilities/timecodes.go @@ -6,11 +6,22 @@ import ( "github.com/nethruster/go-fraction" "io" "strings" + "time" ) -// Timecodes Entries in milliseconds +// Timecodes Entries in milliseconds. First entry includes fallback duration, in time.Duration format type Timecodes []int64 +func (tc Timecodes) FallbackDuration() time.Duration { + return time.Duration(tc[0]) +} +func (tc Timecodes) PTS(n int) int64 { + if n >= 0 && (n+1) < len(tc) { + return tc[n+1] + } + return -1 +} + const timecodesv1Header = "# timecode format v1" func ParseTimecodesV1(reader io.Reader) (Timecodes, error) { @@ -61,6 +72,9 @@ func ParseTimecodesV1(reader io.Reader) (Timecodes, error) { var currentFrame int64 + //fallback + tc = append(tc, (int64(time.Second)*assumedFraction.Numerator())/assumedFraction.Denominator()) + for scanner.Scan() { line := strings.ReplaceAll(strings.TrimSpace(scanner.Text()), " ", "") if line == "" || line[0] == '#' {