Skip to content

Commit 9f08c0f

Browse files
committed
CleanupSub: detect and parse .srt / .ssa
1 parent 695f068 commit 9f08c0f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

cleaner.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ import (
55
"strings"
66
)
77

8-
// CleanupSub performs cleanup on .srt data, returning a string. caller is responsible for passing UTF8 string
8+
// CleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
99
func CleanupSub(utf8 string, filterName string, keepAds bool) (string, error) {
1010

11-
captions := parseSrt(utf8)
11+
var captions []caption
12+
13+
if looksLikeSrt(utf8) {
14+
captions = parseSrt(utf8)
15+
} else {
16+
// falls back on .ssa decoding, for now
17+
captions = parseSsa(utf8)
18+
}
19+
1220
if !keepAds {
1321
captions = removeAds(captions)
1422
}

srt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
// Eol is the end of line characters to use when writing .srt data
1313
const eol = "\n"
1414

15+
func looksLikeSrt(s string) bool {
16+
if strings.HasPrefix(s, "1\n") || strings.HasPrefix(s, "1\r\n") {
17+
return true
18+
}
19+
return false
20+
}
21+
1522
// ParseSrt parses a .srt text into []Caption, assumes s is a clean utf8 string
1623
func parseSrt(s string) []caption {
1724

0 commit comments

Comments
 (0)