feat: detect actual audio format and resolve quality mismatch
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func normalizedFormat(format string) string {
|
||||
f := strings.ToLower(strings.TrimPrefix(format, "."))
|
||||
switch f {
|
||||
case "m4a", "mp4", "mmp4":
|
||||
return "m4a"
|
||||
case "mflac", "flac":
|
||||
return "flac"
|
||||
case "mp3":
|
||||
return "mp3"
|
||||
case "ogg", "opus":
|
||||
return "ogg"
|
||||
case "wav":
|
||||
return "wav"
|
||||
case "aac":
|
||||
return "aac"
|
||||
default:
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
||||
func actualQuality(requested, resolved, format string) string {
|
||||
f := normalizedFormat(format)
|
||||
if f == "flac" {
|
||||
if resolved != "" {
|
||||
return resolved
|
||||
}
|
||||
if requested == "lossless" || requested == "hires" {
|
||||
return requested
|
||||
}
|
||||
}
|
||||
if f == "mp3" {
|
||||
if resolved == "standard" || resolved == "exhigh" || resolved == "sq" {
|
||||
return resolved
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func renameToActualFormat(local string, format string) (string, error) {
|
||||
ext := normalizedFormat(format)
|
||||
if ext == "" {
|
||||
return local, nil
|
||||
}
|
||||
target := strings.TrimSuffix(local, filepath.Ext(local)) + "." + ext
|
||||
if target == local {
|
||||
return local, nil
|
||||
}
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
if err := os.Remove(local); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
if err := os.Rename(local, target); err != nil {
|
||||
return "", fmt.Errorf("rename actual format: %w", err)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
Reference in New Issue
Block a user