Skip to content

Commit

Permalink
Merge 44a67b7 into 5fddf19
Browse files Browse the repository at this point in the history
  • Loading branch information
mysamimi committed Feb 1, 2022
2 parents 5fddf19 + 44a67b7 commit 0d8a141
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
41 changes: 41 additions & 0 deletions subtitles.go
Expand Up @@ -595,6 +595,47 @@ func (s *Subtitles) Order() {
}
}

// ClipFrom clip items from input time
func (s *Subtitles) ClipFrom(cf time.Duration) {
newIndex := 0
var items []*Item
for index := 0; index < len(s.Items); index++ {
s.Items[index].StartAt -= cf
s.Items[index].EndAt -= cf
s.Items[index].Index = newIndex
if s.Items[index].StartAt < 0 {
s.Items[index].StartAt = 0
}
if s.Items[index].EndAt > 0 {
items = append(items, s.Items[index])
}
}
s.Items = items
}

// ClipFrom clip items until input time
func (s *Subtitles) ClipTo(ct time.Duration) {
lastIndex := 0
for index := 0; index < len(s.Items); index++ {
lastIndex = index
if s.Items[index].StartAt > ct {
break
}
if s.Items[index].EndAt > ct {
s.Items[index].EndAt = ct
break
}
}
s.Items = s.Items[:lastIndex+1]
}

// FixIndex fix item index
func (s *Subtitles) FixIndex() {
for i := 0; i < len(s.Items); i++ {
s.Items[i].Index = i + 1
}
}

// RemoveStyling removes the styling from the subtitles
func (s *Subtitles) RemoveStyling() {
s.Regions = map[string]*Region{}
Expand Down
26 changes: 26 additions & 0 deletions subtitles_test.go
Expand Up @@ -276,3 +276,29 @@ func TestSubtitles_RemoveStyling(t *testing.T) {
Styles: map[string]*astisub.Style{},
}, s)
}

func TestSubtitles_FixIndex(t *testing.T) {
var s = mockSubtitles()
s.FixIndex()
for i := 0; i < len(s.Items); i++ {
assert.Equal(t, i+1, s.Items[i].Index)
}
}

func TestSubtitles_ClipTo(t *testing.T) {
var s = mockSubtitles()
s.ClipTo(2 * time.Second)
assert.Equal(t, 1, len(s.Items))
assert.Equal(t, 1*time.Second, s.Items[0].StartAt)
assert.Equal(t, 2*time.Second, s.Items[0].EndAt)
}

func TestSubtitles_ClipFrom(t *testing.T) {
var s = mockSubtitles()
s.ClipFrom(2 * time.Second)
assert.Equal(t, 2, len(s.Items))
assert.Equal(t, 0*time.Second, s.Items[0].StartAt)
assert.Equal(t, 1*time.Second, s.Items[0].EndAt)
assert.Equal(t, 1*time.Second, s.Items[1].StartAt)
assert.Equal(t, 5*time.Second, s.Items[1].EndAt)
}
27 changes: 25 additions & 2 deletions webvtt.go
Expand Up @@ -338,7 +338,26 @@ func formatDurationWebVTT(i time.Duration) string {
}

// WriteToWebVTT writes subtitles in .vtt format
func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
// if set true in second args write index as item index
func (s Subtitles) WriteToWebVTT(args ...interface{}) (err error) {
var o io.Writer
writeWithIndex := false
for i, arg := range args {
switch i {
case 0: // default output writer
out, ok := arg.(io.Writer)
if !ok {
return fmt.Errorf("first input argument must be io.Writer")
}
o = out
case 1:
b, ok := arg.(bool)
if !ok {
return fmt.Errorf("second input argument must be boolean")
}
writeWithIndex = b
}
}
// Do not write anything if no subtitles
if len(s.Items) == 0 {
err = ErrNoSubtitlesToWrite
Expand Down Expand Up @@ -411,7 +430,11 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
}

// Add time boundaries
c = append(c, []byte(strconv.Itoa(index+1))...)
if writeWithIndex {
c = append(c, []byte(strconv.Itoa(item.Index))...)
} else {
c = append(c, []byte(strconv.Itoa(index+1))...)
}
c = append(c, bytesLineSeparator...)
c = append(c, []byte(formatDurationWebVTT(item.StartAt))...)
c = append(c, bytesWebVTTTimeBoundariesSeparator...)
Expand Down

0 comments on commit 0d8a141

Please sign in to comment.