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

Support parsing srt without sequence numbers. #93

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 11 additions & 9 deletions srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {

// Line contains time boundaries
if strings.Contains(line, srtTimeBoundariesSeparator) {
// Return the wrong number of rows
if len(s.Lines) == 0 {
err = fmt.Errorf("astisub: line %d: no lines", lineNum)
return
// Remove last item of previous subtitle since it should be the index.
// If the last line is empty then the item is missing an index.
var index string
if len(s.Lines) != 0 {
index := s.Lines[len(s.Lines)-1].String()
if index != "" {
s.Lines = s.Lines[:len(s.Lines)-1]
}
}

// Remove last item of previous subtitle since it's the index
index := s.Lines[len(s.Lines)-1]
s.Lines = s.Lines[:len(s.Lines)-1]

// Remove trailing empty lines
if len(s.Lines) > 0 {
for i := len(s.Lines) - 1; i >= 0; i-- {
Expand All @@ -84,7 +84,9 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
s = &Item{}

// Fetch Index
s.Index, _ = strconv.Atoi(index.String())
if index != "" {
s.Index, _ = strconv.Atoi(index)
}

// Extract time boundaries
s1 := strings.Split(line, srtTimeBoundariesSeparator)
Expand Down
19 changes: 19 additions & 0 deletions srt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ func TestSRT(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}

func TestSRTMissingSequence(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/missing-sequence-in.srt")
assert.NoError(t, err)
assertSubtitleItems(t, s)

// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToSRT(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())

// Write
c, err := ioutil.ReadFile("./testdata/missing-sequence-out.srt")
samuel marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
err = s.WriteToSRT(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
20 changes: 20 additions & 0 deletions testdata/missing-sequence-in.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
00:01:39 --> 00:01:41,04
(deep rumbling)

00:02:04,08 --> 00:02:07,12 X1:40 X2:600 Y1:20 Y2:50
MAN:
How did we end up here?

00:02:12.16 --> 00:02:15.20
This place is horrible.

00:02:20.24 --> 00:02:22.28
Smells like balls.

00:02:28,32 --> 00:02:31,36
We don't belong
in this shithole.

00:02:31,40 --> 00:02:33,44
(computer playing
electronic melody)
26 changes: 26 additions & 0 deletions testdata/missing-sequence-out.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
1
00:01:39,000 --> 00:01:41,040
(deep rumbling)

2
00:02:04,080 --> 00:02:07,120
MAN:
How did we end up here?

3
00:02:12,160 --> 00:02:15,200
This place is horrible.

4
00:02:20,240 --> 00:02:22,280
Smells like balls.

5
00:02:28,320 --> 00:02:31,360
We don't belong
in this shithole.

6
00:02:31,400 --> 00:02:33,440
(computer playing
electronic melody)