Skip to content

Commit

Permalink
TTML: Fixed improper spacing between captions (#43)
Browse files Browse the repository at this point in the history
* 1. Fixed improper spacing between captions while converting TTML -> VTT

2. Added String To Pointer utility.

3. Converted TTML style attributes to pointers

4. Fixed Webvtt not using parent style attributes in region

* 1. Fixed Test cases
2. Fixed SRT and STL strings join

* 1. Removed Util
2. Added Regex to remove multiple space
3. In ttml.go update 'getStyleAttributes()' to 'newStyleAttributesFromTTMLInStyleAttributes()'
4. Using go-astikit to convert string to string pointer.

* Improper spacing fix

* Improper spacing fix

* Added Space in TTML out.
Added comments

* Added space to fix unit test

* update go.sum

* update go.sum

* Update go.sum

* Update go.sum
  • Loading branch information
discovery-avishekgulshan committed Jun 14, 2021
1 parent 7452768 commit 982542a
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 106 deletions.
2 changes: 1 addition & 1 deletion srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
o.Items = append(o.Items, s)
} else {
// Add text
s.Lines = append(s.Lines, Line{Items: []LineItem{{Text: line}}})
s.Lines = append(s.Lines, Line{Items: []LineItem{{Text: strings.TrimSpace(line)}}})
}
}
return
Expand Down
4 changes: 2 additions & 2 deletions ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ func newSSAEventFromItem(i Item) (e *ssaEvent) {
if len(l.VoiceName) > 0 {
e.name = l.VoiceName
}
lines = append(lines, strings.Join(items, ""))
lines = append(lines, strings.Join(items, " "))
}
e.text = strings.Join(lines, "\\n")
return
Expand Down Expand Up @@ -1014,7 +1014,7 @@ func newSSAEventFromString(header, content string, format map[int]string) (e *ss
e.style = item
}
case ssaEventFormatNameText:
e.text = item
e.text = strings.TrimSpace(item)
}
// Marked
case ssaEventFormatNameMarked:
Expand Down
74 changes: 48 additions & 26 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package astisub
import (
"errors"
"fmt"
"github.com/asticode/go-astikit"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -207,30 +208,30 @@ type StyleAttributes struct {
TeletextSpacesAfter *int
TeletextSpacesBefore *int
// TODO Use pointers with real types below
TTMLBackgroundColor string // https://htmlcolorcodes.com/fr/
TTMLColor string
TTMLDirection string
TTMLDisplay string
TTMLDisplayAlign string
TTMLExtent string
TTMLFontFamily string
TTMLFontSize string
TTMLFontStyle string
TTMLFontWeight string
TTMLLineHeight string
TTMLOpacity string
TTMLOrigin string
TTMLOverflow string
TTMLPadding string
TTMLShowBackground string
TTMLTextAlign string
TTMLTextDecoration string
TTMLTextOutline string
TTMLUnicodeBidi string
TTMLVisibility string
TTMLWrapOption string
TTMLWritingMode string
TTMLZIndex int
TTMLBackgroundColor *string // https://htmlcolorcodes.com/fr/
TTMLColor *string
TTMLDirection *string
TTMLDisplay *string
TTMLDisplayAlign *string
TTMLExtent *string
TTMLFontFamily *string
TTMLFontSize *string
TTMLFontStyle *string
TTMLFontWeight *string
TTMLLineHeight *string
TTMLOpacity *string
TTMLOrigin *string
TTMLOverflow *string
TTMLPadding *string
TTMLShowBackground *string
TTMLTextAlign *string
TTMLTextDecoration *string
TTMLTextOutline *string
TTMLUnicodeBidi *string
TTMLVisibility *string
TTMLWrapOption *string
TTMLWritingMode *string
TTMLZIndex *int
WebVTTAlign string
WebVTTItalics bool
WebVTTLine string
Expand Down Expand Up @@ -261,11 +262,32 @@ func (sa *StyleAttributes) propagateSTLAttributes() {

func (sa *StyleAttributes) propagateTeletextAttributes() {
if sa.TeletextColor != nil {
sa.TTMLColor = "#" + sa.TeletextColor.TTMLString()
sa.TTMLColor = astikit.StrPtr("#" + sa.TeletextColor.TTMLString())
}
}

func (sa *StyleAttributes) propagateTTMLAttributes() {}
func (sa *StyleAttributes) propagateTTMLAttributes() {
if sa.TTMLTextAlign != nil {
sa.WebVTTAlign = *sa.TTMLTextAlign
}
}

func (sa *StyleAttributes) propagateTTMLRegionAttributes() {
if sa.TTMLExtent != nil {
lineHeight := 5 //assuming height of line as 5.33vh
dimensions := strings.Split(*sa.TTMLExtent, " ")
sa.WebVTTWidth = dimensions[0]
if height, err := strconv.Atoi(strings.ReplaceAll(dimensions[1], "%", "")); err == nil {
sa.WebVTTLines = height / lineHeight
}

}
if sa.TTMLOrigin != nil {
sa.WebVTTRegionAnchor = "0%,0%"
sa.WebVTTViewportAnchor = strings.ReplaceAll(strings.TrimSpace(*sa.TTMLOrigin), " ", ",")
}

}

func (sa *StyleAttributes) propagateWebVTTAttributes() {}

Expand Down
24 changes: 12 additions & 12 deletions teletext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,64 +79,64 @@ func TestParseTeletextRow(t *testing.T) {
TeletextColor: ColorBlack,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#000000",
TTMLColor: astikit.StrPtr("#000000"),
}},
{Text: "red", InlineStyle: &StyleAttributes{
TeletextColor: ColorRed,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ff0000",
TTMLColor: astikit.StrPtr("#ff0000"),
}},
{Text: "green", InlineStyle: &StyleAttributes{
TeletextColor: ColorGreen,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#008000",
TTMLColor: astikit.StrPtr("#008000"),
}},
{Text: "yellow", InlineStyle: &StyleAttributes{
TeletextColor: ColorYellow,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffff00",
TTMLColor: astikit.StrPtr("#ffff00"),
}},
{Text: "blue", InlineStyle: &StyleAttributes{
TeletextColor: ColorBlue,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#0000ff",
TTMLColor: astikit.StrPtr("#0000ff"),
}},
{Text: "magenta", InlineStyle: &StyleAttributes{
TeletextColor: ColorMagenta,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ff00ff",
TTMLColor: astikit.StrPtr("#ff00ff"),
}},
{Text: "cyan", InlineStyle: &StyleAttributes{
TeletextColor: ColorCyan,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#00ffff",
TTMLColor: astikit.StrPtr("#00ffff"),
}},
{Text: "white", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffffff",
TTMLColor: astikit.StrPtr("#ffffff"),
}},
{Text: "double height", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextDoubleHeight: astikit.BoolPtr(true),
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffffff",
TTMLColor: astikit.StrPtr("#ffffff"),
}},
{Text: "double width", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextDoubleHeight: astikit.BoolPtr(true),
TeletextDoubleWidth: astikit.BoolPtr(true),
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffffff",
TTMLColor: astikit.StrPtr("#ffffff"),
}},
{Text: "double size", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
Expand All @@ -145,7 +145,7 @@ func TestParseTeletextRow(t *testing.T) {
TeletextDoubleSize: astikit.BoolPtr(true),
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffffff",
TTMLColor: astikit.StrPtr("#ffffff"),
}},
{Text: "reset", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
Expand All @@ -154,7 +154,7 @@ func TestParseTeletextRow(t *testing.T) {
TeletextDoubleSize: astikit.BoolPtr(false),
TeletextSpacesAfter: astikit.IntPtr(0),
TeletextSpacesBefore: astikit.IntPtr(0),
TTMLColor: "#ffffff",
TTMLColor: astikit.StrPtr("#ffffff"),
}},
}, i.Lines[0].Items)
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/example-out.ttml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<p begin="00:02:04.080" end="00:02:07.120" region="region_2">
<span>MAN:</span>
<br></br>
<span>How did we</span>
<span style="style_1" tts:color="green">end up</span>
<span>How did we </span>
<span style="style_1" tts:color="green">end up </span>
<span>here?</span>
</p>
<p begin="00:02:12.160" end="00:02:15.200" region="region_1">
Expand Down
121 changes: 68 additions & 53 deletions ttml.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,48 @@ type TTMLInMetadata struct {

// TTMLInStyleAttributes represents input TTML style attributes
type TTMLInStyleAttributes struct {
BackgroundColor string `xml:"backgroundColor,attr,omitempty"`
Color string `xml:"color,attr,omitempty"`
Direction string `xml:"direction,attr,omitempty"`
Display string `xml:"display,attr,omitempty"`
DisplayAlign string `xml:"displayAlign,attr,omitempty"`
Extent string `xml:"extent,attr,omitempty"`
FontFamily string `xml:"fontFamily,attr,omitempty"`
FontSize string `xml:"fontSize,attr,omitempty"`
FontStyle string `xml:"fontStyle,attr,omitempty"`
FontWeight string `xml:"fontWeight,attr,omitempty"`
LineHeight string `xml:"lineHeight,attr,omitempty"`
Opacity string `xml:"opacity,attr,omitempty"`
Origin string `xml:"origin,attr,omitempty"`
Overflow string `xml:"overflow,attr,omitempty"`
Padding string `xml:"padding,attr,omitempty"`
ShowBackground string `xml:"showBackground,attr,omitempty"`
TextAlign string `xml:"textAlign,attr,omitempty"`
TextDecoration string `xml:"textDecoration,attr,omitempty"`
TextOutline string `xml:"textOutline,attr,omitempty"`
UnicodeBidi string `xml:"unicodeBidi,attr,omitempty"`
Visibility string `xml:"visibility,attr,omitempty"`
WrapOption string `xml:"wrapOption,attr,omitempty"`
WritingMode string `xml:"writingMode,attr,omitempty"`
ZIndex int `xml:"zIndex,attr,omitempty"`
BackgroundColor *string `xml:"backgroundColor,attr,omitempty"`
Color *string `xml:"color,attr,omitempty"`
Direction *string `xml:"direction,attr,omitempty"`
Display *string `xml:"display,attr,omitempty"`
DisplayAlign *string `xml:"displayAlign,attr,omitempty"`
Extent *string `xml:"extent,attr,omitempty"`
FontFamily *string `xml:"fontFamily,attr,omitempty"`
FontSize *string `xml:"fontSize,attr,omitempty"`
FontStyle *string `xml:"fontStyle,attr,omitempty"`
FontWeight *string `xml:"fontWeight,attr,omitempty"`
LineHeight *string `xml:"lineHeight,attr,omitempty"`
Opacity *string `xml:"opacity,attr,omitempty"`
Origin *string `xml:"origin,attr,omitempty"`
Overflow *string `xml:"overflow,attr,omitempty"`
Padding *string `xml:"padding,attr,omitempty"`
ShowBackground *string `xml:"showBackground,attr,omitempty"`
TextAlign *string `xml:"textAlign,attr,omitempty"`
TextDecoration *string `xml:"textDecoration,attr,omitempty"`
TextOutline *string `xml:"textOutline,attr,omitempty"`
UnicodeBidi *string `xml:"unicodeBidi,attr,omitempty"`
Visibility *string `xml:"visibility,attr,omitempty"`
WrapOption *string `xml:"wrapOption,attr,omitempty"`
WritingMode *string `xml:"writingMode,attr,omitempty"`
ZIndex *int `xml:"zIndex,attr,omitempty"`
}

// StyleAttributes converts TTMLInStyleAttributes into a StyleAttributes
func (s TTMLInStyleAttributes) styleAttributes() (o *StyleAttributes) {
o = &StyleAttributes{
o = newStyleAttributesFromTTMLInStyleAttributes(s)
o.propagateTTMLAttributes()
return
}

// StyleAttributes converts TTMLInStyleAttributes for region into a StyleAttributes
func (s TTMLInStyleAttributes) regionStyleAttributes() (o *StyleAttributes) {
o = newStyleAttributesFromTTMLInStyleAttributes(s)
o.propagateTTMLRegionAttributes()
return
}

func newStyleAttributesFromTTMLInStyleAttributes(s TTMLInStyleAttributes) *StyleAttributes {
return &StyleAttributes{
TTMLBackgroundColor: s.BackgroundColor,
TTMLColor: s.Color,
TTMLDirection: s.Direction,
Expand All @@ -124,8 +137,6 @@ func (s TTMLInStyleAttributes) styleAttributes() (o *StyleAttributes) {
TTMLWritingMode: s.WritingMode,
TTMLZIndex: s.ZIndex,
}
o.propagateTTMLAttributes()
return
}

// TTMLInHeader represents an input TTML header
Expand Down Expand Up @@ -330,7 +341,7 @@ func ReadFromTTML(i io.Reader) (o *Subtitles, err error) {
for _, tr := range ttml.Regions {
var r = &Region{
ID: tr.ID,
InlineStyle: tr.TTMLInStyleAttributes.styleAttributes(),
InlineStyle: tr.TTMLInStyleAttributes.regionStyleAttributes(),
}
if len(tr.Style) > 0 {
if _, ok := o.Styles[tr.Style]; !ok {
Expand Down Expand Up @@ -450,30 +461,30 @@ type TTMLOutMetadata struct {

// TTMLOutStyleAttributes represents output TTML style attributes
type TTMLOutStyleAttributes struct {
BackgroundColor string `xml:"tts:backgroundColor,attr,omitempty"`
Color string `xml:"tts:color,attr,omitempty"`
Direction string `xml:"tts:direction,attr,omitempty"`
Display string `xml:"tts:display,attr,omitempty"`
DisplayAlign string `xml:"tts:displayAlign,attr,omitempty"`
Extent string `xml:"tts:extent,attr,omitempty"`
FontFamily string `xml:"tts:fontFamily,attr,omitempty"`
FontSize string `xml:"tts:fontSize,attr,omitempty"`
FontStyle string `xml:"tts:fontStyle,attr,omitempty"`
FontWeight string `xml:"tts:fontWeight,attr,omitempty"`
LineHeight string `xml:"tts:lineHeight,attr,omitempty"`
Opacity string `xml:"tts:opacity,attr,omitempty"`
Origin string `xml:"tts:origin,attr,omitempty"`
Overflow string `xml:"tts:overflow,attr,omitempty"`
Padding string `xml:"tts:padding,attr,omitempty"`
ShowBackground string `xml:"tts:showBackground,attr,omitempty"`
TextAlign string `xml:"tts:textAlign,attr,omitempty"`
TextDecoration string `xml:"tts:textDecoration,attr,omitempty"`
TextOutline string `xml:"tts:textOutline,attr,omitempty"`
UnicodeBidi string `xml:"tts:unicodeBidi,attr,omitempty"`
Visibility string `xml:"tts:visibility,attr,omitempty"`
WrapOption string `xml:"tts:wrapOption,attr,omitempty"`
WritingMode string `xml:"tts:writingMode,attr,omitempty"`
ZIndex int `xml:"tts:zIndex,attr,omitempty"`
BackgroundColor *string `xml:"tts:backgroundColor,attr,omitempty"`
Color *string `xml:"tts:color,attr,omitempty"`
Direction *string `xml:"tts:direction,attr,omitempty"`
Display *string `xml:"tts:display,attr,omitempty"`
DisplayAlign *string `xml:"tts:displayAlign,attr,omitempty"`
Extent *string `xml:"tts:extent,attr,omitempty"`
FontFamily *string `xml:"tts:fontFamily,attr,omitempty"`
FontSize *string `xml:"tts:fontSize,attr,omitempty"`
FontStyle *string `xml:"tts:fontStyle,attr,omitempty"`
FontWeight *string `xml:"tts:fontWeight,attr,omitempty"`
LineHeight *string `xml:"tts:lineHeight,attr,omitempty"`
Opacity *string `xml:"tts:opacity,attr,omitempty"`
Origin *string `xml:"tts:origin,attr,omitempty"`
Overflow *string `xml:"tts:overflow,attr,omitempty"`
Padding *string `xml:"tts:padding,attr,omitempty"`
ShowBackground *string `xml:"tts:showBackground,attr,omitempty"`
TextAlign *string `xml:"tts:textAlign,attr,omitempty"`
TextDecoration *string `xml:"tts:textDecoration,attr,omitempty"`
TextOutline *string `xml:"tts:textOutline,attr,omitempty"`
UnicodeBidi *string `xml:"tts:unicodeBidi,attr,omitempty"`
Visibility *string `xml:"tts:visibility,attr,omitempty"`
WrapOption *string `xml:"tts:wrapOption,attr,omitempty"`
WritingMode *string `xml:"tts:writingMode,attr,omitempty"`
ZIndex *int `xml:"tts:zIndex,attr,omitempty"`
}

// ttmlOutStyleAttributesFromStyleAttributes converts StyleAttributes into a TTMLOutStyleAttributes
Expand Down Expand Up @@ -637,13 +648,17 @@ func (s Subtitles) WriteToTTML(o io.Writer) (err error) {
// Add lines
for _, line := range item.Lines {
// Loop through line items
for _, lineItem := range line.Items {
for idx, lineItem := range line.Items {
// Init ttml item
var ttmlItem = TTMLOutItem{
Text: lineItem.Text,
TTMLOutStyleAttributes: ttmlOutStyleAttributesFromStyleAttributes(lineItem.InlineStyle),
XMLName: xml.Name{Local: "span"},
}
// condition to avoid adding space as the last character.
if idx < len(line.Items)-1 {
ttmlItem.Text = ttmlItem.Text + " "
}

// Add style
if lineItem.Style != nil {
Expand Down
Loading

0 comments on commit 982542a

Please sign in to comment.