diff --git a/README.md b/README.md index 2c8b33b..7a0dfd0 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ docker build -t server . docker run --rm -it \ -p 3000:8080 \ --env YTDLP_SITE_ADMIN_INITIAL_PASSWORD=abc123 \ + --env YTDLP_SITE_SESSION_AUTH_KEY=avowt7n8 \ server docker run --rm -it \ diff --git a/handlers.go b/handlers.go index 0920714..ba2d8a7 100644 --- a/handlers.go +++ b/handlers.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "fmt" "net/http" "os" @@ -99,32 +98,22 @@ func downloadPostHandler(c echo.Context) error { type Meta struct { title string - ext string } func getMeta(url string) (Meta, error) { - cmd := exec.Command("yt-dlp", "--simulate", "--print", "%(title)s.%(ext)s", url) + cmd := exec.Command("yt-dlp", + "--simulate", "--print", "%(title)s", + url) var stdout bytes.Buffer cmd.Stdout = &stdout err := cmd.Run() if err != nil { - fmt.Println("getTitle error:", err) + fmt.Println("getTitle error:", err, stdout.String()) return Meta{}, err } else { - - isDot := func(r rune) bool { - return r == '.' - } - - fields := strings.FieldsFunc(strings.TrimSpace(stdout.String()), isDot) - if len(fields) < 2 { - return Meta{}, errors.New("couldn't parse ytdlp output") - } - return Meta{ - title: strings.Join(fields[:len(fields)-1], "."), - ext: fields[len(fields)-1], + title: strings.TrimSpace(stdout.String()), }, nil } } @@ -183,7 +172,7 @@ func humanSize(bytes int64) string { } func startDownload(videoID uint, videoURL string) { - db.Model(&Video{}).Where("id = ?", videoID).Update("status", "downloading") + db.Model(&Video{}).Where("id = ?", videoID).Update("status", "metadata") meta, err := getMeta(videoURL) if err != nil { @@ -193,15 +182,21 @@ func startDownload(videoID uint, videoURL string) { fmt.Println("set video title:", meta.title) db.Model(&Video{}).Where("id = ?", videoID).Update("title", meta.title) - videoFilename := fmt.Sprintf("%d-%s.%s", videoID, meta.title, meta.ext) + db.Model(&Video{}).Where("id = ?", videoID).Update("status", "downloading") + videoFilename := fmt.Sprintf("%d-%s.mp4", videoID, meta.title) videoFilepath := filepath.Join(getDownloadDir(), "video", videoFilename) - cmd := exec.Command("yt-dlp", "-o", videoFilepath, videoURL) + cmd := exec.Command("yt-dlp", + "-f", "bestvideo[height<=720]+bestaudio/best[height<=720]", + "--recode-video", "mp4", + "-o", videoFilepath, + videoURL) err = cmd.Run() if err != nil { db.Model(&Video{}).Where("id = ?", videoID).Update("status", "failed") return } + db.Model(&Video{}).Where("id = ?", videoID).Update("status", "audio") audioFilename := fmt.Sprintf("%d-%s.mp3", videoID, meta.title) audioFilepath := filepath.Join(getDownloadDir(), "audio", audioFilename) audioDir := filepath.Dir(audioFilepath) diff --git a/middleware.go b/middleware.go index 9648d24..f1a7508 100644 --- a/middleware.go +++ b/middleware.go @@ -19,6 +19,7 @@ func authMiddleware(next echo.HandlerFunc) echo.HandlerFunc { userID, ok := session.Values["user_id"] if !ok { fmt.Println("authMiddleware: session does not contain user_id. Redirect to /login") + // return c.String(http.StatusForbidden, "not logged in") return c.Redirect(http.StatusSeeOther, "/login") } fmt.Println("set user_id", userID, "in context") diff --git a/templates/video.html b/templates/video.html index f9678bd..7945bae 100644 --- a/templates/video.html +++ b/templates/video.html @@ -10,10 +10,8 @@