Skip to content

Commit

Permalink
fixup! [webvtt] Preserve WebVTT tags
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanNguyen700 committed Nov 21, 2023
1 parent 292c7c5 commit 7b49b90
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
11 changes: 7 additions & 4 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,14 @@ type StyleAttributes struct {
TTMLWritingMode *string
TTMLZIndex *int
WebVTTAlign string
WebVTTItalics bool
WebVTTLine string
WebVTTLines int
WebVTTPosition string
WebVTTRegionAnchor string
WebVTTScroll string
WebVTTSize string
WebVTTStyles []string
WebVTTTags []*WebVTTTag
WebVTTTags []WebVTTTag
WebVTTVertical string
WebVTTViewportAnchor string
WebVTTWidth string
Expand All @@ -256,7 +255,11 @@ type WebVTTTag struct {
Classes []string
}

func (t *WebVTTTag) getStartTag() string {
func (t WebVTTTag) getStartTag() string {
if t.Name == "" {
return ""
}

s := t.Name
if len(t.Classes) > 0 {
s += "." + strings.Join(t.Classes, ".")
Expand All @@ -269,7 +272,7 @@ func (t *WebVTTTag) getStartTag() string {
return "<" + s + ">"
}

func (t *WebVTTTag) getEndTag() string {
func (t WebVTTTag) getEndTag() string {
if t.Name == "" {
return ""
}
Expand Down
24 changes: 14 additions & 10 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"log"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -305,15 +306,13 @@ func parseTextWebVTT(i string) (o Line) {
// Create tokenizer
tr := html.NewTokenizer(strings.NewReader(i))

webVTTTagStack := make([]*WebVTTTag, 64)
webVTTTagStackIndex := -1
webVTTTagStack := make([]WebVTTTag, 64)
webVTTTagStackIndex := -1 // This must be increased when pushing to stack and decreased when poping from stack

// Loop
loopCount := 0
for {
// Get next tag
t := tr.Next()
loopCount++
// Process error
if err := tr.Err(); err != nil {
break
Expand All @@ -339,15 +338,20 @@ func parseTextWebVTT(i string) (o Line) {
annotation = strings.TrimSpace(matches[4])
}

// Only support voicename if <v> appears in the begining of the line
// TODO: do something with <v> in the middle of the line
if loopCount == 1 && tagName == "v" {
o.VoiceName = annotation
if tagName == "v" {
if o.VoiceName == "" {
// Only get voicename of the first <v> appears in the line
o.VoiceName = annotation
} else {
// TODO: do something with other <v> instead of ignoring
log.Printf("astisub: found another voice name %q in %q. Ignore", annotation, i)
}
continue
}

// Push the tag to stack
webVTTTagStackIndex++
webVTTTagStack[webVTTTagStackIndex] = &WebVTTTag{
webVTTTagStack[webVTTTagStackIndex] = WebVTTTag{
Name: tagName,
Classes: classes,
Annotation: annotation,
Expand All @@ -359,7 +363,7 @@ func parseTextWebVTT(i string) (o Line) {
// Get style attribute
var sa *StyleAttributes
if webVTTTagStackIndex >= 0 {
tags := make([]*WebVTTTag, webVTTTagStackIndex+1)
tags := make([]WebVTTTag, webVTTTagStackIndex+1)
copy(tags, webVTTTagStack[:webVTTTagStackIndex+1])
sa = &StyleAttributes{
WebVTTTags: tags,
Expand Down

0 comments on commit 7b49b90

Please sign in to comment.