mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
fix(audio): normalize file extension case in GetAudioDuration (#7321)
Uppercase or mixed-case extensions (e.g. .MP3, .WAV) fell through to the default branch and returned "unsupported audio format", so audio token usage could not be measured. Lowercase the extension before the switch. Closes #7319
This commit is contained in:
1 parent
2509e25fa0
commit
76f7dafd2b
2 files changed
+56
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/abema/go-mp4"
|
||||
"github.com/go-audio/aiff"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
// 它不再依赖外部的 ffmpeg 或 ffprobe 程序。
|
||||
func GetAudioDuration(ctx context.Context, f io.ReadSeeker, ext string) (duration float64, err error) {
|
||||
SysLog(fmt.Sprintf("GetAudioDuration: ext=%s", ext))
|
||||
ext = strings.ToLower(ext)
|
||||
// 根据文件扩展名选择解析器
|
||||
switch ext {
|
||||
case ".mp3":
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// minimalWAV builds a valid 16-bit PCM mono WAV with the given number of sample frames.
|
||||
func minimalWAV(sampleRate uint32, frames int) []byte {
|
||||
const numChannels = 1
|
||||
const bitsPerSample = 16
|
||||
dataSize := frames * numChannels * (bitsPerSample / 8)
|
||||
buf := &bytes.Buffer{}
|
||||
buf.WriteString("RIFF")
|
||||
binary.Write(buf, binary.LittleEndian, uint32(36+dataSize))
|
||||
buf.WriteString("WAVE")
|
||||
buf.WriteString("fmt ")
|
||||
binary.Write(buf, binary.LittleEndian, uint32(16))
|
||||
binary.Write(buf, binary.LittleEndian, uint16(1)) // PCM
|
||||
binary.Write(buf, binary.LittleEndian, uint16(numChannels))
|
||||
binary.Write(buf, binary.LittleEndian, sampleRate)
|
||||
byteRate := sampleRate * numChannels * (bitsPerSample / 8)
|
||||
binary.Write(buf, binary.LittleEndian, byteRate)
|
||||
binary.Write(buf, binary.LittleEndian, uint16(numChannels*(bitsPerSample/8)))
|
||||
binary.Write(buf, binary.LittleEndian, uint16(bitsPerSample))
|
||||
buf.WriteString("data")
|
||||
binary.Write(buf, binary.LittleEndian, uint32(dataSize))
|
||||
buf.Write(make([]byte, dataSize))
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestGetAudioDurationCaseInsensitiveExt(t *testing.T) {
|
||||
wav := minimalWAV(8000, 8000) // 1 second
|
||||
|
||||
for _, ext := range []string{".wav", ".WAV", ".Wav"} {
|
||||
d, err := GetAudioDuration(context.Background(), bytes.NewReader(wav), ext)
|
||||
if err != nil {
|
||||
t.Fatalf("ext %q: unexpected error: %v", ext, err)
|
||||
}
|
||||
if d < 0.99 || d > 1.01 {
|
||||
t.Fatalf("ext %q: expected ~1s duration, got %v", ext, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAudioDurationUppercaseNotUnsupported(t *testing.T) {
|
||||
_, err := GetAudioDuration(context.Background(), bytes.NewReader([]byte("not audio")), ".MP3")
|
||||
if err != nil && strings.Contains(err.Error(), "unsupported audio format") {
|
||||
t.Fatalf("uppercase extension rejected as unsupported: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user