Skip to content

Commit 9228f87

Browse files
committed
refactor and export api
1 parent 77513ff commit 9228f87

20 files changed

+265
-144
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Martin Lindhe
3+
Copyright (c) 2015-2017 Martin Lindhe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,31 @@ WARNING: The API is unstable, work in progress!
1515
go get -u github.com/martinlindhe/subtitles
1616
```
1717

18+
# Example - convert srt to vtt
1819

19-
# Example
20+
```go
21+
in := "1\n" +
22+
"00:00:04,630 --> 00:00:06,018\n" +
23+
"Go ninja!\n" +
24+
"\n" +
25+
"1\n" +
26+
"00:01:09,630 --> 00:01:11,005\n" +
27+
"No ninja!\n"
28+
29+
res, _ := NewFromSRT(in)
30+
31+
// Output: WEBVTT
32+
//
33+
// 00:00:04.630 --> 00:00:06.018
34+
// Go ninja!
35+
//
36+
// 00:01:09.630 --> 00:01:11.005
37+
// No ninja!
38+
fmt.Println(res.AsVTT())
39+
```
40+
41+
# Example - download subtitle from thesubdb.com
2042

21-
Fetch subtitle from thesubdb.com:
2243
```go
2344
f, _ := os.Open(fileName)
2445
defer f.Close()

caption.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package subtitles
22

33
import (
4-
"fmt"
54
"time"
65
)
76

@@ -12,13 +11,3 @@ type Caption struct {
1211
End time.Time
1312
Text []string
1413
}
15-
16-
// AsSrt renders the caption as srt
17-
func (cap Caption) AsSrt() string {
18-
res := fmt.Sprintf("%d", cap.Seq) + eol +
19-
SrtTime(cap.Start) + " --> " + SrtTime(cap.End) + eol
20-
for _, line := range cap.Text {
21-
res += line + eol
22-
}
23-
return res + eol
24-
}

caption_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func TestRenderTime(t *testing.T) {
1818
assert.Equal(t, "1\n"+
1919
"18:40:22,110 --> 18:41:20,123\n"+
2020
"<i>Go ninja!</i>\n\n",
21-
cap.AsSrt())
21+
cap.AsSRT())
2222
}

cleaner.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package subtitles
22

