refactor: move audio sniffer to internal package
parent
62a38d5ab4
commit
6c168ee536
@ -1,55 +0,0 @@
|
|||||||
package common
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type Sniffer func(header []byte) bool
|
|
||||||
|
|
||||||
var snifferRegistry = map[string]Sniffer{
|
|
||||||
".mp3": SnifferMP3,
|
|
||||||
".flac": SnifferFLAC,
|
|
||||||
".ogg": SnifferOGG,
|
|
||||||
".m4a": SnifferM4A,
|
|
||||||
".wav": SnifferWAV,
|
|
||||||
".wma": SnifferWMA,
|
|
||||||
".aac": SnifferAAC,
|
|
||||||
".dff": SnifferDFF,
|
|
||||||
}
|
|
||||||
|
|
||||||
func SniffAll(header []byte) (string, bool) {
|
|
||||||
for ext, sniffer := range snifferRegistry {
|
|
||||||
if sniffer(header) {
|
|
||||||
return ext, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferM4A(header []byte) bool {
|
|
||||||
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferOGG(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("OggS"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferFLAC(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("fLaC"))
|
|
||||||
}
|
|
||||||
func SnifferMP3(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("ID3"))
|
|
||||||
}
|
|
||||||
func SnifferWAV(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("RIFF"))
|
|
||||||
}
|
|
||||||
func SnifferWMA(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"))
|
|
||||||
}
|
|
||||||
func SnifferAAC(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte{0xFF, 0xF1})
|
|
||||||
}
|
|
||||||
|
|
||||||
// SnifferDFF sniff a DSDIFF format
|
|
||||||
// reference to: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
|
||||||
func SnifferDFF(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("FRM8"))
|
|
||||||
}
|
|
@ -0,0 +1,61 @@
|
|||||||
|
package sniff
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type Sniffer interface {
|
||||||
|
Sniff(header []byte) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var audioExtensions = map[string]Sniffer{
|
||||||
|
// ref: https://mimesniff.spec.whatwg.org
|
||||||
|
".mp3": prefixSniffer("ID3"),
|
||||||
|
".ogg": prefixSniffer("OggS"),
|
||||||
|
".wav": prefixSniffer("RIFF"),
|
||||||
|
|
||||||
|
// ref: https://www.loc.gov/preservation/digital/formats/fdd/fdd000027.shtml
|
||||||
|
".wma": prefixSniffer{
|
||||||
|
0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11,
|
||||||
|
0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c,
|
||||||
|
},
|
||||||
|
|
||||||
|
// ref: https://www.garykessler.net/library/file_sigs.html
|
||||||
|
".m4a": mpeg4Sniffer{}, // MPEG-4 container, m4a treat as audio
|
||||||
|
".aac": prefixSniffer{0xFF, 0xF1}, // MPEG-4 AAC-LC
|
||||||
|
|
||||||
|
".flac": prefixSniffer("fLaC"), // ref: https://xiph.org/flac/format.html
|
||||||
|
".dff": prefixSniffer("FRM8"), // DSDIFF, ref: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// AudioExtension sniffs the known audio types, and returns the file extension.
|
||||||
|
// header is recommended to at least 16 bytes.
|
||||||
|
func AudioExtension(header []byte) (string, bool) {
|
||||||
|
for ext, sniffer := range audioExtensions {
|
||||||
|
if sniffer.Sniff(header) {
|
||||||
|
return ext, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// AudioExtensionWithFallback is equivalent to AudioExtension, but returns fallback
|
||||||
|
// most likely to use .mp3 as fallback, because mp3 files may not have ID3v2 tag.
|
||||||
|
func AudioExtensionWithFallback(header []byte, fallback string) string {
|
||||||
|
ext, ok := AudioExtension(header)
|
||||||
|
if !ok {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
return ext
|
||||||
|
}
|
||||||
|
|
||||||
|
type prefixSniffer []byte
|
||||||
|
|
||||||
|
func (s prefixSniffer) Sniff(header []byte) bool {
|
||||||
|
return bytes.HasPrefix(header, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
type mpeg4Sniffer struct{}
|
||||||
|
|
||||||
|
func (mpeg4Sniffer) Sniff(header []byte) bool {
|
||||||
|
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
|
||||||
|
}
|
Loading…
Reference in New Issue