Skip to content

Commit c230a8e

Browse files
committed
rework internals
1 parent 9785470 commit c230a8e

File tree

10 files changed

+136
-113
lines changed

10 files changed

+136
-113
lines changed

caption/cleaner.go renamed to cleaner/cleaner.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
package caption
1+
package cleaner
22

33
import (
44
"fmt"
55
"strings"
6+
7+
"github.com/martinlindhe/subber/caption"
68
)
79

8-
// CleanSubs removes advertisement from the subtitles
9-
func CleanSubs(subs []Caption) []Caption {
10+
// RemoveAds removes advertisement from the subtitles
11+
func RemoveAds(subs []caption.Caption) []caption.Caption {
1012

11-
var res []Caption
13+
var res []caption.Caption
1214

1315
ads := []string{
1416

caption/cleaner_test.go renamed to cleaner/cleaner_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package caption
1+
package cleaner
22

33
import (
44
"testing"
55

6+
"github.com/martinlindhe/subber/caption"
67
"github.com/martinlindhe/subber/testExtras"
78
"github.com/stretchr/testify/assert"
89
)
910

1011
func TestCleanSub(t *testing.T) {
1112

12-
var in = []Caption{
13+
var in = []caption.Caption{
1314
{
1415
1,
1516
testExtras.MakeTime(0, 0, 4, 630),
@@ -30,9 +31,7 @@ func TestCleanSub(t *testing.T) {
3031
},
3132
}
3233

33-
cleaned := CleanSubs(in)
34-
35-
var expected = []Caption{
34+
var expected = []caption.Caption{
3635
{
3736
1,
3837
testExtras.MakeTime(0, 0, 4, 630),
@@ -47,5 +46,5 @@ func TestCleanSub(t *testing.T) {
4746
},
4847
}
4948

50-
assert.Equal(t, expected, cleaned)
49+
assert.Equal(t, expected, RemoveAds(in))
5150
}

download/thesubdb.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ import (
1313
)
1414

1515
// FindSub finds subtitle online, and parses it into a []caption.Caption
16-
func FindSub(videoFileName string, language string, keepAds bool) ([]caption.Caption, error) {
16+
func FindSub(videoFileName string, language string) ([]caption.Caption, error) {
1717

1818
text, err := FindSubText(videoFileName, language)
1919
if err != nil {
2020
return nil, err
2121
}
2222

23-
captions := srt.ParseSrt([]byte(text))
24-
25-
if !keepAds {
26-
captions = caption.CleanSubs(captions)
27-
}
23+
captions := srt.ParseSrt(string(text))
2824

2925
return captions, nil
3026
}

parser/parser.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package parser
2+
3+
import (
4+
"github.com/martinlindhe/subber/caption"
5+
"github.com/martinlindhe/subber/srt"
6+
"github.com/martinlindhe/subber/ssa"
7+
"github.com/martinlindhe/subber/txtformat"
8+
)
9+
10+
// Parse tries to parse a subtitle from the data stream
11+
func Parse(b []byte) []caption.Caption {
12+
13+
s := txtformat.ConvertToUTF8(b)
14+
15+
if s[0] == '[' {
16+
// looks like ssa
17+
return ssa.ParseSsa(s)
18+
}
19+
20+
// XXXX
21+
return srt.ParseSrt(s)
22+
}

srt/srt.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@ package srt
33
import (
44
"fmt"
55
"io/ioutil"
6-
"os"
76
"regexp"
87
"strconv"
98
"strings"
109
"time"
1110

1211
"github.com/martinlindhe/subber/caption"
13-
"github.com/martinlindhe/subber/filter"
14-
"github.com/martinlindhe/subber/txtformat"
1512
)
1613

1714
// Eol is the end of line characters to use when writing .srt data
1815
const Eol = "\n"
1916

20-
// ParseSrt parses a .srt text into []Caption
21-
func ParseSrt(b []byte) []caption.Caption {
22-
23-
s := txtformat.ConvertToUTF8(b)
17+
// ParseSrt parses a .srt text into []Caption, assumes s is a clean utf8 string
18+
func ParseSrt(s string) []caption.Caption {
2419

2520
var res []caption.Caption
2621

@@ -180,48 +175,3 @@ func renderCaptionAsSrt(caption caption.Caption) string {
180175

181176
return res + Eol
182177
}
183-
184-
// CleanupSrt performs cleanup on fileName, overwriting the original file
185-
func CleanupSrt(inFileName string, filterName string, skipBackup bool, keepAds bool) error {
186-
187-
fmt.Fprintf(os.Stderr, "CleanupSrt %s\n", inFileName)
188-
189-
data, err := ioutil.ReadFile(inFileName)
190-
if err != nil {
191-
return err
192-
}
193-
194-
captions := ParseSrt(data)
195-
if !keepAds {
196-
captions = caption.CleanSubs(captions)
197-
}
198-
199-
captions = filter.FilterSubs(captions, filterName)
200-
201-
out := RenderSrt(captions)
202-
203-
if string(data) == out {
204-
return nil
205-
}
206-
207-
if !skipBackup {
208-
backupFileName := inFileName + ".org"
209-
os.Rename(inFileName, backupFileName)
210-
// fmt.Printf("Backed up to %s\n", backupFileName)
211-
}
212-
213-
f, err := os.Create(inFileName)
214-
if err != nil {
215-
return err
216-
}
217-
218-
defer f.Close()
219-
220-
_, err = f.WriteString(out)
221-
if err != nil {
222-
return err
223-
}
224-
225-
//fmt.Printf("Written %d captions to %s\n", len(captions), inFileName)
226-
return nil
227-
}

srt/srt_test.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/martinlindhe/subber/caption"
77
"github.com/martinlindhe/subber/testExtras"
8+
"github.com/martinlindhe/subber/txtformat"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -27,18 +28,17 @@ func TestParseTime(t *testing.T) {
2728

2829
func TestParseSrt(t *testing.T) {
2930

30-
in := []byte(
31-
"1\n" +
32-
"00:00:04,630 --> 00:00:06,018\n" +
33-
"Go ninja!\n" +
34-
"\n" +
35-
"2\n" +
36-
"00:00:10,000 --> 00:00:11,000\n" +
37-
"Subtitles By MrCool\n" +
38-
"\n" +
39-
"3\n" +
40-
"00:01:09,630 --> 00:01:11,005\n" +
41-
"No ninja!\n")
31+
in := "1\n" +
32+
"00:00:04,630 --> 00:00:06,018\n" +
33+
"Go ninja!\n" +
34+
"\n" +
35+
"2\n" +
36+
"00:00:10,000 --> 00:00:11,000\n" +
37+
"Subtitles By MrCool\n" +
38+
"\n" +
39+
"3\n" +
40+
"00:01:09,630 --> 00:01:11,005\n" +
41+
"No ninja!\n"
4242

4343
var expected = []caption.Caption{
4444
{
@@ -66,14 +66,13 @@ func TestParseSrt(t *testing.T) {
6666

6767
func TestParseSrtWithMacLinebreaks(t *testing.T) {
6868

69-
in := []byte(
70-
"1\r" +
71-
"00:00:04,630 --> 00:00:06,018\r" +
72-
"Go ninja!\r" +
73-
"\r" +
74-
"3\r" +
75-
"00:01:09,630 --> 00:01:11,005\r" +
76-
"No ninja!\r")
69+
in := "1\r" +
70+
"00:00:04,630 --> 00:00:06,018\r" +
71+
"Go ninja!\r" +
72+
"\r" +
73+
"3\r" +
74+
"00:01:09,630 --> 00:01:11,005\r" +
75+
"No ninja!\r"
7776

7877
var expected = []caption.Caption{
7978
{
@@ -90,7 +89,9 @@ func TestParseSrtWithMacLinebreaks(t *testing.T) {
9089
},
9190
}
9291

93-
assert.Equal(t, expected, ParseSrt(in))
92+
utf8 := txtformat.ConvertToUTF8([]byte(in))
93+
94+
assert.Equal(t, expected, ParseSrt(utf8))
9495
}
9596

9697
func TestParseSrtSkipEmpty(t *testing.T) {
@@ -122,7 +123,7 @@ func TestParseSrtSkipEmpty(t *testing.T) {
122123
},
123124
}
124125

125-
assert.Equal(t, expected, ParseSrt([]byte(in)))
126+
assert.Equal(t, expected, ParseSrt(in))
126127
}
127128

128129
func TestParseSrtCrlf(t *testing.T) {
@@ -141,7 +142,7 @@ func TestParseSrtCrlf(t *testing.T) {
141142
},
142143
}
143144

144-
assert.Equal(t, expected, ParseSrt([]byte(in)))
145+
assert.Equal(t, expected, ParseSrt(in))
145146
}
146147

147148
func TestParseExtraLineBreak(t *testing.T) {
@@ -163,7 +164,7 @@ func TestParseExtraLineBreak(t *testing.T) {
163164
},
164165
}
165166

166-
assert.Equal(t, expected, ParseSrt([]byte(in)))
167+
assert.Equal(t, expected, ParseSrt(in))
167168
}
168169

169170
func TestParseWierdTimestamp(t *testing.T) {
@@ -181,7 +182,7 @@ func TestParseWierdTimestamp(t *testing.T) {
181182
},
182183
}
183184

184-
assert.Equal(t, expected, ParseSrt([]byte(in)))
185+
assert.Equal(t, expected, ParseSrt(in))
185186
}
186187

187188
func TestRenderSrt(t *testing.T) {
@@ -226,7 +227,9 @@ func TestParseLatin1Srt(t *testing.T) {
226227
},
227228
}
228229

229-
assert.Equal(t, expected, ParseSrt([]byte(in)))
230+
utf8 := txtformat.ConvertToUTF8([]byte(in))
231+
232+
assert.Equal(t, expected, ParseSrt(utf8))
230233
}
231234

232235
func TestParseUTF16BESrt(t *testing.T) {
@@ -262,7 +265,9 @@ func TestParseUTF16BESrt(t *testing.T) {
262265
},
263266
}
264267

265-
assert.Equal(t, expected, ParseSrt(in))
268+
utf8 := txtformat.ConvertToUTF8(in)
269+
270+
assert.Equal(t, expected, ParseSrt(utf8))
266271
}
267272

268273
func TestParseUTF16LESrt(t *testing.T) {
@@ -298,7 +303,9 @@ func TestParseUTF16LESrt(t *testing.T) {
298303
},
299304
}
300305

301-
assert.Equal(t, expected, ParseSrt(in))
306+
utf8 := txtformat.ConvertToUTF8(in)
307+
308+
assert.Equal(t, expected, ParseSrt(utf8))
302309
}
303310

304311
func TestParseUTF8BomSrt(t *testing.T) {
@@ -334,5 +341,7 @@ func TestParseUTF8BomSrt(t *testing.T) {
334341
},
335342
}
336343

337-
assert.Equal(t, expected, ParseSrt([]byte(in)))
344+
utf8 := txtformat.ConvertToUTF8(in)
345+
346+
assert.Equal(t, expected, ParseSrt(utf8))
338347
}

ssa/ssa.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import (
88
"time"
99

1010
"github.com/martinlindhe/subber/caption"
11-
"github.com/martinlindhe/subber/txtformat"
1211
)
1312

14-
func ParseSsa(b []byte) []caption.Caption {
15-
16-
s := txtformat.ConvertToUTF8(b)
13+
func ParseSsa(s string) []caption.Caption {
1714

1815
var res []caption.Caption
1916

ssa/ssa_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import (
1010

1111
func TestParseSsa(t *testing.T) {
1212

13-
in := []byte(
14-
"[Events]\n" +
15-
"Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text\n" +
16-
"Dialogue: 0,0:01:06.37,0:01:08.04,Default,,0000,0000,0000,,Honey, I'm home!\n" +
17-
"Dialogue: 0,0:01:09.05,0:01:10.69,Default,,0000,0000,0000,,Hi.\\n- Hi, love.\n")
13+
in := "[Events]\n" +
14+
"Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text\n" +
15+
"Dialogue: 0,0:01:06.37,0:01:08.04,Default,,0000,0000,0000,,Honey, I'm home!\n" +
16+
"Dialogue: 0,0:01:09.05,0:01:10.69,Default,,0000,0000,0000,,Hi.\\n- Hi, love.\n"
1817

1918
var expected = []caption.Caption{
2019
{

0 commit comments

Comments
 (0)