refactor: move audio sniffer to internal package

This commit is contained in:
Unlock Music Dev 2022-11-22 06:16:40 +08:00
parent 62a38d5ab4
commit 6c168ee536
Signed by: um-dev
GPG Key ID: 95202E10D3413A1D
8 changed files with 86 additions and 72 deletions

View File

@ -4,6 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"unlock-music.dev/cli/internal/sniff"
) )
type RawDecoder struct { type RawDecoder struct {
@ -26,7 +28,7 @@ func (d *RawDecoder) Validate() error {
} }
var ok bool var ok bool
d.audioExt, ok = SniffAll(header) d.audioExt, ok = sniff.AudioExtension(header)
if !ok { if !ok {
return errors.New("raw: sniff audio type failed") return errors.New("raw: sniff audio type failed")
} }

View File

@ -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"))
}

View File

@ -206,8 +206,12 @@ func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
if d.cover != nil { if d.cover != nil {
return d.cover, nil return d.cover, nil
} }
if d.meta == nil {
return nil, errors.New("ncm meta not found")
}
imgURL := d.meta.GetAlbumImageURL() imgURL := d.meta.GetAlbumImageURL()
if d.meta != nil && !strings.HasPrefix(imgURL, "http") { if !strings.HasPrefix(imgURL, "http") {
return nil, nil // no cover image return nil, nil // no cover image
} }
@ -215,18 +219,19 @@ func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("download image failed: %w", err) return nil, fmt.Errorf("ncm download image failed: %w", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("download image failed: unexpected http status %s", resp.Status) return nil, fmt.Errorf("ncm download image failed: unexpected http status %s", resp.Status)
} }
data, err := io.ReadAll(resp.Body) d.cover, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("download image failed: %w", err) return nil, fmt.Errorf("ncm download image failed: %w", err)
} }
return data, nil
return d.cover, nil
} }
func (d *Decoder) GetMeta() common.Meta { func (d *Decoder) GetMeta() common.Meta {

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
) )
type Decoder struct { type Decoder struct {
@ -89,7 +90,7 @@ func (d *Decoder) validateDecode() error {
} }
d.cipher.Decrypt(buf, 0) d.cipher.Decrypt(buf, 0)
_, ok := common.SniffAll(buf) _, ok := sniff.AudioExtension(buf)
if !ok { if !ok {
return errors.New("qmc: detect file type failed") return errors.New("qmc: detect file type failed")
} }

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
) )
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70} var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
@ -30,7 +31,7 @@ func (d *Decoder) Validate() error {
return nil return nil
} }
if _, ok := common.SniffAll(header); ok { // not encrypted if _, ok := sniff.AudioExtension(header); ok { // not encrypted
d.audio = io.MultiReader(bytes.NewReader(header), d.raw) d.audio = io.MultiReader(bytes.NewReader(header), d.raw)
return nil return nil
} }

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
) )
type Decoder struct { type Decoder struct {
@ -27,7 +28,7 @@ func (d *Decoder) Validate() error {
{ // try to decode with x2m { // try to decode with x2m
header := decryptX2MHeader(encryptedHeader) header := decryptX2MHeader(encryptedHeader)
if _, ok := common.SniffAll(header); ok { if _, ok := sniff.AudioExtension(header); ok {
d.audio = io.MultiReader(bytes.NewReader(header), d.rd) d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
return nil return nil
} }
@ -36,7 +37,7 @@ func (d *Decoder) Validate() error {
{ // try to decode with x3m { // try to decode with x3m
// not read file again, since x2m and x3m have the same header size // not read file again, since x2m and x3m have the same header size
header := decryptX3MHeader(encryptedHeader) header := decryptX3MHeader(encryptedHeader)
if _, ok := common.SniffAll(header); ok { if _, ok := sniff.AudioExtension(header); ok {
d.audio = io.MultiReader(bytes.NewReader(header), d.rd) d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
return nil return nil
} }

View File

@ -25,6 +25,7 @@ import (
_ "unlock-music.dev/cli/algo/xiami" _ "unlock-music.dev/cli/algo/xiami"
_ "unlock-music.dev/cli/algo/ximalaya" _ "unlock-music.dev/cli/algo/ximalaya"
"unlock-music.dev/cli/internal/logging" "unlock-music.dev/cli/internal/logging"
"unlock-music.dev/cli/internal/sniff"
) )
var AppVersion = "v0.0.6" var AppVersion = "v0.0.6"
@ -182,13 +183,10 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
return fmt.Errorf("read header failed: %w", err) return fmt.Errorf("read header failed: %w", err)
} }
outExt := ".mp3" outExt := sniff.AudioExtensionWithFallback(header.Bytes(), ".mp3")
if ext, ok := common.SniffAll(header.Bytes()); ok { inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outExt = ext
}
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outPath := filepath.Join(outputDir, filenameOnly+outExt) outPath := filepath.Join(outputDir, inFilename+outExt)
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil { if err != nil {
return err return err

61
internal/sniff/audio.go Normal file
View File

@ -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])
}