From 6d62ebfa30951fd3b03d1582eb9edd0976e0e96a Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 12 Apr 2021 11:36:19 +1200 Subject: [PATCH] use mediainfo to get duration for final subtitle entry --- main.go | 18 +++++++++++++++++- writesubs.go | 10 ++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 5d8aad8..f076478 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/writesubs.go b/writesubs.go index 4994afd..4e7a3e4 100644 --- a/writesubs.go +++ b/writesubs.go @@ -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",