Skip to content

Commit

Permalink
Simple support for WebVTT style (#95)
Browse files Browse the repository at this point in the history
* Simple support for WebVTT style

* fixup! Simple support for WebVTT style

* [webvtt/test] Add cases of STYLES

* [webvtt] Use long condition instead of function

---------

Co-authored-by: Nhan Nguyen <[email protected]>
  • Loading branch information
NhanNguyen700 and NhanNguyen700 authored Nov 3, 2023
1 parent f1d0d10 commit 2c5a2cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type StyleAttributes struct {
WebVTTRegionAnchor string
WebVTTScroll string
WebVTTSize string
WebVTTStyles []string
WebVTTVertical string
WebVTTViewportAnchor string
WebVTTWidth string
Expand Down
14 changes: 14 additions & 0 deletions testdata/example-in.vtt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ STYLE
::cue(b) {
color: peachpuff;
}
::cue(c) {
color: white;
}
STYLE
::cue(a) {
color: red;
}
::cue(d) {
color: red;
background-image: linear-gradient(to bottom, dimgray, lightgray);
}

Region: id=fred width=40% lines=3 regionanchor=0%,100% viewportanchor=10%,90% scroll=up
Region: id=bill width=40% lines=3 regionanchor=100%,100% viewportanchor=90%,90% scroll=up
Expand Down
15 changes: 15 additions & 0 deletions testdata/example-out.vtt
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
WEBVTT
STYLE
::cue(b) {
color: peachpuff;
}
::cue(c) {
color: white;
}
::cue(a) {
color: red;
}
::cue(d) {
color: red;
background-image: linear-gradient(to bottom, dimgray, lightgray);
}
Region: id=bill lines=3 regionanchor=100%,100% scroll=up viewportanchor=90%,90% width=40%
Region: id=fred lines=3 regionanchor=0%,100% scroll=up viewportanchor=10%,90% width=40%

Expand Down
35 changes: 32 additions & 3 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
webvttBlockNameRegion = "region"
webvttBlockNameStyle = "style"
webvttBlockNameText = "text"
webvttDefaultStyleID = "astisub-webvtt-default-style-id"
webvttTimeBoundariesSeparator = " --> "
webvttTimestampMap = "X-TIMESTAMP-MAP"
)
Expand Down Expand Up @@ -109,6 +110,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
var comments []string
var index int
var timeOffset time.Duration
var webVTTStyles *StyleAttributes

for scanner.Scan() {
// Fetch line
Expand All @@ -122,8 +124,14 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
comments = append(comments, strings.TrimPrefix(line, "NOTE "))
// Empty line
case len(line) == 0:
// Reset block name
blockName = ""
// Reset block name, if we are not in the middle of CSS.
// If we are in STYLE block and the CSS is empty or we meet the right brace at the end of last line,
// then we are not in CSS and can switch to parse next WebVTT block.
if blockName != webvttBlockNameStyle || webVTTStyles == nil ||
len(webVTTStyles.WebVTTStyles) == 0 ||
strings.HasSuffix(webVTTStyles.WebVTTStyles[len(webVTTStyles.WebVTTStyles)-1], "}") {
blockName = ""
}
// Region
case strings.HasPrefix(line, "Region: "):
// Add region styles
Expand Down Expand Up @@ -162,6 +170,15 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
// Style
case strings.HasPrefix(line, "STYLE"):
blockName = webvttBlockNameStyle

if _, ok := o.Styles[webvttDefaultStyleID]; !ok {
webVTTStyles = &StyleAttributes{}
o.Styles[webvttDefaultStyleID] = &Style{
InlineStyle: webVTTStyles,
ID: webvttDefaultStyleID,
}
}

// Time boundaries
case strings.Contains(line, webvttTimeBoundariesSeparator):
// Set block name
Expand Down Expand Up @@ -257,7 +274,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
case webvttBlockNameComment:
comments = append(comments, line)
case webvttBlockNameStyle:
// TODO Do something with the style
webVTTStyles.WebVTTStyles = append(webVTTStyles.WebVTTStyles, line)
case webvttBlockNameText:
// Parse line
if l := parseTextWebVTT(line); len(l.Items) > 0 {
Expand Down Expand Up @@ -360,11 +377,23 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
var c []byte
c = append(c, []byte("WEBVTT\n\n")...)

var style []string
for _, s := range s.Styles {
if s.InlineStyle != nil {
style = append(style, s.InlineStyle.WebVTTStyles...)
}
}

if len(style) > 0 {
c = append(c, []byte(fmt.Sprintf("STYLE\n%s\n\n", strings.Join(style, "\n")))...)
}

// Add regions
var k []string
for _, region := range s.Regions {
k = append(k, region.ID)
}

sort.Strings(k)
for _, id := range k {
c = append(c, []byte("Region: id="+s.Regions[id].ID)...)
Expand Down

0 comments on commit 2c5a2cc

Please sign in to comment.