Skip to content

Commit

Permalink
Merge 913ad61 into 62013ad
Browse files Browse the repository at this point in the history
  • Loading branch information
inremen committed Dec 13, 2019
2 parents 62013ad + 913ad61 commit 83d3662
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 22 deletions.
30 changes: 21 additions & 9 deletions stl.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,32 @@ func ReadFromSTL(i io.Reader) (o *Subtitles, err error) {

// Parse TTI block
var t = parseTTIBlock(b, g.framerate)

if t.extensionBlockNumber != extensionBlockNumberReservedUserData {

var starttime=g.timecodeStartOfProgramme
if starttime>t.timecodeIn{
starttime=0
}
// Create item
var i = &Item{
EndAt: t.timecodeOut - g.timecodeStartOfProgramme,
StartAt: t.timecodeIn - g.timecodeStartOfProgramme,
EndAt: t.timecodeOut - starttime,
StartAt: t.timecodeIn - starttime,
}

// Loop through rows
for _, text := range bytes.Split(t.text, []byte{0x8a}) {
parseTeletextRow(i, ch, func() styler { return newSTLStyler() }, text)
parseTeletextRow(i, ch, func() styler { return newSTLStyler() }, text)
}

if i.InlineStyle!=nil{
fmt.Printf("InlineStyle not NULL\n")
}else{
i.InlineStyle=&StyleAttributes{}
}
i.InlineStyle.TeletextVerticalPostion=t.verticalPosition
i.InlineStyle.TeletextJustificationCode=t.justificationCode
i.InlineStyle.propagateTeletextPositionJustification()
// Append item
o.Items = append(o.Items, i)
}
Expand Down Expand Up @@ -351,18 +363,18 @@ func parseGSIBlock(b []byte) (g *gsiBlock, err error) {
}

// Creation date
if v := strings.TrimSpace(string(b[224:230])); len(v) > 0 {
if v := strings.TrimSpace(string(b[224:230])); len(v) > 0 {
if g.creationDate, err = time.Parse("060102", v); err != nil {
err = errors.Wrapf(err, "astisub: parsing date %s failed", v)
return
err = errors.Wrapf(err, "astisub: parsing date %s failed", v)
g.creationDate=time.Now()
}
}

// Revision date
if v := strings.TrimSpace(string(b[230:236])); len(v) > 0 {
if v := strings.TrimSpace(string(b[230:236])); len(v) > 0 {
if g.revisionDate, err = time.Parse("060102", v); err != nil {
err = errors.Wrapf(err, "astisub: parsing date %s failed", v)
return
g.revisionDate=time.Now()
}
}

Expand Down
70 changes: 65 additions & 5 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
BytesBOM = []byte{239, 187, 191}
bytesLineSeparator = []byte("\n")
bytesSpace = []byte(" ")
bytesCierrac = []byte("</c>")
)

// Colors
Expand Down Expand Up @@ -73,7 +74,7 @@ func Open(o Options) (s *Subtitles, err error) {
case ".ssa", ".ass":
s, err = ReadFromSSA(f)
case ".stl":
s, err = ReadFromSTL(f)
s, err = ReadFromSTL(f)
case ".ts":
s, err = ReadFromTeletext(f, o.Teletext)
case ".ttml":
Expand All @@ -83,6 +84,7 @@ func Open(o Options) (s *Subtitles, err error) {
default:
err = ErrInvalidExtension
}

return
}

Expand Down Expand Up @@ -158,6 +160,53 @@ func (c *Color) TTMLString() string {
return fmt.Sprintf("%.6x", uint32(c.Red)<<16|uint32(c.Green)<<8|uint32(c.Blue))
}

// WebVtt expresses the color as a webvtt string
func (c *Color) WebVTTColor() string {
switch c {
case ColorBlack:
return "<c.black>" //rgba(0,0,0,1)
case ColorRed:
return "<c.red>" //rgba(255,0,0,1)
case ColorGreen:
return "<c.lime>" //rgba(0,255,0,1)
case ColorYellow:
return "<c.yellow>" //rgba(255,255,0,1)
case ColorBlue:
return "<c.blue>" //rgba(0,0,255,1)
case ColorMagenta:
return "<c.magenta>" //rgba(255,0,255,1)
case ColorCyan:
return "<c.cyan>" //rgba(0,255,255,1)
case ColorWhite:
return "<c.white>" //rgba(255,255,255,1)
default:
return ""
}

}

func (sa *StyleAttributes) WebVTTPositionFromTeletext() string {
switch sa.TeletextJustificationCode{
case stlJustificationCodeLeftJustifiedText:
return "20%" //left
case stlJustificationCodeRightJustifiedText:
return "80%" //right
default:
return ""
}
}

func (sa *StyleAttributes) WebVTTLineFromTeletext() string {
if sa.TeletextVerticalPostion<3{ //top
return "20%"
} else if sa.TeletextVerticalPostion<=12{ //(23/2)+1 //middle
return "50%"
} else{
return ""
}
}


// StyleAttributes represents style attributes
type StyleAttributes struct {
SSAAlignment *int
Expand Down Expand Up @@ -195,6 +244,8 @@ type StyleAttributes struct {
TeletextDoubleWidth *bool
TeletextSpacesAfter *int
TeletextSpacesBefore *int
TeletextVerticalPostion int
TeletextJustificationCode byte
// TODO Use pointers with real types below
TTMLBackgroundColor string // https://htmlcolorcodes.com/fr/
TTMLColor string
Expand Down Expand Up @@ -230,18 +281,27 @@ type StyleAttributes struct {
WebVTTVertical string
WebVTTViewportAnchor string
WebVTTWidth string
WebVTTColor string
}

func (sa *StyleAttributes) propagateSSAAttributes() {}

func (sa *StyleAttributes) propagateSTLAttributes() {}

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

func (sa *StyleAttributes) propagateTeletextPositionJustification() {
sa.WebVTTLine = sa.WebVTTLineFromTeletext()
sa.WebVTTPosition=sa.WebVTTPositionFromTeletext()
}



func (sa *StyleAttributes) propagateTTMLAttributes() {}

func (sa *StyleAttributes) propagateWebVTTAttributes() {}
Expand Down Expand Up @@ -574,7 +634,7 @@ func (s Subtitles) Write(dst string) (err error) {
return
}
defer f.Close()

// Write the content
switch filepath.Ext(strings.ToLower(dst)) {
case ".srt":
Expand All @@ -585,7 +645,7 @@ func (s Subtitles) Write(dst string) (err error) {
err = s.WriteToSTL(f)
case ".ttml":
err = s.WriteToTTML(f)
case ".vtt":
case ".vtt":
err = s.WriteToWebVTT(f)
default:
err = ErrInvalidExtension
Expand Down Expand Up @@ -692,7 +752,7 @@ func formatDuration(i time.Duration, millisecondSep string, numberOfMillisecondD

// Parse milliseconds
var milliseconds = float64(n/time.Millisecond) / float64(1000)
s += fmt.Sprintf("%."+strconv.Itoa(numberOfMillisecondDigits)+"f", milliseconds)[2:]
s += fmt.Sprintf("%."+strconv.Itoa(numberOfMillisecondDigits)+"f", milliseconds)[2:]
return
}

Expand Down
14 changes: 8 additions & 6 deletions teletext.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
var l = Line{}
var li = LineItem{InlineStyle: &StyleAttributes{}}
var started bool
started=true
var s styler
for _, v := range row {
// Create specific styler
Expand Down Expand Up @@ -893,10 +894,10 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
color = ColorCyan
case 0x7:
color = ColorWhite
case 0xa:
/*case 0xa:
started = false
case 0xb:
started = true
started = true*/
case 0xc:
doubleHeight = astiptr.Bool(false)
doubleSize = astiptr.Bool(false)
Expand Down Expand Up @@ -952,13 +953,13 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
li.Text += string(d.decode(v))
}
}

// Append line item
appendTeletextLineItem(&l, li, s)

// Append line
if len(l.Items) > 0 {
i.Lines = append(i.Lines, l)
i.Lines = append(i.Lines, l)
}
}

Expand Down Expand Up @@ -992,8 +993,9 @@ func appendTeletextLineItem(l *Line, li LineItem, s styler) {

// Propagate style attributes
li.InlineStyle.propagateTeletextAttributes()

if s != nil {
s.propagateStyleAttributes(li.InlineStyle)
s.propagateStyleAttributes(li.InlineStyle)
}

// Append line item
Expand Down
16 changes: 14 additions & 2 deletions webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
}

// formatDurationWebVTT formats a .vtt duration
func formatDurationWebVTT(i time.Duration) string {
func formatDurationWebVTT(i time.Duration) string {
return formatDuration(i, ".", 3)
}

Expand Down Expand Up @@ -290,16 +290,28 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
if item.InlineStyle.WebVTTVertical != "" {
c = append(c, bytesSpace...)
c = append(c, []byte("vertical:"+item.InlineStyle.WebVTTVertical)...)
}
}
}

// Add new line
c = append(c, bytesLineSeparator...)

// Loop through lines
for _, l := range item.Lines {
var cierrac=false
if l.Items!=nil && len(l.Items)>0 && l.Items[0].InlineStyle!=nil &&
l.Items[0].InlineStyle.WebVTTColor!=""{
c = append(c, []byte(l.Items[0].InlineStyle.WebVTTColor)...)
cierrac=true
}

c = append(c, []byte(l.String())...)
if cierrac{
c = append(c, bytesCierrac...)
}
c = append(c, bytesLineSeparator...)


}

// Add new line
Expand Down

0 comments on commit 83d3662

Please sign in to comment.