use mediainfo to get duration for final subtitle entry

This commit is contained in:
mappu 2021-04-12 11:36:19 +12:00
parent 3ea89f1674
commit 6d62ebfa30
2 changed files with 21 additions and 7 deletions

18
main.go
View File

@ -11,12 +11,16 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
type config struct {
youtubeDl string
mkvmerge string
mediainfo string
overrideOutput string
subsOnly bool
deleteTemporaries bool
}
@ -74,6 +78,16 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
}
// Determine video's total length
minfo := exec.CommandContext(ctx, cfg.mediainfo, `--Inform="General;%Duration%"`, filepath.Join(tmpdir, "downloaded.mkv"))
minfo.Stderr = os.Stderr
ret, err := minfo.Output()
if err != nil {
return err
}
msecsDuration, err := strconv.ParseInt(strings.TrimSpace(string(ret)), 10, 64)
if err != nil {
return err
}
// Create the subtitle file (clamped to total length)
@ -82,7 +96,7 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
return err
}
err = ltc.WriteSRT(fh)
err = ltc.WriteSRT(fh, float64(msecsDuration)/1000)
fh.Close()
if err != nil {
return err
@ -135,6 +149,7 @@ read equivalent loadtup.com HTML content from stdin.
Options:
--youtube-dl PATH Override path to youtube-dl
--mkvmerge PATH Override path to mkvmerge
--mediainfo PATH Override path to mediainfo
--output PATH Override output filename
(only valid for a single URL)
--delete-temporary=false Preserve temporary files
@ -150,6 +165,7 @@ func main() {
flag.StringVar(&cfg.youtubeDl, "youtube-dl", "youtube-dl", "")
flag.StringVar(&cfg.mkvmerge, "mkvmerge", "mkvmerge", "")
flag.StringVar(&cfg.mediainfo, "mediainfo", "mediainfo", "")
flag.StringVar(&cfg.overrideOutput, "output", "", "")
flag.BoolVar(&cfg.deleteTemporaries, "delete-temporary", true, "")
flag.Usage = usage

View File

@ -17,7 +17,7 @@ func secs_to_srt_time(secs float64) string {
return fmt.Sprintf("%02d:%02d:%02d,%03d", hh, mm, ss, ms)
}
func (ltc *loadTupContent) WriteSRT(w io.Writer) error {
func (ltc *loadTupContent) WriteSRT(w io.Writer, totalVideoDurationSecs float64) error {
/*
SRT file format (example from Wikipedia):
@ -44,11 +44,9 @@ func (ltc *loadTupContent) WriteSRT(w io.Writer) error {
if i < len(ltc.Caps)-1 {
end = secs_to_srt_time(ltc.Secs[i+1])
} else {
// The final subtitle. We don't know how long it should be displayed
// for since we don't know the entire video's duration
// FIXME supply
// Assume 3 seconds
end = secs_to_srt_time(ltc.Secs[i] + 3)
// The final subtitle. Loadtup displays these for the entire
// remaining video duration
end = secs_to_srt_time(totalVideoDurationSecs)
}
fmt.Fprintf(w, "%d\n%s --> %s\n%s\n\n",