33
import (
4-
"fmt"
54
"strings"
65
"time"
76

@@ -10,44 +9,45 @@ import (
109

1110
// CleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
1211
func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string, error) {
13-
var captions []Caption
12+
var subtitle Subtitle
13+
var err error
1414

1515
if looksLikeSrt(utf8) {
16-
captions = parseSrt(utf8)
16+
subtitle, err = NewFromSRT(utf8)
1717
} else {
1818
// falls back on .ssa decoding, for now
19-
captions = parseSsa(utf8)
19+
subtitle, err = NewFromSSA(utf8)
20+
}
21+
if err != nil {
22+
return "", err
2023
}
2124

2225
if !keepAds {
23-
captions = removeAds(captions)
26+
subtitle.removeAds()
2427
}
2528

2629
if sync != 0 {
27-
captions = resyncSubs(captions, sync)
30+
subtitle.resyncSubs(sync)
2831
}
2932

30-
captions = filterSubs(captions, filterName)
31-
out := renderSrt(captions)
33+
subtitle.filterSubs(filterName)
34+
out := subtitle.AsSRT()
3235

3336
return out, nil
3437
}
3538

36-
func resyncSubs(subs []Caption, sync int) []Caption {
37-
38-
// var res []caption
39-
fmt.Printf("resyncing with %d\n", sync)
40-
41-
for i := range subs {
42-
subs[i].Start = subs[i].Start.Add(time.Duration(sync) * time.Millisecond)
43-
subs[i].End = subs[i].End.Add(time.Duration(sync) * time.Millisecond)
39+
func (subtitle *Subtitle) resyncSubs(sync int) {
40+
// log.Printf("resyncing with %d\n", sync)
41+
for i := range subtitle.Captions {
42+
subtitle.Captions[i].Start = subtitle.Captions[i].Start.
43+
Add(time.Duration(sync) * time.Millisecond)
44+
subtitle.Captions[i].End = subtitle.Captions[i].End.
45+
Add(time.Duration(sync) * time.Millisecond)
4446
}
45-
46-
return subs
4747
}
4848

4949
// RemoveAds removes advertisement from the subtitles
50-
func removeAds(subs []Caption) (res []Caption) {
50+
func (subtitle *Subtitle) removeAds() *Subtitle {
5151
ads := []string{
5252
// english:
5353
"captions paid for by",
@@ -109,7 +109,8 @@ func removeAds(subs []Caption) (res []Caption) {
109109
}
110110

111111
seq := 1
112-
for orgSeq, sub := range subs {
112+
res := []Caption{}
113+
for orgSeq, sub := range subtitle.Captions {
113114

114115
isAd := false
115116

@@ -130,5 +131,6 @@ func removeAds(subs []Caption) (res []Caption) {
130131
seq++
131132
}
132133
}
133-
return
134+
subtitle.Captions = res
135+
return subtitle
134136
}

cleaner_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestRemoveAds(t *testing.T) {
1010

11-
in := []Caption{{
11+
in := Subtitle{[]Caption{{
1212
1,
1313
MakeTime(0, 0, 4, 630),
1414
MakeTime(0, 0, 6, 18),
@@ -23,9 +23,9 @@ func TestRemoveAds(t *testing.T) {
2323
MakeTime(0, 1, 9, 630),
2424
MakeTime(0, 1, 11, 005),
2525
[]string{"No ninja!"},
26-
}}
26+
}}}
2727

28-
expected := []Caption{{
28+
expected := Subtitle{[]Caption{{
2929
1,
3030
MakeTime(0, 0, 4, 630),
3131
MakeTime(0, 0, 6, 18),
@@ -35,7 +35,7 @@ func TestRemoveAds(t *testing.T) {
3535
MakeTime(0, 1, 9, 630),
3636
MakeTime(0, 1, 11, 005),
3737
[]string{"No ninja!"},
38-
}}
38+
}}}
3939

40-
assert.Equal(t, expected, removeAds(in))
40+
assert.Equal(t, &expected, in.removeAds())
4141
}

filter.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
)
66

77
// filterSubs pass the captions through a filter function
8-
func filterSubs(captions []Caption, filter string) []Caption {
8+
func (subtitle *Subtitle) filterSubs(filter string) {
99
if filter == "caps" {
10-
return filterCapitalization(captions)
10+
subtitle.filterCapitalization()
1111
}
1212
if filter == "html" {
13-
return filterHTML(captions)
13+
subtitle.filterHTML()
1414
}
1515
if filter != "none" {
1616
fmt.Printf("Unrecognized filter name: %s\n", filter)
1717
}
18-
return captions
1918
}

caps.go renamed to filter_caps.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,28 @@ import (
77
)
88

99
// filterCapitalization converts "ALL CAPS" text into "Initial letter capped"
10-
func filterCapitalization(captions []Caption) []Caption {
11-
12-
for _, cap := range captions {
10+
func (subtitle *Subtitle) filterCapitalization() *Subtitle {
11+
for _, cap := range subtitle.Captions {
1312
for i, line := range cap.Text {
1413

1514
clean := ucFirst(line)
16-
1715
if clean != cap.Text[i] {
1816
log.Printf("[caps] %s -> %s\n", cap.Text[i], clean)
19-
cap.Text[i] = clean
17+
cap.Text[i] = clean // XXX updated?!
2018
}
2119
}
2220
}
23-
24-
return captions
21+
return subtitle
2522
}
2623

2724
func ucFirst(s string) string {
28-
2925
res := ""
30-
3126
for i, c := range s {
3227
if i == 0 {
3328
res += strings.ToUpper(string(c))
3429
} else {
3530
res += strings.ToLower(string(c))
3631
}
3732
}
38-
3933
return res
4034
}

caps_test.go renamed to filter_caps_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ import (
88

99
func TestFilterCapitalization(t *testing.T) {
1010

11-
in := []Caption{{
11+
in := Subtitle{Captions: []Caption{{
1212
Seq: 1,
1313
Start: MakeTime(0, 0, 4, 630),
1414
End: MakeTime(0, 0, 6, 18),
1515
Text: []string{"GO NINJA!", "NINJA GO!"},
16-
}}
16+
}}}
1717

18-
expected := []Caption{{
18+
expected := Subtitle{[]Caption{{
1919
1,
2020
MakeTime(0, 0, 4, 630),
2121
MakeTime(0, 0, 6, 18),
2222
[]string{"Go ninja!", "Ninja go!"},
23-
}}
23+
}}}
2424

25-
assert.Equal(t, expected, filterCapitalization(in))
25+
assert.Equal(t, &expected, in.filterCapitalization())
2626
}

html.go renamed to filter_html.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
)
88

99
// filterHTML removes all html tags from captions
10-
func filterHTML(captions []Caption) []Caption {
11-
for _, cap := range captions {
10+
func (subtitle *Subtitle) filterHTML() *Subtitle {
11+
for _, cap := range subtitle.Captions {
1212
for i, line := range cap.Text {
1313
clean := sanitize.HTML(line)
1414
if clean != cap.Text[i] {
1515
log.Printf("[html] %s -> %s\n", cap.Text[i], clean)
16-
cap.Text[i] = clean
16+
cap.Text[i] = clean // XXX works?!
1717
}
1818
}
1919
}
20-
return captions
20+
return subtitle
2121
}

0 commit comments

Comments
 (0)