Skip to content

Commit

Permalink
Use youtube-dl output template with extension parameter mxpv#50
Browse files Browse the repository at this point in the history
  • Loading branch information
huseyz committed Nov 26, 2019
1 parent 2c5e333 commit 90de499
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions pkg/ytdl/ytdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package ytdl

import (
"context"
"fmt"
"os/exec"
"path/filepath"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -38,7 +40,13 @@ 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 youtubeDlOutputTemplate(feedPath string, episode *model.Episode) string {
filename := fmt.Sprintf("%s.%s", episode.ID, "%(ext)s")
return filepath.Join(feedPath, filename)
}

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 {
Expand All @@ -50,7 +58,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s
"--format",
"bestaudio",
"--output",
destPath,
outputTemplate,
url,
)
} else { //nolint
Expand All @@ -62,7 +70,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s
"--format",
"worstaudio",
"--output",
destPath,
outputTemplate,
url,
)
}
Expand All @@ -76,7 +84,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
Expand All @@ -85,7 +93,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,
)
}
Expand Down

0 comments on commit 90de499

Please sign in to comment.