Skip to content

Commit

Permalink
Allow for more lenient duration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-taylor committed Dec 17, 2024
1 parent 646078d commit 6eb61e8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
6 changes: 3 additions & 3 deletions srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

// Constants
const (
srtTimeBoundariesSeparator = " --> "
srtTimeBoundariesSeparator = "-->"
)

// Vars
var (
bytesSRTTimeBoundariesSeparator = []byte(srtTimeBoundariesSeparator)
bytesSRTTimeBoundariesSeparator = []byte(" "+srtTimeBoundariesSeparator+" ")
)

// parseDurationSRT parses an .srt duration
Expand Down Expand Up @@ -106,7 +106,7 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
return
}
// We do this to eliminate extra stuff like positions which are not documented anywhere
s2 := strings.Split(s1[1], " ")
s2 := strings.Fields(s1[1])

// Parse time boundaries
if s.StartAt, err = parseDurationSRT(s1[0]); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions srt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/asticode/go-astisub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSRT(t *testing.T) {
Expand Down Expand Up @@ -138,3 +140,18 @@ func TestSRTStyled(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}

func TestSRTParseDuration(t *testing.T) {
testData := `
1
00:00:01.876-->00:0:03.390
Duration without enclosing space`

s, err := astisub.ReadFromSRT(strings.NewReader(testData))
require.NoError(t, err)

require.Len(t, s.Items, 1)
assert.Equal(t, 1*time.Second+876*time.Millisecond, s.Items[0].StartAt)
assert.Equal(t, 3*time.Second+390*time.Millisecond, s.Items[0].EndAt)
assert.Equal(t, "Duration without enclosing space", s.Items[0].Lines[0].String())
}
6 changes: 3 additions & 3 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ const (
webvttBlockNameStyle = "style"
webvttBlockNameText = "text"
webvttDefaultStyleID = "astisub-webvtt-default-style-id"
webvttTimeBoundariesSeparator = " --> "
webvttTimeBoundariesSeparator = "-->"
webvttTimestampMapHeader = "X-TIMESTAMP-MAP"
)

// Vars
var (
bytesWebVTTItalicEndTag = []byte("</i>")
bytesWebVTTItalicStartTag = []byte("<i>")
bytesWebVTTTimeBoundariesSeparator = []byte(webvttTimeBoundariesSeparator)
bytesWebVTTTimeBoundariesSeparator = []byte(" "+webvttTimeBoundariesSeparator+" ")
webVTTRegexpInlineTimestamp = regexp.MustCompile(`<((?:\d{2,}:)?\d{2}:\d{2}\.\d{3})>`)
webVTTRegexpTag = regexp.MustCompile(`(</*\s*([^\.\s]+)(\.[^\s/]*)*\s*([^/]*)\s*/*>)`)
)
Expand Down Expand Up @@ -237,7 +237,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
var left = strings.Split(line, webvttTimeBoundariesSeparator)

// Split line on space to get remaining of time data
var right = strings.Split(left[1], " ")
var right = strings.Fields(left[1])

// Parse time boundaries
if item.StartAt, err = parseDurationWebVTT(left[0]); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions webvtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,27 @@ Text with a <00:06:30.000>timestamp in the middle
Do no fall to the next item
`, b.String())
}

func TestWebVTTParseDuration(t *testing.T) {
testData := `WEBVTT
1
00:00:01.876-->00:0:03.390
Duration without enclosing space
2
00:00:03.391-->00:00:06.567 align:middle
Duration with tab spaced styles`

s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
require.NoError(t, err)

require.Len(t, s.Items, 2)
assert.Equal(t, 1*time.Second+876*time.Millisecond, s.Items[0].StartAt)
assert.Equal(t, 3*time.Second+390*time.Millisecond, s.Items[0].EndAt)
assert.Equal(t, "Duration without enclosing space", s.Items[0].Lines[0].String())
assert.Equal(t, 3*time.Second+391*time.Millisecond, s.Items[1].StartAt)
assert.Equal(t, 6*time.Second+567*time.Millisecond, s.Items[1].EndAt)
assert.Equal(t, "Duration with tab spaced styles", s.Items[1].Lines[0].String())
assert.NotNil(t, s.Items[1].InlineStyle)
assert.Equal(t, s.Items[1].InlineStyle.WebVTTAlign, "middle")
}

0 comments on commit 6eb61e8

Please sign in to comment.