Skip to content

Commit 12d35c9

Browse files
committed
filter: merge added, which merges multiple captions with same timestamps into one
1 parent a60ebde commit 12d35c9

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

cmd/subber/subber.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
keepAds = kingpin.Flag("keep-ads", "Do not strip advertisement captions.").Bool()
2222
skipBackups = kingpin.Flag("skip-backups", "Do not make backup (.srt.org) of original .srt").Bool()
2323
language = kingpin.Flag("language", "Language.").Default("en").String()
24-
filterName = kingpin.Flag("filter", "Filter (none, caps, html, ocr, flip, all).").Default("none").String()
24+
filterName = kingpin.Flag("filter", "Filter (none, caps, html, ocr, merge, flip, all).").Default("none").String()
2525
sync = kingpin.Flag("sync", "Synchronize captions (milliseconds).").Int()
2626
)
2727

filter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func (subtitle *Subtitle) FilterCaptions(filter string) {
1717
subtitle.filterHTML()
1818
case "ocr":
1919
subtitle.filterOCR()
20+
case "merge":
21+
subtitle.filterMerge()
2022
case "flip":
2123
subtitle.filterFlip()
2224
case "none":

filter_merge.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package subtitles
2+
3+
// filterMerge combines all separate captions with same time spawn into one caption
4+
func (subtitle *Subtitle) filterMerge() *Subtitle {
5+
6+
idxToRemove := []int{}
7+
for i, cap := range subtitle.Captions {
8+
for j, pCap := range subtitle.Captions[0:i] {
9+
if pCap.Start == cap.Start && pCap.End == cap.End {
10+
subtitle.Captions[j].Text = append(pCap.Text, cap.Text...)
11+
idxToRemove = append(idxToRemove, i)
12+
break
13+
}
14+
}
15+
}
16+
17+
newCaptions := []Caption{}
18+
for i, cap := range subtitle.Captions {
19+
if !contains(idxToRemove, i) {
20+
newCaptions = append(newCaptions, cap)
21+
}
22+
}
23+
24+
subtitle.Captions = newCaptions
25+
26+
return subtitle
27+
}
28+
29+
func contains(s []int, e int) bool {
30+
for _, a := range s {
31+
if a == e {
32+
return true
33+
}
34+
}
35+
return false
36+
}

filter_merge_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package subtitles
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFilterMerge(t *testing.T) {
10+
in := `1
11+
00:01:28,800 --> 00:01:30,166
12+
I never thought
13+
14+
2
15+
00:01:28,800 --> 00:01:30,166
16+
I would be caught up
17+
18+
3
19+
00:01:30,266 --> 00:01:32,233
20+
in a story such as this,
21+
22+
4
23+
00:01:33,266 --> 00:01:35,867
24+
because I live on the
25+
26+
5
27+
00:01:33,266 --> 00:01:35,867
28+
other side of the world.
29+
`
30+
31+
sub, err := NewFromSRT(in)
32+
assert.Equal(t, nil, err)
33+
34+
sub = *sub.filterMerge()
35+
36+
expected := `1
37+
00:01:28,800 --> 00:01:30,166
38+
I never thought
39+
I would be caught up
40+
41+
3
42+
00:01:30,266 --> 00:01:32,233
43+
in a story such as this,
44+
45+
4
46+
00:01:33,266 --> 00:01:35,867
47+
because I live on the
48+
other side of the world.
49+
50+
`
51+
assert.Equal(t, expected, sub.AsSRT())
52+
53+
}

0 commit comments

Comments
 (0)