-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ccdb: handle obscure text-based format
- Loading branch information
1 parent
497af97
commit 724b4b5
Showing
14 changed files
with
164 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
install: | ||
go install -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package subtitles | ||
|
||
// an obscure (to me) text-based subtitle format, can have extension .cc or .txt | ||
// found in The Way We Live Now (2001) BBC TV 1.txt | ||
// STATUS: incomplete detection and support | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func looksLikeCCDBCapture(s string) bool { | ||
return strings.Contains(s, "[SUBTITLE]") | ||
} | ||
|
||
// NewFromCCDBCapture parses a ccdb capture text into []Caption, assumes s is a clean utf8 string | ||
func NewFromCCDBCapture(s string) (res Subtitle, err error) { | ||
rows := strings.Split(s, "\n") | ||
seq := 1 | ||
caption := Caption{Seq: seq} | ||
parseText := false | ||
for rowNum, row := range rows { | ||
if len(row) > 1 && row[0] == '[' { | ||
continue | ||
} | ||
if parseText { | ||
// log.Println("TEXT:", row) | ||
if row == "\r" || row == "" { | ||
parseText = false // XXX until text = empty | ||
} else { | ||
row = strings.TrimSpace(row) | ||
if row != "" { | ||
caption.Text = append(caption.Text, row) | ||
} | ||
} | ||
if strings.Join(caption.Text, "") != "" { | ||
res.Captions = append(res.Captions, caption) | ||
seq++ | ||
caption = Caption{Seq: seq} | ||
} | ||
} else if !parseText { | ||
if row == "" { | ||
if rowNum != len(rows)-1 { | ||
log.Println("NOTICE: ccdb seem to have reached end of valid stream at row", rowNum, "of", len(rows)) | ||
} | ||
break | ||
} | ||
// log.Println("TIME:", row) | ||
parts := strings.SplitN(row, ",", 2) | ||
if len(parts) == 2 { | ||
caption.Start, _ = parseCCDBTime(parts[0]) | ||
caption.End, _ = parseCCDBTime(parts[1]) | ||
} else { | ||
log.Println("TIME seq", seq, ", input row", (rowNum + 1), "error:", row) | ||
} | ||
parseText = true | ||
} | ||
} | ||
return | ||
} | ||
|
||
func parseCCDBTime(s string) (time.Time, error) { | ||
return parseTime(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package subtitles | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewFromCCDBCapture(t *testing.T) { | ||
|
||
in := "[SUBTITLE]\r\n" + | ||
"[COLF]&HFFFFFF,[STYLE]no,[SIZE]10,[FONT]Arial\r\n" + | ||
"00:00:16.24,00:01:25.82\r\n" + | ||
"Whoa. \r\n" + | ||
"\r\n" + | ||
"00:01:31.45,00:01:33.62\r\n" + | ||
"Go on. Get out. \r\n" + | ||
"\r\n" + | ||
"00:01:33.62,00:01:33.65\r\n" + | ||
" \r\n" + // should disappear in the parsed captions | ||
"\r\n" + | ||
"00:01:33.65,00:01:34.81\r\n" + | ||
"Out! \r\n" + | ||
"\r\n" | ||
|
||
expected := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 16, 24), | ||
makeTime(0, 1, 25, 82), | ||
[]string{"Whoa."}, | ||
}, Caption{ | ||
2, | ||
makeTime(0, 1, 31, 45), | ||
makeTime(0, 1, 33, 62), | ||
[]string{"Go on. Get out."}, | ||
}, Caption{ | ||
3, | ||
makeTime(0, 1, 33, 65), | ||
makeTime(0, 1, 34, 81), | ||
[]string{"Out!"}, | ||
}}} | ||
|
||
res, err := NewFromCCDBCapture(in) | ||
assert.Equal(t, nil, err) | ||
assert.Equal(t, expected, res) | ||
} | ||
|
||
func TestParseCCDBTime(t *testing.T) { | ||
t1, _ := parseCCDBTime("00:00:16.24") | ||
assert.Equal(t, makeTime(0, 0, 16, 24), t1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.