From 367210ff23d14a9457f556234256ee96ee22691a Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 12 Apr 2021 11:51:50 +1200 Subject: [PATCH] add customizable logging levels --- main.go | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index f076478..ac10497 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,11 @@ import ( "strings" ) +const ( + LogLevelInfo int = 1 + LogLevelVerbose int = 2 +) + type config struct { youtubeDl string mkvmerge string @@ -22,12 +27,19 @@ type config struct { overrideOutput string subsOnly bool deleteTemporaries bool + loglevel int } func performDownload(ctx context.Context, cfg *config, targetUrl string) error { // + if cfg.loglevel >= LogLevelInfo { + fmt.Printf("Starting download for '%s'...\n", targetUrl) + } + + // + var content []byte var err error if targetUrl == "-" { @@ -70,16 +82,20 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error { // Download the video ytdl := exec.CommandContext(ctx, cfg.youtubeDl, `-f`, `bestvideo+bestaudio`, "https://youtu.be/"+ltc.VideoID, `--merge-output-format`, `mkv`, "-o", filepath.Join(tmpdir, "downloaded")) - ytdl.Stdout = os.Stdout - ytdl.Stderr = os.Stderr + if cfg.loglevel >= LogLevelVerbose { + ytdl.Stdout = os.Stdout + ytdl.Stderr = os.Stderr + } err = ytdl.Run() if err != nil { return err } // Determine video's total length - minfo := exec.CommandContext(ctx, cfg.mediainfo, `--Inform="General;%Duration%"`, filepath.Join(tmpdir, "downloaded.mkv")) - minfo.Stderr = os.Stderr + minfo := exec.CommandContext(ctx, cfg.mediainfo, `--Inform=General;%Duration%`, filepath.Join(tmpdir, "downloaded.mkv")) + if cfg.loglevel >= LogLevelVerbose { + minfo.Stderr = os.Stderr + } ret, err := minfo.Output() if err != nil { return err @@ -89,6 +105,10 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error { return err } + if cfg.loglevel >= LogLevelVerbose { + fmt.Printf("Video duration is %d ms\n", msecsDuration) + } + // Create the subtitle file (clamped to total length) fh, err := os.OpenFile(filepath.Join(tmpdir, "subtitles.srt"), os.O_CREATE|os.O_WRONLY, 0600) @@ -105,8 +125,10 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error { // Mux the subtitles into the file mkvm := exec.CommandContext(ctx, cfg.mkvmerge, `-o`, filepath.Join(tmpdir, "muxed.mkv"), filepath.Join(tmpdir, "downloaded.mkv"), filepath.Join(tmpdir, "subtitles.srt")) - mkvm.Stdout = os.Stdout - mkvm.Stderr = os.Stderr + if cfg.loglevel >= LogLevelVerbose { + mkvm.Stdout = os.Stdout + mkvm.Stderr = os.Stderr + } err = mkvm.Run() if err != nil { return err @@ -137,6 +159,10 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error { // Done + if cfg.loglevel >= LogLevelInfo { + fmt.Printf("Download complete for '%s'\n", outputFile) + } + return nil } @@ -153,6 +179,7 @@ Options: --output PATH Override output filename (only valid for a single URL) --delete-temporary=false Preserve temporary files + --loglevel 0|1|2 Set verbosity (0=silent, 1=normal, 2=verbose) `) os.Exit(1) } @@ -168,6 +195,7 @@ func main() { flag.StringVar(&cfg.mediainfo, "mediainfo", "mediainfo", "") flag.StringVar(&cfg.overrideOutput, "output", "", "") flag.BoolVar(&cfg.deleteTemporaries, "delete-temporary", true, "") + flag.IntVar(&cfg.loglevel, "loglevel", 1, "") flag.Usage = usage flag.Parse()