feat: add wav download quality
This commit is contained in:
+29
-10
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/audio"
|
||||
"github.com/mathismqn/godeez/internal/deezer"
|
||||
"github.com/mathismqn/godeez/internal/fsutil"
|
||||
"github.com/mathismqn/godeez/internal/store"
|
||||
@@ -15,17 +16,25 @@ import (
|
||||
)
|
||||
|
||||
func (d *Downloader) downloadTrack(ctx context.Context, resource deezer.Resource, track *deezer.Track, opts Options, outputDir string) downloadResult {
|
||||
media, err := d.deezerClient.FetchMedia(ctx, track, opts.Quality)
|
||||
media, err := d.deezerClient.FetchMedia(ctx, track, opts.sourceQuality())
|
||||
if err != nil {
|
||||
return downloadResult{err: fmt.Errorf("failed to fetch media: %w", err)}
|
||||
}
|
||||
|
||||
mediaFormat := media.Format()
|
||||
if opts.Strict && strings.ToLower(mediaFormat) != opts.Quality {
|
||||
outputFormat := mediaFormat
|
||||
if opts.convertsToWAV() {
|
||||
if mediaFormat != "FLAC" {
|
||||
return downloadResult{err: fmt.Errorf("wav requires a flac source, but only '%s' is available", strings.ToLower(mediaFormat))}
|
||||
}
|
||||
outputFormat = "WAV"
|
||||
}
|
||||
|
||||
if opts.Strict && strings.ToLower(outputFormat) != opts.Quality {
|
||||
return downloadResult{err: fmt.Errorf("requested quality '%s' not available", opts.Quality)}
|
||||
}
|
||||
|
||||
if skipPath, skip := d.shouldSkipDownload(ctx, track.ID, mediaFormat); skip {
|
||||
if skipPath, skip := d.shouldSkipDownload(ctx, track.ID, outputFormat); skip {
|
||||
return downloadResult{skipped: true, path: skipPath}
|
||||
}
|
||||
|
||||
@@ -42,18 +51,28 @@ func (d *Downloader) downloadTrack(ctx context.Context, resource deezer.Resource
|
||||
return downloadResult{err: fmt.Errorf("failed to get media stream: %w", err)}
|
||||
}
|
||||
|
||||
fileName := track.Filename(d.kind, mediaFormat)
|
||||
fileName := track.Filename(d.kind, outputFormat)
|
||||
outputPath := d.uniqueOutputPath(track.ID, filepath.Join(outputDir, fileName))
|
||||
|
||||
key := deezer.BlowfishKey(track.ID)
|
||||
if err := d.streamToFile(dlCtx, stream, outputPath, key); err != nil {
|
||||
if opts.convertsToWAV() {
|
||||
tmpPath, err := d.streamToTempFile(dlCtx, stream, outputDir, key)
|
||||
if err != nil {
|
||||
return downloadResult{err: fmt.Errorf("failed to stream to file: %w", err)}
|
||||
}
|
||||
defer fsutil.Remove(tmpPath)
|
||||
|
||||
if err := audio.FLACToWAV(ctx, tmpPath, outputPath); err != nil {
|
||||
return downloadResult{err: fmt.Errorf("failed to convert to wav: %w", err)}
|
||||
}
|
||||
} else if err := d.streamToFile(dlCtx, stream, outputPath, key); err != nil {
|
||||
return downloadResult{err: fmt.Errorf("failed to stream to file: %w", err)}
|
||||
}
|
||||
|
||||
var warnings []string
|
||||
|
||||
if opts.Quality != strings.ToLower(mediaFormat) {
|
||||
warnings = append(warnings, fmt.Sprintf("requested quality '%s' not available, using '%s' instead", opts.Quality, strings.ToLower(mediaFormat)))
|
||||
if opts.Quality != strings.ToLower(outputFormat) {
|
||||
warnings = append(warnings, fmt.Sprintf("requested quality '%s' not available, using '%s' instead", opts.Quality, strings.ToLower(outputFormat)))
|
||||
}
|
||||
|
||||
cover, err := d.deezerClient.FetchCoverImage(ctx, track)
|
||||
@@ -69,7 +88,7 @@ func (d *Downloader) downloadTrack(ctx context.Context, resource deezer.Resource
|
||||
}
|
||||
|
||||
warnings = append(warnings, metadata.warnings...)
|
||||
warnings = append(warnings, d.finalizeDownload(resource, track, outputPath, mediaFormat, metadata.genre, cover, metadata.bpmKey)...)
|
||||
warnings = append(warnings, d.finalizeDownload(resource, track, outputPath, outputFormat, metadata.genre, cover, metadata.bpmKey)...)
|
||||
|
||||
return downloadResult{warnings: warnings}
|
||||
}
|
||||
@@ -90,7 +109,7 @@ func (d *Downloader) uniqueOutputPath(trackID, path string) string {
|
||||
return candidate
|
||||
}
|
||||
|
||||
func (d *Downloader) finalizeDownload(resource deezer.Resource, track *deezer.Track, outputPath, mediaFormat, genre string, cover []byte, bpmKey bpmKey) []string {
|
||||
func (d *Downloader) finalizeDownload(resource deezer.Resource, track *deezer.Track, outputPath, outputFormat, genre string, cover []byte, bpmKey bpmKey) []string {
|
||||
var warnings []string
|
||||
|
||||
if err := tag.Write(outputPath, buildTagMetadata(resource, track, cover, bpmKey, genre)); err != nil {
|
||||
@@ -104,7 +123,7 @@ func (d *Downloader) finalizeDownload(resource deezer.Resource, track *deezer.Tr
|
||||
|
||||
info := &store.DownloadInfo{
|
||||
TrackID: track.ID,
|
||||
Quality: mediaFormat,
|
||||
Quality: outputFormat,
|
||||
Path: outputPath,
|
||||
Hash: hash,
|
||||
Downloaded: time.Now(),
|
||||
|
||||
Reference in New Issue
Block a user