fixed Can't download extracted audio as mp3 #152 (#196)

This commit is contained in:
Eric Lam
2024-09-18 01:49:07 +08:00
committed by GitHub
parent ef0d7ba5f8
commit a2793f9541
2 changed files with 44 additions and 17 deletions

View File

@@ -10,6 +10,11 @@ type ProgressTemplate struct {
Eta float32 `json:"eta"` Eta float32 `json:"eta"`
} }
type PostprocessTemplate struct {
FilePath string `json:"filepath"`
}
// Defines where and how the download needs to be saved // Defines where and how the download needs to be saved
type DownloadOutput struct { type DownloadOutput struct {
Path string Path string

View File

@@ -22,13 +22,20 @@ import (
"github.com/marcopeocchi/yt-dlp-web-ui/server/config" "github.com/marcopeocchi/yt-dlp-web-ui/server/config"
) )
const template = `download: const downloadTemplate = `download:
{ {
"eta":%(progress.eta)s, "eta":%(progress.eta)s,
"percentage":"%(progress._percent_str)s", "percentage":"%(progress._percent_str)s",
"speed":%(progress.speed)s "speed":%(progress.speed)s
}` }`
// filename not returning the correct extension after postprocess
const postprocessTemplate = `postprocess:
{
"filepath":"%(info.filepath)s"
}
`
const ( const (
StatusPending = iota StatusPending = iota
StatusDownloading StatusDownloading
@@ -80,16 +87,16 @@ func (p *Process) Start() {
buildFilename(&p.Output) buildFilename(&p.Output)
//TODO: it spawn another one yt-dlp process, too slow. templateReplacer := strings.NewReplacer("\n", "", "\t", "", " ", "")
go p.GetFileName(&out)
baseParams := []string{ baseParams := []string{
strings.Split(p.Url, "?list")[0], //no playlist strings.Split(p.Url, "?list")[0], //no playlist
"--newline", "--newline",
"--no-colors", "--no-colors",
"--no-playlist", "--no-playlist",
"--progress-template", "--progress-template",
strings.NewReplacer("\n", "", "\t", "", " ", "").Replace(template), templateReplacer.Replace(downloadTemplate),
"--progress-template",
templateReplacer.Replace(postprocessTemplate),
} }
// if user asked to manually override the output path... // if user asked to manually override the output path...
@@ -167,23 +174,33 @@ func (p *Process) consumeLogs(ctx context.Context, logs <-chan []byte) {
func (p *Process) parseLogEntry(entry []byte) { func (p *Process) parseLogEntry(entry []byte) {
var progress ProgressTemplate var progress ProgressTemplate
var postprocess PostprocessTemplate
if err := json.Unmarshal(entry, &progress); err != nil { if err := json.Unmarshal(entry, &progress); err == nil {
return p.Progress = DownloadProgress{
Status: StatusDownloading,
Percentage: progress.Percentage,
Speed: progress.Speed,
ETA: progress.Eta,
}
slog.Info("progress",
slog.String("id", p.getShortId()),
slog.String("url", p.Url),
slog.String("percentage", progress.Percentage),
)
} }
p.Progress = DownloadProgress{ if err := json.Unmarshal(entry, &postprocess); err == nil {
Status: StatusDownloading, p.Output.SavedFilePath = postprocess.FilePath
Percentage: progress.Percentage,
Speed: progress.Speed, slog.Info("postprocess",
ETA: progress.Eta, slog.String("id", p.getShortId()),
slog.String("url", p.Url),
slog.String("filepath", postprocess.FilePath),
)
} }
slog.Info("progress",
slog.String("id", p.getShortId()),
slog.String("url", p.Url),
slog.String("percentage", progress.Percentage),
)
} }
func (p *Process) detectYtDlpErrors(r io.Reader) { func (p *Process) detectYtDlpErrors(r io.Reader) {
@@ -208,6 +225,11 @@ func (p *Process) Complete() {
Speed: 0, Speed: 0,
ETA: 0, ETA: 0,
} }
// for safety, if the filename is not set, set it with original function
if p.Output.SavedFilePath == "" {
p.GetFileName(&p.Output)
}
slog.Info("finished", slog.Info("finished",
slog.String("id", p.getShortId()), slog.String("id", p.getShortId()),