Use youtube-dl output template with extension parameter #50 #59

This commit is contained in:
Huseyin Zengin
2019-11-26 10:58:33 -08:00
committed by Maksym Pavlenko
parent 59e4f5b024
commit 3ba98e8aee
2 changed files with 15 additions and 7 deletions
+2 -2
View File
@@ -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
+13 -5
View File
@@ -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)
}