diff --git a/README.md b/README.md index e91a6cc..8f29bea 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ With Docker: `ronnieroller/media-roller:latest`. See https://hub.docker.com/repository/docker/ronnieroller/media-roller The files are saved to the /download directory which you can mount as needed. +## Docker Environemnt Variables +* `MR_DOWNLOAD_DIR` where videos are saved. Defaults to `/download` +* `MR_PROXY` will pass the value to yt-dlp witht he `--proxy` argument. Defaults to empty + # API To download a video directly, use the API endpoint: diff --git a/src/media/fetch.go b/src/media/fetch.go index 06b34f2..b6170cb 100644 --- a/src/media/fetch.go +++ b/src/media/fetch.go @@ -64,7 +64,7 @@ func FetchMedia(w http.ResponseWriter, r *http.Request) { return } - if err := fetchIndexTmpl.Execute(w, data); err != nil { + if err = fetchIndexTmpl.Execute(w, data); err != nil { log.Error().Msgf("Error rendering template: %v", err) http.Error(w, "Internal error", http.StatusInternalServerError) } @@ -132,7 +132,7 @@ func downloadMedia(url string) (string, string, error) { log.Info().Msgf("Downloading %s to %s", url, name) - cmd := exec.Command("yt-dlp", + args := []string{ "--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "--merge-output-format", "mp4", "--trim-filenames", "100", @@ -140,7 +140,15 @@ func downloadMedia(url string) (string, string, error) { "--write-info-json", "--verbose", "--output", name, - url) + } + + if vars := getEnvVars(); len(vars) > 0 { + args = append(args, vars...) + } + + args = append(args, url) + + cmd := exec.Command("yt-dlp", args...) var stdoutBuf, stderrBuf bytes.Buffer stdoutIn, _ := cmd.StdoutPipe() @@ -206,9 +214,9 @@ func getAllFilesForId(id string) ([]Media, error) { // We expect two files to be produced for each video, a json manifest and an mp4. for _, f := range files { if !strings.HasSuffix(f, ".json") { - fi, err := os.Stat(root + f) + fi, err2 := os.Stat(root + f) var size int64 = 0 - if err == nil { + if err2 == nil { size = fi.Size() } @@ -268,3 +276,13 @@ func getDownloadDir() string { } return "downloads/" } + +func getEnvVars() []string { + vars := make([]string, 0) + + if ev := strings.TrimSpace(os.Getenv("MR_PROXY")); ev != "" { + vars = append(vars, "--proxy", ev) + } + + return vars +}