Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
15e340eac4 | |||
c836ac7cb5 |
@ -1,17 +1,24 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
type RawDecoder struct {
|
type RawDecoder struct {
|
||||||
file []byte
|
file []byte
|
||||||
audioExt string
|
audioExt string
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
|
||||||
func NewRawDecoder(file []byte) Decoder {
|
func NewRawDecoder(file []byte) Decoder {
|
||||||
return &RawDecoder{file: file}
|
return &RawDecoder{file: file}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) Validate() error {
|
func (d *RawDecoder) Validate() error {
|
||||||
return nil
|
for ext, sniffer := range snifferRegistry {
|
||||||
|
if sniffer(d.file) {
|
||||||
|
d.audioExt = ext
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors.New("audio doesn't recognized")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) Decode() error {
|
func (d RawDecoder) Decode() error {
|
||||||
@ -27,21 +34,19 @@ func (d RawDecoder) GetAudioData() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) GetAudioExt() string {
|
func (d RawDecoder) GetAudioExt() string {
|
||||||
return "." + d.audioExt
|
return d.audioExt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) GetMeta() Meta {
|
func (d RawDecoder) GetMeta() Meta {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func DecoderFuncWithExt(ext string) NewDecoderFunc {
|
|
||||||
return func(file []byte) Decoder {
|
|
||||||
return &RawDecoder{file: file, audioExt: ext}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func init() {
|
func init() {
|
||||||
/*RegisterDecoder("mp3", DecoderFuncWithExt("mp3"))
|
RegisterDecoder("mp3", NewRawDecoder)
|
||||||
RegisterDecoder("flac", DecoderFuncWithExt("flac"))
|
RegisterDecoder("flac", NewRawDecoder)
|
||||||
RegisterDecoder("wav", DecoderFuncWithExt("wav"))
|
RegisterDecoder("ogg", NewRawDecoder)
|
||||||
RegisterDecoder("ogg", DecoderFuncWithExt("ogg"))
|
RegisterDecoder("m4a", NewRawDecoder)
|
||||||
RegisterDecoder("m4a", DecoderFuncWithExt("m4a"))*/
|
RegisterDecoder("wav", NewRawDecoder)
|
||||||
|
RegisterDecoder("wma", NewRawDecoder)
|
||||||
|
RegisterDecoder("aac", NewRawDecoder)
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,13 @@ import "bytes"
|
|||||||
type Sniffer func(header []byte) bool
|
type Sniffer func(header []byte) bool
|
||||||
|
|
||||||
var snifferRegistry = map[string]Sniffer{
|
var snifferRegistry = map[string]Sniffer{
|
||||||
".m4a": SnifferM4A,
|
".mp3": SnifferMP3,
|
||||||
".ogg": SnifferOGG,
|
|
||||||
".flac": SnifferFLAC,
|
".flac": SnifferFLAC,
|
||||||
|
".ogg": SnifferOGG,
|
||||||
|
".m4a": SnifferM4A,
|
||||||
".wav": SnifferWAV,
|
".wav": SnifferWAV,
|
||||||
".wma": SnifferWMA,
|
".wma": SnifferWMA,
|
||||||
".mp3": SnifferMP3,
|
".aac": SnifferAAC,
|
||||||
}
|
}
|
||||||
|
|
||||||
func SniffAll(header []byte) (string, bool) {
|
func SniffAll(header []byte) (string, bool) {
|
||||||
@ -42,3 +43,6 @@ func SnifferWAV(header []byte) bool {
|
|||||||
func SnifferWMA(header []byte) bool {
|
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"))
|
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})
|
||||||
|
}
|
||||||
|
@ -40,7 +40,7 @@ func (d Decoder) GetAudioData() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetAudioExt() string {
|
func (d Decoder) GetAudioExt() string {
|
||||||
return ""
|
return "" // use sniffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetMeta() common.Meta {
|
func (d Decoder) GetMeta() common.Meta {
|
||||||
@ -58,9 +58,8 @@ func (d *Decoder) Validate() error {
|
|||||||
|
|
||||||
d.key = d.file[0x1c:0x2c]
|
d.key = d.file[0x1c:0x2c]
|
||||||
d.key = append(d.key, 0x00)
|
d.key = append(d.key, 0x00)
|
||||||
_ = d.file[0x2c:0x3c] //key2
|
_ = d.file[0x2c:0x3c] //todo: key2
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
func (d *Decoder) Decode() error {
|
||||||
@ -69,7 +68,8 @@ func (d *Decoder) Decode() error {
|
|||||||
lenData := len(dataEncrypted)
|
lenData := len(dataEncrypted)
|
||||||
initMask()
|
initMask()
|
||||||
if fullMaskLen < lenData {
|
if fullMaskLen < lenData {
|
||||||
logging.Log().Warn("文件过大,处理后的音频不完整,请向我们报告此文件的信息")
|
logging.Log().Warn("The file is too large and the processed audio is incomplete, " +
|
||||||
|
"please report to us about this file at https://github.com/unlock-music/cli/issues")
|
||||||
lenData = fullMaskLen
|
lenData = fullMaskLen
|
||||||
}
|
}
|
||||||
d.audio = make([]byte, lenData)
|
d.audio = make([]byte, lenData)
|
||||||
|
@ -41,7 +41,7 @@ var maskV2 []byte
|
|||||||
var fullMaskLen int
|
var fullMaskLen int
|
||||||
var initMaskOK = false
|
var initMaskOK = false
|
||||||
|
|
||||||
//todo: 根据需求解压Mask大小
|
//todo: decompress mask on demand
|
||||||
func initMask() {
|
func initMask() {
|
||||||
if initMaskOK {
|
if initMaskOK {
|
||||||
return
|
return
|
||||||
|
@ -125,4 +125,5 @@ func padOrTruncate(raw string, length int) string {
|
|||||||
func init() {
|
func init() {
|
||||||
// Kuwo Mp3/Flac
|
// Kuwo Mp3/Flac
|
||||||
common.RegisterDecoder("kwm", NewDecoder)
|
common.RegisterDecoder("kwm", NewDecoder)
|
||||||
|
common.RegisterDecoder("kwm", common.NewRawDecoder)
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,6 @@ func (d *Decoder) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo: 读取前进行检查长度,防止越界
|
|
||||||
func (d *Decoder) readKeyData() error {
|
func (d *Decoder) readKeyData() error {
|
||||||
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
||||||
return errors.New("invalid cover file offset")
|
return errors.New("invalid cover file offset")
|
||||||
|
@ -73,7 +73,7 @@ func init() {
|
|||||||
common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a"))
|
||||||
common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a"))
|
||||||
// QQ Music IOS Mp3
|
// QQ Music IOS Mp3
|
||||||
common.RegisterDecoder("tm0", common.DecoderFuncWithExt("mp3"))
|
common.RegisterDecoder("tm0", common.NewRawDecoder)
|
||||||
common.RegisterDecoder("tm3", common.DecoderFuncWithExt("mp3"))
|
common.RegisterDecoder("tm3", common.NewRawDecoder)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user