Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for more lenient duration parsing #124

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
Loading