icza / srtgears

Subtitle engine for reading, manipulating / transforming and saving subtitle files.
https://srt-gears.appspot.com/
Other
5 stars 1 forks source link

RemoveHI issue #1

Open adam42389 opened 1 year ago

adam42389 commented 1 year ago

Hi, Thanks for this package! It does exactly what I needed.

Running it on 1700 files it panicked once on Subtitle.RemoveHI() method:

Exception has occurred: panic
"runtime error: index out of range [0] with length 0"
Stack:
    2  0x0000000100413cd4 in github.com/icza/srtgears.(*Subtitle).RemoveHI
        at ./go/pkg/mod/github.com/icza/srtgears@v0.0.0-20220812134320-0c724b7bb537/subtitle.go:57
    3  0x0000000100413545 in github.com/icza/srtgears.(*SubsPack).RemoveHI
        at ./go/pkg/mod/github.com/icza/srtgears@v0.0.0-20220812134320-0c724b7bb537/subspack.go:68
    (truncated)

Subtitle that caused it:

185
00:27:39,128 --> 00:27:43,923
<he explains how trap works>

Fixed with length check:

func (s *Subtitle) RemoveHI() (remove bool) {
    // It may be just some (e.g. first) lines are hearing impaired.
    for i := len(s.Lines) - 1; i >= 0; i-- {
        line := s.Lines[i]
        // Check without HTML formatting to recognize and remove these:
        // "<i>[PHONE RINGING]</i>"
        line = htmlPattern.ReplaceAllString(line, "")

        // BEGIN FIX

        if len(line) == 0 {
            remove = true
            s.Lines = append(s.Lines[:i], s.Lines[i+1:]...)
            continue
        }

        // END FIX

        first, last := line[0], line[len(line)-1]
        if first == '[' && last == ']' || first == '(' && last == ')' {
            remove = true
            s.Lines = append(s.Lines[:i], s.Lines[i+1:]...)
        }
    }
    return
}

Also, in subrip.go line 219, should have comma before milliseconds:

wr.prf("%02d:%02d:%02d,%03d", hour, min, sec, ms)

Hope this helps. Thanks again.

icza commented 1 year ago

Hi, thanks for reporting these.

I went ahead and just checked line length before indexing, but did not remove that line. I'm not sure if that always denotes a HI line.