From 95fc4587d73cbf2e1a34f58c3fe1250d3c53ed33 Mon Sep 17 00:00:00 2001 From: Huseyin Zengin Date: Mon, 25 Nov 2019 21:30:45 +0100 Subject: [PATCH] Use youtube-dl output template with extension parameter #50 --- cmd/podsync/updater.go | 4 ++-- pkg/ytdl/ytdl.go | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/podsync/updater.go b/cmd/podsync/updater.go index 94a6fa85..2b56e7bd 100644 --- a/cmd/podsync/updater.go +++ b/cmd/podsync/updater.go @@ -21,7 +21,7 @@ import ( ) type Downloader interface { - Download(ctx context.Context, feedConfig *config.Feed, url string, destPath string) (string, error) + Download(ctx context.Context, feedConfig *config.Feed, url string, feedPath string, episode *model.Episode) (string, error) } type Updater struct { @@ -86,7 +86,7 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error { if os.IsNotExist(err) { // There is no file on disk, download episode logger.Infof("! downloading episode %s", episode.VideoURL) - if output, err := u.downloader.Download(ctx, feedConfig, episode.VideoURL, episodePath); err == nil { + if output, err := u.downloader.Download(ctx, feedConfig, episode.VideoURL, feedPath, episode); err == nil { downloaded++ } else { // YouTube might block host with HTTP Error 429: Too Many Requests diff --git a/pkg/ytdl/ytdl.go b/pkg/ytdl/ytdl.go index b79c53b0..85a67bc6 100644 --- a/pkg/ytdl/ytdl.go +++ b/pkg/ytdl/ytdl.go @@ -2,7 +2,9 @@ package ytdl import ( "context" + "fmt" "os/exec" + "path/filepath" "time" "github.com/pkg/errors" @@ -38,7 +40,8 @@ func New(ctx context.Context) (*YoutubeDl, error) { return ytdl, nil } -func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url string, destPath string) (string, error) { +func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url string, feedPath string, episode *model.Episode) (string, error) { + outputTemplate := youtubeDlOutputTemplate(feedPath, episode) if feedConfig.Format == model.FormatAudio { // Audio if feedConfig.Quality == model.QualityHigh { @@ -50,7 +53,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "bestaudio", "--output", - destPath, + outputTemplate, url, ) } else { //nolint @@ -62,7 +65,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "worstaudio", "--output", - destPath, + outputTemplate, url, ) } @@ -76,7 +79,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "--output", - destPath, + outputTemplate, url, ) } else { //nolint @@ -85,7 +88,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst", "--output", - destPath, + outputTemplate, url, ) } @@ -105,3 +108,8 @@ func (YoutubeDl) exec(ctx context.Context, args ...string) (string, error) { return string(output), nil } + +func youtubeDlOutputTemplate(feedPath string, episode *model.Episode) string { + filename := fmt.Sprintf("%s.%s", episode.ID, "%(ext)s") + return filepath.Join(feedPath, filename) +}