add customizable logging levels
This commit is contained in:
parent
6d62ebfa30
commit
367210ff23
30
main.go
30
main.go
@ -15,6 +15,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogLevelInfo int = 1
|
||||||
|
LogLevelVerbose int = 2
|
||||||
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
youtubeDl string
|
youtubeDl string
|
||||||
mkvmerge string
|
mkvmerge string
|
||||||
@ -22,12 +27,19 @@ type config struct {
|
|||||||
overrideOutput string
|
overrideOutput string
|
||||||
subsOnly bool
|
subsOnly bool
|
||||||
deleteTemporaries bool
|
deleteTemporaries bool
|
||||||
|
loglevel int
|
||||||
}
|
}
|
||||||
|
|
||||||
func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
|
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 content []byte
|
||||||
var err error
|
var err error
|
||||||
if targetUrl == "-" {
|
if targetUrl == "-" {
|
||||||
@ -70,16 +82,20 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
|
|||||||
|
|
||||||
// Download the video
|
// 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 := exec.CommandContext(ctx, cfg.youtubeDl, `-f`, `bestvideo+bestaudio`, "https://youtu.be/"+ltc.VideoID, `--merge-output-format`, `mkv`, "-o", filepath.Join(tmpdir, "downloaded"))
|
||||||
|
if cfg.loglevel >= LogLevelVerbose {
|
||||||
ytdl.Stdout = os.Stdout
|
ytdl.Stdout = os.Stdout
|
||||||
ytdl.Stderr = os.Stderr
|
ytdl.Stderr = os.Stderr
|
||||||
|
}
|
||||||
err = ytdl.Run()
|
err = ytdl.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine video's total length
|
// Determine video's total length
|
||||||
minfo := exec.CommandContext(ctx, cfg.mediainfo, `--Inform="General;%Duration%"`, filepath.Join(tmpdir, "downloaded.mkv"))
|
minfo := exec.CommandContext(ctx, cfg.mediainfo, `--Inform=General;%Duration%`, filepath.Join(tmpdir, "downloaded.mkv"))
|
||||||
|
if cfg.loglevel >= LogLevelVerbose {
|
||||||
minfo.Stderr = os.Stderr
|
minfo.Stderr = os.Stderr
|
||||||
|
}
|
||||||
ret, err := minfo.Output()
|
ret, err := minfo.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -89,6 +105,10 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.loglevel >= LogLevelVerbose {
|
||||||
|
fmt.Printf("Video duration is %d ms\n", msecsDuration)
|
||||||
|
}
|
||||||
|
|
||||||
// Create the subtitle file (clamped to total length)
|
// Create the subtitle file (clamped to total length)
|
||||||
|
|
||||||
fh, err := os.OpenFile(filepath.Join(tmpdir, "subtitles.srt"), os.O_CREATE|os.O_WRONLY, 0600)
|
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
|
// 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 := exec.CommandContext(ctx, cfg.mkvmerge, `-o`, filepath.Join(tmpdir, "muxed.mkv"), filepath.Join(tmpdir, "downloaded.mkv"), filepath.Join(tmpdir, "subtitles.srt"))
|
||||||
|
if cfg.loglevel >= LogLevelVerbose {
|
||||||
mkvm.Stdout = os.Stdout
|
mkvm.Stdout = os.Stdout
|
||||||
mkvm.Stderr = os.Stderr
|
mkvm.Stderr = os.Stderr
|
||||||
|
}
|
||||||
err = mkvm.Run()
|
err = mkvm.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -137,6 +159,10 @@ func performDownload(ctx context.Context, cfg *config, targetUrl string) error {
|
|||||||
|
|
||||||
// Done
|
// Done
|
||||||
|
|
||||||
|
if cfg.loglevel >= LogLevelInfo {
|
||||||
|
fmt.Printf("Download complete for '%s'\n", outputFile)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +179,7 @@ Options:
|
|||||||
--output PATH Override output filename
|
--output PATH Override output filename
|
||||||
(only valid for a single URL)
|
(only valid for a single URL)
|
||||||
--delete-temporary=false Preserve temporary files
|
--delete-temporary=false Preserve temporary files
|
||||||
|
--loglevel 0|1|2 Set verbosity (0=silent, 1=normal, 2=verbose)
|
||||||
`)
|
`)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -168,6 +195,7 @@ func main() {
|
|||||||
flag.StringVar(&cfg.mediainfo, "mediainfo", "mediainfo", "")
|
flag.StringVar(&cfg.mediainfo, "mediainfo", "mediainfo", "")
|
||||||
flag.StringVar(&cfg.overrideOutput, "output", "", "")
|
flag.StringVar(&cfg.overrideOutput, "output", "", "")
|
||||||
flag.BoolVar(&cfg.deleteTemporaries, "delete-temporary", true, "")
|
flag.BoolVar(&cfg.deleteTemporaries, "delete-temporary", true, "")
|
||||||
|
flag.IntVar(&cfg.loglevel, "loglevel", 1, "")
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user