feat: detect actual audio format and resolve quality mismatch

This commit is contained in:
OpenClaw Agent
2026-09-16 05:34:47 +08:00
parent f376348b05
commit 83224fe5b5
6 changed files with 260 additions and 26 deletions
+67
View File
@@ -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
}