Skip to content

Commit

Permalink
Merge 3447ccf into 44053e2
Browse files Browse the repository at this point in the history
  • Loading branch information
MerNat committed May 30, 2020
2 parents 44053e2 + 3447ccf commit 09d3ecf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
line = strings.TrimSpace(scanner.Text())
lineNum++

if lineNum == 1 {
line = strings.TrimPrefix(line, string(BytesBOM))
}

// Line contains time boundaries
if strings.Contains(line, srtTimeBoundariesSeparator) {
// Remove last item of previous subtitle since it's the index
index := s.Lines[len(s.Lines)-1]
s.Lines = s.Lines[:len(s.Lines)-1]

// Remove trailing empty lines
Expand All @@ -66,6 +71,9 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
// Init subtitle
s = &Item{}

// Fetch Index
s.Index, _ = strconv.Atoi(index.String())

// Fetch time boundaries
boundaries := strings.Split(line, srtTimeBoundariesSeparator)
if s.StartAt, err = parseDurationSRT(boundaries[0]); err != nil {
Expand Down
1 change: 1 addition & 0 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func NewSubtitles() *Subtitles {
// Item represents a text to show between 2 time boundaries with formatting
type Item struct {
Comments []string
Index int
EndAt time.Duration
InlineStyle *StyleAttributes
Lines []Line
Expand Down
17 changes: 14 additions & 3 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
var item = &Item{}
var blockName string
var comments []string
var indexContent []int

for scanner.Scan() {
// Fetch line
line = strings.TrimSpace(scanner.Text())

// Fetch Index
lineNum++
// Check prefixes

switch {
// Comment
case strings.HasPrefix(line, "NOTE "):
Expand Down Expand Up @@ -106,7 +110,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
// Add region
o.Regions[r.ID] = r
// Style
case strings.HasPrefix(line, "STYLE "):
case strings.HasPrefix(line, "STYLE"):
blockName = webvttBlockNameStyle
// Time boundaries
case strings.Contains(line, webvttTimeBoundariesSeparator):
Expand Down Expand Up @@ -185,10 +189,17 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
item.Lines = append(item.Lines, Line{Items: []LineItem{{Text: line}}})
default:
// This is the ID
// TODO Do something with the id
// o.Items is empty initially - implementing to save indexe(s) later.
if index, err := strconv.Atoi(line); err == nil {
indexContent = append(indexContent, index)
}
}
}
}
// Attach indexContent to the correct position.
for _, v := range indexContent {
o.Items[v-1].Index = v
}
return
}

Expand Down

0 comments on commit 09d3ecf

Please sign in to comment.