2021-05-16 12:18:19 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import "bytes"
|
|
|
|
|
|
|
|
type Sniffer func(header []byte) bool
|
|
|
|
|
|
|
|
var snifferRegistry = map[string]Sniffer{
|
2021-05-23 19:59:29 +08:00
|
|
|
".mp3": SnifferMP3,
|
2021-05-16 12:18:19 +08:00
|
|
|
".flac": SnifferFLAC,
|
2021-05-23 19:59:29 +08:00
|
|
|
".ogg": SnifferOGG,
|
|
|
|
".m4a": SnifferM4A,
|
2021-05-16 12:18:19 +08:00
|
|
|
".wav": SnifferWAV,
|
|
|
|
".wma": SnifferWMA,
|
2021-05-23 19:59:29 +08:00
|
|
|
".aac": SnifferAAC,
|
2021-05-25 12:20:04 +08:00
|
|
|
".dff": SnifferDFF,
|
2021-05-16 12:18:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
2021-05-23 19:59:29 +08:00
|
|
|
func SnifferAAC(header []byte) bool {
|
|
|
|
return bytes.HasPrefix(header, []byte{0xFF, 0xF1})
|
|
|
|
}
|
2021-05-25 12:20:04 +08:00
|
|
|
|
|
|
|
// 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"))
|
|
|
|
}
|