1
0
forked from um/cli
cli/algo/tm/tm.go

77 lines
1.5 KiB
Go
Raw Normal View History

2020-12-25 20:54:59 +00:00
package tm
import (
"bytes"
"errors"
2022-11-18 23:25:40 +00:00
2021-03-02 10:16:19 +00:00
"github.com/unlock-music/cli/algo/common"
2020-12-25 20:54:59 +00:00
)
2020-12-26 07:03:34 +00:00
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
2020-12-25 20:54:59 +00:00
type Decoder struct {
2020-12-26 07:03:34 +00:00
file []byte
audio []byte
headerMatch bool
audioExt string
2020-12-25 20:54:59 +00:00
}
func (d *Decoder) GetAudioData() []byte {
2020-12-26 07:03:34 +00:00
return d.audio
2020-12-25 20:54:59 +00:00
}
func (d *Decoder) GetAudioExt() string {
if d.audioExt != "" {
return "." + d.audioExt
}
return ""
2020-12-25 20:54:59 +00:00
}
func (d *Decoder) GetMeta() common.Meta {
return nil
}
func (d *Decoder) Validate() error {
if len(d.file) < 8 {
return errors.New("invalid file size")
}
2020-12-26 07:03:34 +00:00
if !bytes.Equal(magicHeader, d.file[:4]) {
2020-12-25 20:54:59 +00:00
return errors.New("not a valid tm file")
}
2020-12-26 07:03:34 +00:00
d.headerMatch = true
2020-12-25 20:54:59 +00:00
return nil
}
func (d *Decoder) Decode() error {
2020-12-26 07:03:34 +00:00
d.audio = d.file
if d.headerMatch {
for i := 0; i < 8; i++ {
d.audio[i] = replaceHeader[i]
}
d.audioExt = "m4a"
}
2020-12-25 20:54:59 +00:00
return nil
}
2020-12-26 07:47:10 +00:00
//goland:noinspection GoUnusedExportedFunction
2020-12-26 07:47:10 +00:00
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
}
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return func(file []byte) common.Decoder {
return &Decoder{file: file, audioExt: ext}
}
}
2020-12-26 07:47:10 +00:00
func init() {
// QQ Music IOS M4a
2021-11-11 15:43:20 +00:00
common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
2020-12-26 07:47:10 +00:00
// QQ Music IOS Mp3
2021-11-11 15:43:20 +00:00
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
2020-12-26 07:47:10 +00:00
}