support translator notes (scrcaps), emit ASS format instead of SRT
This commit is contained in:
parent
8b5f2e035b
commit
feefb054bf
3
main.go
3
main.go
@ -11,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -116,7 +117,7 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ltc.WriteSRT(fh, float64(msecsDuration)/1000)
|
err = ltc.WriteSubtitle(fh, float64(msecsDuration)/1000)
|
||||||
fh.Close()
|
fh.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -103,10 +103,6 @@ func (ltc *loadTupContent) Validate() error {
|
|||||||
return fmt.Errorf("scrsecs/scrcaps length mismatch")
|
return fmt.Errorf("scrsecs/scrcaps length mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(len(ltc.Scrcaps) == 0 || (len(ltc.Scrcaps) == 1 && ltc.Scrcaps[0] == "")) {
|
|
||||||
return errors.New("unsupported use of strcaps")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ltc.VideoID) == 0 {
|
if len(ltc.VideoID) == 0 {
|
||||||
return errors.New("unexpected blank video ID")
|
return errors.New("unexpected blank video ID")
|
||||||
}
|
}
|
||||||
|
90
writesubs.go
90
writesubs.go
@ -3,10 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func secs_to_srt_time(secs float64) string {
|
func secs_to_ass_time(secs float64) string {
|
||||||
dur := time.Duration(secs) * time.Second
|
dur := time.Duration(secs) * time.Second
|
||||||
|
|
||||||
hh := int64(dur.Hours())
|
hh := int64(dur.Hours())
|
||||||
@ -14,45 +15,92 @@ func secs_to_srt_time(secs float64) string {
|
|||||||
ss := int64(dur.Seconds()) - (hh * 3600) - (mm * 60)
|
ss := int64(dur.Seconds()) - (hh * 3600) - (mm * 60)
|
||||||
ms := int64(dur.Milliseconds()) - (hh * 3600000) - (mm * 60000) - (ss * 1000)
|
ms := int64(dur.Milliseconds()) - (hh * 3600000) - (mm * 60000) - (ss * 1000)
|
||||||
|
|
||||||
return fmt.Sprintf("%02d:%02d:%02d,%03d", hh, mm, ss, ms)
|
return fmt.Sprintf("%02d:%02d:%02d.%03d", hh, mm, ss, ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ltc *loadTupContent) WriteSRT(w io.Writer, totalVideoDurationSecs float64) error {
|
// WriteSubtitle streams the video subtitles to the supplied writer in ASS format.
|
||||||
/*
|
func (ltc *loadTupContent) WriteSubtitle(w io.Writer, totalVideoDurationSecs float64) error {
|
||||||
|
|
||||||
SRT file format (example from Wikipedia):
|
w.Write([]byte(`[Script Info]
|
||||||
|
; Script generated by loadtup-dl
|
||||||
|
ScriptType: v4.00+
|
||||||
|
Collisions: Normal
|
||||||
|
Timer: 100,0000
|
||||||
|
Video Aspect Ratio: 0
|
||||||
|
|
||||||
|
[V4+ Styles]
|
||||||
|
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
|
||||||
|
Style: Default, Arial,18,&H00FFFFFF,&H00FFFFFF,&H00000008,&H80000008,-1,0,0,0,100,100,0.00,0.00,1,1.00,2.00,2,30,30,30,0
|
||||||
|
Style: TLNote, Arial,12,&H00FFFFFF,&H00FFFFFF,&H00000008,&H80000008,-1,0,0,0,100,100,0.00,0.00,1,1.00,2.00,2,30,30,30,0
|
||||||
|
|
||||||
1
|
[Events]
|
||||||
00:02:17,440 --> 00:02:20,375
|
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
||||||
Senator, we're making
|
`))
|
||||||
our final approach into Coruscant.
|
|
||||||
|
|
||||||
2
|
type entry struct {
|
||||||
00:02:20,476 --> 00:02:22,501
|
startTime float64
|
||||||
Very good, Lieutenant.
|
entry string
|
||||||
*/
|
}
|
||||||
|
entries := make([]entry, 0, len(ltc.Caps)+len(ltc.Scrcaps))
|
||||||
|
|
||||||
ctr := 1
|
|
||||||
for i := 0; i < len(ltc.Caps); i += 1 {
|
for i := 0; i < len(ltc.Caps); i += 1 {
|
||||||
if ltc.Caps[i] == "" {
|
if ltc.Caps[i] == "" {
|
||||||
// Don't show anything
|
// Don't show anything
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
start := secs_to_srt_time(ltc.Secs[i])
|
start := secs_to_ass_time(ltc.Secs[i])
|
||||||
var end string
|
var end string
|
||||||
if i < len(ltc.Caps)-1 {
|
if i < len(ltc.Caps)-1 {
|
||||||
end = secs_to_srt_time(ltc.Secs[i+1])
|
end = secs_to_ass_time(ltc.Secs[i+1])
|
||||||
} else {
|
} else {
|
||||||
// The final subtitle. Loadtup displays these for the entire
|
// The final subtitle. Loadtup displays these for the entire
|
||||||
// remaining video duration
|
// remaining video duration
|
||||||
end = secs_to_srt_time(totalVideoDurationSecs)
|
end = secs_to_ass_time(totalVideoDurationSecs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "%d\n%s --> %s\n%s\n\n",
|
// Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
||||||
ctr, start, end, ltc.Caps[i])
|
entries = append(entries, entry{
|
||||||
// We emitted a message, increase the counter
|
startTime: ltc.Secs[i],
|
||||||
ctr += 1
|
entry: fmt.Sprintf("Dialogue: 0,%s,%s,Default,,0000,0000,0000,,{\\a2}%s\n", start, end, ltc.Caps[i]),
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repeat for scrcaps, using top positioning and a different layer
|
||||||
|
|
||||||
|
for i := 0; i < len(ltc.Scrcaps); i += 1 {
|
||||||
|
if ltc.Caps[i] == "" {
|
||||||
|
// Don't show anything
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
start := secs_to_ass_time(ltc.Scrsecs[i])
|
||||||
|
var end string
|
||||||
|
if i < len(ltc.Scrcaps)-1 {
|
||||||
|
end = secs_to_ass_time(ltc.Scrsecs[i+1])
|
||||||
|
} else {
|
||||||
|
// The final subtitle. Loadtup displays these for the entire
|
||||||
|
// remaining video duration
|
||||||
|
end = secs_to_ass_time(totalVideoDurationSecs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
||||||
|
entries = append(entries, entry{
|
||||||
|
startTime: ltc.Scrsecs[i],
|
||||||
|
entry: fmt.Sprintf("Dialogue: 1,%s,%s,TLNote,,0000,0000,0000,,{\\a6}%s\n", start, end, ltc.Scrcaps[i]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort all the entries by their start time, to mingle TL note entries
|
||||||
|
// properly with the other subtitles
|
||||||
|
sort.SliceStable(entries, func(i, j int) bool {
|
||||||
|
return entries[i].startTime < entries[j].startTime
|
||||||
|
})
|
||||||
|
|
||||||
|
// Emit all to writer
|
||||||
|
for _, e := range entries {
|
||||||
|
w.Write([]byte(e.entry))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user