Skip to content

Commit

Permalink
export Caption
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed May 9, 2017
1 parent 01b0599 commit da526ac
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 74 deletions.
2 changes: 1 addition & 1 deletion caps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// filterCapitalization converts "ALL CAPS" text into "Initial letter capped"
func filterCapitalization(captions []caption) []caption {
func filterCapitalization(captions []Caption) []Caption {

for _, cap := range captions {
for i, line := range cap.Text {
Expand Down
4 changes: 2 additions & 2 deletions caps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

func TestFilterCapitalization(t *testing.T) {

var in = []caption{{
in := []Caption{{
Seq: 1,
Start: makeTime(0, 0, 4, 630),
End: makeTime(0, 0, 6, 18),
Text: []string{"GO NINJA!", "NINJA GO!"},
}}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand Down
19 changes: 16 additions & 3 deletions caption.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package subtitles

import "time"
import (
"fmt"
"time"
)

// caption represents one subtitle block
type caption struct {
// Caption represents one subtitle block
type Caption struct {
Seq int
Start time.Time
End time.Time
Text []string
}

// AsSrt renders the caption as srt
func (cap Caption) AsSrt() string {
res := fmt.Sprintf("%d", cap.Seq) + eol +
SrtTime(cap.Start) + " --> " + SrtTime(cap.End) + eol
for _, line := range cap.Text {
res += line + eol
}
return res + eol
}
7 changes: 5 additions & 2 deletions caption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (

func TestRenderTime(t *testing.T) {

cap := caption{
cap := Caption{
1,
makeTime(18, 40, 22, 110),
makeTime(18, 41, 20, 123),
[]string{"<i>Go ninja!</i>"},
}

assert.Equal(t, "18:40:22,110 --> 18:41:20,123", cap.srtTime())
assert.Equal(t, "1\n"+
"18:40:22,110 --> 18:41:20,123\n"+
"<i>Go ninja!</i>\n\n",
cap.AsSrt())
}
15 changes: 4 additions & 11 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

// CleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string, error) {

var captions []caption
var captions []Caption

if looksLikeSrt(utf8) {
captions = parseSrt(utf8)
Expand All @@ -29,13 +28,12 @@ func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string,
}

captions = filterSubs(captions, filterName)

out := renderSrt(captions)

return out, nil
}

func resyncSubs(subs []caption, sync int) []caption {
func resyncSubs(subs []Caption, sync int) []Caption {

// var res []caption
fmt.Printf("resyncing with %d\n", sync)
Expand All @@ -49,12 +47,8 @@ func resyncSubs(subs []caption, sync int) []caption {
}

// RemoveAds removes advertisement from the subtitles
func removeAds(subs []caption) []caption {

var res []caption

func removeAds(subs []Caption) (res []Caption) {
ads := []string{

// english:
"captions paid for by",
"english subtitles",
Expand Down Expand Up @@ -136,6 +130,5 @@ func removeAds(subs []caption) []caption {
seq++
}
}

return res
return
}
4 changes: 2 additions & 2 deletions cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestRemoveAds(t *testing.T) {

var in = []caption{{
in := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand All @@ -25,7 +25,7 @@ func TestRemoveAds(t *testing.T) {
[]string{"No ninja!"},
}}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand Down
4 changes: 1 addition & 3 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
)

// filterSubs pass the captions through a filter function
func filterSubs(captions []caption, filter string) []caption {

func filterSubs(captions []Caption, filter string) []Caption {
if filter == "caps" {
return filterCapitalization(captions)
}
Expand All @@ -16,6 +15,5 @@ func filterSubs(captions []caption, filter string) []caption {
if filter != "none" {
fmt.Printf("Unrecognized filter name: %s\n", filter)
}

return captions
}
5 changes: 1 addition & 4 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import (
)

// filterHTML removes all html tags from captions
func filterHTML(captions []caption) []caption {

func filterHTML(captions []Caption) []Caption {
for _, cap := range captions {
for i, line := range cap.Text {
clean := sanitize.HTML(line)

if clean != cap.Text[i] {
log.Printf("[html] %s -> %s\n", cap.Text[i], clean)
cap.Text[i] = clean
}
}
}

return captions
}
4 changes: 2 additions & 2 deletions html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

func TestFilterHTML(t *testing.T) {

var in = []caption{{
in := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"<b>GO NINJA!</b>", "NINJA&nbsp;GO!"},
}}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package subtitles

// parse tries to parse a subtitle from the data stream
func parse(b []byte) []caption {
func parse(b []byte) []Caption {

s := convertToUTF8(b)

Expand Down
28 changes: 7 additions & 21 deletions srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func looksLikeSrt(s string) bool {
}

// ParseSrt parses a .srt text into []Caption, assumes s is a clean utf8 string
func parseSrt(s string) (res []caption) {
func parseSrt(s string) (res []Caption) {
r1 := regexp.MustCompile("([0-9:.,]*) --> ([0-9:.,]*)")
lines := strings.Split(s, "\n")
outSeq := 1
Expand All @@ -37,7 +37,7 @@ func parseSrt(s string) (res []caption) {
continue
}

var o caption
var o Caption
o.Seq = outSeq

i++
Expand Down Expand Up @@ -130,35 +130,21 @@ func ParseTime(in string) (time.Time, error) {
}

// writeSrt prints a srt render to outFileName
func writeSrt(subs []caption, outFileName string) error {
func writeSrt(subs []Caption, outFileName string) error {
text := renderSrt(subs)
return ioutil.WriteFile(outFileName, []byte(text), 0644)
}

// renderSrt produces a text representation of the subtitles
func renderSrt(subs []caption) (res string) {
func renderSrt(subs []Caption) (res string) {
for _, sub := range subs {
res += renderCaptionAsSrt(sub)
res += sub.AsSrt()
}
return
}

func renderCaptionAsSrt(cap caption) string {
res := fmt.Sprintf("%d", cap.Seq) + eol +
cap.srtTime() + eol

for _, line := range cap.Text {
res += line + eol
}

return res + eol
}

func renderSrtTime(t time.Time) string {
// SrtTime renders a timestamp for use in .srt
func SrtTime(t time.Time) string {
res := t.Format("15:04:05.000")
return strings.Replace(res, ".", ",", 1)
}

func (cap caption) srtTime() string {
return renderSrtTime(cap.Start) + " --> " + renderSrtTime(cap.End)
}
22 changes: 11 additions & 11 deletions srt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestParseSrt(t *testing.T) {
"00:01:09,630 --> 00:01:11,005\n" +
"No ninja!\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestParseSrtWithMacLinebreaks(t *testing.T) {
"00:01:09,630 --> 00:01:11,005\r" +
"No ninja!\r"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestParseSrtSkipEmpty(t *testing.T) {
"00:01:09,630 --> 00:01:11,005\n" +
"No ninja!\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand All @@ -123,7 +123,7 @@ func TestParseSrtCrlf(t *testing.T) {
"Go ninja!\r\n" +
"\r\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand All @@ -143,7 +143,7 @@ func TestParseExtraLineBreak(t *testing.T) {
"Go ninja!\r\n" +
"\r\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand All @@ -159,7 +159,7 @@ func TestParseWierdTimestamp(t *testing.T) {
"00:14:52.00 --> 00:14:57,500\r\n" +
"Go ninja!\r\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 14, 52, 0),
makeTime(0, 14, 57, 500),
Expand All @@ -179,7 +179,7 @@ func TestRenderSrt(t *testing.T) {
"00:01:09,630 --> 00:01:11,005\n" +
"No ninja!\n\n"

var in = []caption{{
in := []Caption{{
1,
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
Expand All @@ -199,7 +199,7 @@ func TestParseLatin1Srt(t *testing.T) {
"00:14:52.00 --> 00:14:57,500\r\n" +
"Hall\xe5 ninja!\r\n"

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 14, 52, 0),
makeTime(0, 14, 57, 500),
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestParseUTF16BESrt(t *testing.T) {
0, '\r', 0, '\n',
}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 0, 0),
makeTime(0, 0, 0, 1),
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestParseUTF16LESrt(t *testing.T) {
'\r', 0, '\n', 0,
}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 0, 0),
makeTime(0, 0, 0, 1),
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestParseUTF8BomSrt(t *testing.T) {
'\r', '\n',
}

var expected = []caption{{
expected := []Caption{{
1,
makeTime(0, 0, 0, 0),
makeTime(0, 0, 0, 1),
Expand Down
Loading

0 comments on commit da526ac

Please sign in to comment.