-
Notifications
You must be signed in to change notification settings - Fork 134
Feat/streaming optimization #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 20 commits
1e7c16b
969140e
102b36b
d2f7670
abc0985
820c2cb
9abe444
4ae3e61
82e090d
aa08cba
d6b6e46
f8731f9
b0e80b6
44a67b7
06b2226
2d08a66
b0feb66
4776701
764fc08
8fd230f
fa8dbf1
914aeaf
a2d8524
266762f
932f2c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| module github.com/asticode/go-astisub | ||
|
|
||
| go 1.13 | ||
| go 1.24.0 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems your "revert to go 1.13" change was reverted by one of the last commit. Could you revert it back? |
||
|
|
||
| require ( | ||
| github.com/asticode/go-astikit v0.20.0 | ||
| github.com/asticode/go-astits v1.8.0 | ||
| github.com/asticode/go-astikit v0.57.1 | ||
| github.com/asticode/go-astits v1.15.0 | ||
| github.com/stretchr/testify v1.4.0 | ||
| golang.org/x/net v0.0.0-20200904194848-62affa334b73 | ||
| golang.org/x/text v0.3.2 | ||
| golang.org/x/net v0.49.0 | ||
| golang.org/x/text v0.33.0 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.0 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| gopkg.in/yaml.v2 v2.2.2 // indirect | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package astisub | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "strconv" | ||
|
|
@@ -220,49 +221,76 @@ func (s Subtitles) WriteToSRT(o io.Writer) (err error) { | |
| return | ||
| } | ||
|
|
||
| // Init writer | ||
| w := bufio.NewWriter(o) | ||
|
mysamimi marked this conversation as resolved.
Outdated
|
||
| defer w.Flush() | ||
|
|
||
| // Add BOM header | ||
| var c []byte | ||
| c = append(c, BytesBOM...) | ||
| if _, err = w.Write(BytesBOM); err != nil { | ||
|
mysamimi marked this conversation as resolved.
Outdated
|
||
| err = fmt.Errorf("astisub: writing bom failed: %w", err) | ||
| return | ||
| } | ||
|
|
||
| // Loop through subtitles | ||
| for k, v := range s.Items { | ||
| // Add time boundaries | ||
| c = append(c, []byte(strconv.Itoa(k+1))...) | ||
| c = append(c, bytesLineSeparator...) | ||
| c = append(c, []byte(formatDurationSRT(v.StartAt))...) | ||
| c = append(c, bytesSRTTimeBoundariesSeparator...) | ||
| c = append(c, []byte(formatDurationSRT(v.EndAt))...) | ||
| c = append(c, bytesLineSeparator...) | ||
| if _, err = w.WriteString(strconv.Itoa(k + 1)); err != nil { | ||
| err = fmt.Errorf("astisub: writing index failed: %w", err) | ||
| return | ||
| } | ||
| if _, err = w.Write(bytesLineSeparator); err != nil { | ||
| err = fmt.Errorf("astisub: writing line separator failed: %w", err) | ||
| return | ||
| } | ||
| if _, err = w.WriteString(formatDurationSRT(v.StartAt)); err != nil { | ||
| err = fmt.Errorf("astisub: writing start at failed: %w", err) | ||
| return | ||
| } | ||
| if _, err = w.Write(bytesSRTTimeBoundariesSeparator); err != nil { | ||
| err = fmt.Errorf("astisub: writing time boundaries separator failed: %w", err) | ||
| return | ||
| } | ||
| if _, err = w.WriteString(formatDurationSRT(v.EndAt)); err != nil { | ||
| err = fmt.Errorf("astisub: writing end at failed: %w", err) | ||
| return | ||
| } | ||
| if _, err = w.Write(bytesLineSeparator); err != nil { | ||
| err = fmt.Errorf("astisub: writing line separator failed: %w", err) | ||
| return | ||
| } | ||
|
|
||
| // Loop through lines | ||
| for _, l := range v.Lines { | ||
| c = append(c, []byte(l.srtBytes())...) | ||
| if err = l.writeSRT(w); err != nil { | ||
| return | ||
|
mysamimi marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| // Add new line | ||
| c = append(c, bytesLineSeparator...) | ||
| } | ||
|
|
||
| // Remove last new line | ||
| c = c[:len(c)-1] | ||
|
|
||
| // Write | ||
| if _, err = o.Write(c); err != nil { | ||
| err = fmt.Errorf("astisub: writing failed: %w", err) | ||
| return | ||
| if k < len(s.Items)-1 { | ||
| if _, err = w.Write(bytesLineSeparator); err != nil { | ||
| err = fmt.Errorf("astisub: writing line separator failed: %w", err) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (l Line) srtBytes() (c []byte) { | ||
| func (l Line) writeSRT(w io.Writer) (err error) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide the |
||
| for _, li := range l.Items { | ||
| c = append(c, li.srtBytes()...) | ||
| if err = li.writeSRT(w); err != nil { | ||
| return | ||
|
mysamimi marked this conversation as resolved.
|
||
| } | ||
| } | ||
| if _, err = w.Write(bytesLineSeparator); err != nil { | ||
| err = fmt.Errorf("astisub: writing line separator failed: %w", err) | ||
| return | ||
| } | ||
| c = append(c, bytesLineSeparator...) | ||
| return | ||
| } | ||
|
|
||
| func (li LineItem) srtBytes() (c []byte) { | ||
| func (li LineItem) writeSRT(w io.Writer) (err error) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide the |
||
| // Get color | ||
| var color string | ||
| if li.InlineStyle != nil && li.InlineStyle.SRTColor != nil { | ||
|
|
@@ -282,32 +310,52 @@ func (li LineItem) srtBytes() (c []byte) { | |
|
|
||
| // Append | ||
| if color != "" { | ||
| c = append(c, []byte("<font color=\""+color+"\">")...) | ||
| if _, err = w.Write([]byte("<font color=\"" + color + "\">")); err != nil { | ||
| return fmt.Errorf("astisub: writing font color failed: %w", err) | ||
| } | ||
| } | ||
| if b { | ||
| c = append(c, []byte("<b>")...) | ||
| if _, err = w.Write([]byte("<b>")); err != nil { | ||
| return fmt.Errorf("astisub: writing bold failed: %w", err) | ||
| } | ||
| } | ||
| if i { | ||
| c = append(c, []byte("<i>")...) | ||
| if _, err = w.Write([]byte("<i>")); err != nil { | ||
| return fmt.Errorf("astisub: writing italics failed: %w", err) | ||
| } | ||
| } | ||
| if u { | ||
| c = append(c, []byte("<u>")...) | ||
| if _, err = w.Write([]byte("<u>")); err != nil { | ||
| return fmt.Errorf("astisub: writing underline failed: %w", err) | ||
| } | ||
| } | ||
| if pos != 0 { | ||
| c = append(c, []byte(fmt.Sprintf(`{\an%d}`, pos))...) | ||
| if _, err = w.Write([]byte(fmt.Sprintf(`{\an%d}`, pos))); err != nil { | ||
| return fmt.Errorf("astisub: writing position failed: %w", err) | ||
| } | ||
| } | ||
| if _, err = w.Write([]byte(escapeHTML(li.Text))); err != nil { | ||
| return fmt.Errorf("astisub: writing text failed: %w", err) | ||
| } | ||
| c = append(c, []byte(escapeHTML(li.Text))...) | ||
| if u { | ||
| c = append(c, []byte("</u>")...) | ||
| if _, err = w.Write([]byte("</u>")); err != nil { | ||
| return fmt.Errorf("astisub: writing underline close failed: %w", err) | ||
| } | ||
| } | ||
| if i { | ||
| c = append(c, []byte("</i>")...) | ||
| if _, err = w.Write([]byte("</i>")); err != nil { | ||
| return fmt.Errorf("astisub: writing italics close failed: %w", err) | ||
| } | ||
| } | ||
| if b { | ||
| c = append(c, []byte("</b>")...) | ||
| if _, err = w.Write([]byte("</b>")); err != nil { | ||
| return fmt.Errorf("astisub: writing bold close failed: %w", err) | ||
| } | ||
| } | ||
| if color != "" { | ||
| c = append(c, []byte("</font>")...) | ||
| if _, err = w.Write([]byte("</font>")); err != nil { | ||
| return fmt.Errorf("astisub: writing font close failed: %w", err) | ||
| } | ||
| } | ||
| return | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.