forked from um/cli
Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
0b3ad0d97c | |||
c87204c78a | |||
3630fc0c78 | |||
b3f7cd33df | |||
c6fa777db1 | |||
cab705a130 | |||
f258e3e8f2 | |||
59f3759d48 | |||
db547002f9 | |||
05a1affa03 | |||
68e0c06246 | |||
e010d1b308 | |||
9b4e3b87ef | |||
1ccc507f61 | |||
119a8cf380 | |||
6179bcf0c7 | |||
c9ed772ce0 | |||
9c0acb794e | |||
bda32bc53e | |||
3f6566f7a9 | |||
1c5310ffae | |||
2941a9ac76 | |||
1835f9852a | |||
4f4d8db3d5 | |||
877f37f01e | |||
7b12f61a97 | |||
f6bc911125 | |||
|
9ddbd5f702 | ||
|
d480c9a2f8 | ||
907fe4143b | |||
4ad9a721f0 |
@ -149,12 +149,18 @@ func (d *Decoder) readMetaData() error {
|
||||
}
|
||||
|
||||
func (d *Decoder) readCoverData() error {
|
||||
bCoverCRC := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
|
||||
return fmt.Errorf("ncm read cover crc: %w", err)
|
||||
bCoverFrameLen := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %w", err)
|
||||
}
|
||||
|
||||
bCoverLen := make([]byte, 4) //
|
||||
coverFrameStartOffset, err := d.rd.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ncm fetch cover frame start offset: %w", err)
|
||||
}
|
||||
coverFrameLen := binary.LittleEndian.Uint32(bCoverFrameLen)
|
||||
|
||||
bCoverLen := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %w", err)
|
||||
}
|
||||
@ -166,7 +172,10 @@ func (d *Decoder) readCoverData() error {
|
||||
}
|
||||
d.cover = coverBuf
|
||||
|
||||
return nil
|
||||
offsetAudioData := coverFrameStartOffset + int64(coverFrameLen) + 4
|
||||
_, err = d.rd.Seek(offsetAudioData, io.SeekStart)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Decoder) parseMeta() error {
|
||||
|
@ -80,6 +80,39 @@ func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
|
||||
return deriveKey(buf)
|
||||
}
|
||||
|
||||
func OpenMMKV(mmkvPath string, key string, logger *zap.Logger) error {
|
||||
filePath, fileName := filepath.Split(mmkvPath)
|
||||
mgr, err := mmkv.NewManager(filepath.Dir(filePath))
|
||||
if err != nil {
|
||||
return fmt.Errorf("init mmkv manager: %w", err)
|
||||
}
|
||||
|
||||
// If `vaultKey` is empty, the key is ignored.
|
||||
streamKeyVault, err = mgr.OpenVaultCrypto(fileName, key)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("open mmkv vault: %w", err)
|
||||
}
|
||||
|
||||
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// /
|
||||
func readKeyFromMMKVCustom(mid string) ([]byte, error) {
|
||||
if streamKeyVault == nil {
|
||||
return nil, fmt.Errorf("mmkv vault not loaded")
|
||||
}
|
||||
|
||||
// get ekey from mmkv vault
|
||||
eKey, err := streamKeyVault.GetBytes(mid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get eKey error: %w", err)
|
||||
}
|
||||
return deriveKey(eKey)
|
||||
}
|
||||
|
||||
// / getRelativeMMKVDir get mmkv dir relative to file (legacy QQMusic for macOS behaviour)
|
||||
func getRelativeMMKVDir(file string) (string, error) {
|
||||
mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv")
|
||||
if _, err := os.Stat(mmkvDir); err != nil {
|
||||
@ -102,7 +135,7 @@ func getDefaultMMKVDir() (string, error) {
|
||||
|
||||
mmkvDir := filepath.Join(
|
||||
homeDir,
|
||||
"Library/Containers/com.tencent.QQMusicMac/Data", // todo: make configurable
|
||||
"Library/Containers/com.tencent.QQMusicMac/Data",
|
||||
"Library/Application Support/QQMusicMac/mmkv",
|
||||
)
|
||||
if _, err := os.Stat(mmkvDir); err != nil {
|
||||
|
@ -5,13 +5,12 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
"unlock-music.dev/cli/internal/sniff"
|
||||
)
|
||||
@ -145,6 +144,17 @@ func (d *Decoder) searchKey() (err error) {
|
||||
return d.readRawMetaQTag()
|
||||
case "STag":
|
||||
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||
case "cex\x00":
|
||||
footer, err := NewMusicExTag(d.raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.audioLen = fileSize - int(footer.TagSize)
|
||||
d.decodedKey, err = readKeyFromMMKVCustom(footer.MediaFileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
size := binary.LittleEndian.Uint32(suffixBuf)
|
||||
|
||||
@ -244,12 +254,20 @@ func init() {
|
||||
"6d3461", //QQ Music Weiyun M4a
|
||||
"776176", //QQ Music Weiyun Wav
|
||||
|
||||
"mgg", "mgg1", "mggl", //QQ Music New Ogg
|
||||
"mflac", "mflac0", "mflach", //QQ Music New Flac
|
||||
|
||||
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
|
||||
}
|
||||
for _, ext := range supportedExts {
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
}
|
||||
|
||||
// New ogg/flac:
|
||||
extraExtsCanHaveSuffix := []string{"mgg", "mflac"}
|
||||
// Mac also adds some extra suffix to ext:
|
||||
extraExtSuffix := []string{"0", "1", "a", "h", "l"}
|
||||
for _, ext := range extraExtsCanHaveSuffix {
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
for _, suffix := range extraExtSuffix {
|
||||
common.RegisterDecoder(ext+suffix, false, NewDecoder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
93
algo/qmc/qmc_footer_musicex.go
Normal file
93
algo/qmc/qmc_footer_musicex.go
Normal file
@ -0,0 +1,93 @@
|
||||
package qmc
|
||||
|
||||
import (
|
||||
bytes "bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MusicExTagV1 struct {
|
||||
SongID uint32 // Song ID
|
||||
Unknown1 uint32 // unused & unknown
|
||||
Unknown2 uint32 // unused & unknown
|
||||
MediaID string // Media ID
|
||||
MediaFileName string // real file name
|
||||
Unknown3 uint32 // unused; uninitialized memory?
|
||||
|
||||
// 16 byte at the end of tag.
|
||||
// TagSize should be respected when parsing.
|
||||
TagSize uint32 // 19.57: fixed value: 0xC0
|
||||
TagVersion uint32 // 19.57: fixed value: 0x01
|
||||
TagMagic []byte // fixed value "musicex\0" (8 bytes)
|
||||
}
|
||||
|
||||
func NewMusicExTag(f io.ReadSeeker) (*MusicExTagV1, error) {
|
||||
_, err := f.Seek(-16, io.SeekEnd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("musicex seek error: %w", err)
|
||||
}
|
||||
|
||||
buffer := make([]byte, 16)
|
||||
bytesRead, err := f.Read(buffer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get musicex error: %w", err)
|
||||
}
|
||||
if bytesRead != 16 {
|
||||
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, 16)
|
||||
}
|
||||
|
||||
tag := &MusicExTagV1{
|
||||
TagSize: binary.LittleEndian.Uint32(buffer[0x00:0x04]),
|
||||
TagVersion: binary.LittleEndian.Uint32(buffer[0x04:0x08]),
|
||||
TagMagic: buffer[0x04:0x0C],
|
||||
}
|
||||
|
||||
if !bytes.Equal(tag.TagMagic, []byte("musicex\x00")) {
|
||||
return nil, errors.New("MusicEx magic mismatch")
|
||||
}
|
||||
if tag.TagVersion != 1 {
|
||||
return nil, errors.New(fmt.Sprintf("unsupported musicex tag version. expecting 1, got %d", tag.TagVersion))
|
||||
}
|
||||
|
||||
if tag.TagSize < 0xC0 {
|
||||
return nil, errors.New(fmt.Sprintf("unsupported musicex tag size. expecting at least 0xC0, got 0x%02x", tag.TagSize))
|
||||
}
|
||||
|
||||
buffer = make([]byte, tag.TagSize)
|
||||
bytesRead, err = f.Read(buffer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if uint32(bytesRead) != tag.TagSize {
|
||||
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, tag.TagSize)
|
||||
}
|
||||
|
||||
tag.SongID = binary.LittleEndian.Uint32(buffer[0x00:0x04])
|
||||
tag.Unknown1 = binary.LittleEndian.Uint32(buffer[0x04:0x08])
|
||||
tag.Unknown2 = binary.LittleEndian.Uint32(buffer[0x08:0x0C])
|
||||
tag.MediaID = readUnicodeTagName(buffer[0x0C:], 30*2)
|
||||
tag.MediaFileName = readUnicodeTagName(buffer[0x48:], 50*2)
|
||||
tag.Unknown3 = binary.LittleEndian.Uint32(buffer[0xAC:0xB0])
|
||||
return tag, nil
|
||||
}
|
||||
|
||||
// readUnicodeTagName reads a buffer to maxLen.
|
||||
// reconstruct text by skipping alternate char (ascii chars encoded in UTF-16-LE),
|
||||
// until finding a zero or reaching maxLen.
|
||||
func readUnicodeTagName(buffer []byte, maxLen int) string {
|
||||
builder := strings.Builder{}
|
||||
|
||||
for i := 0; i < maxLen; i += 2 {
|
||||
chr := buffer[i]
|
||||
if chr != 0 {
|
||||
builder.WriteByte(chr)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
@ -23,7 +23,7 @@ import (
|
||||
_ "unlock-music.dev/cli/algo/kgm"
|
||||
_ "unlock-music.dev/cli/algo/kwm"
|
||||
_ "unlock-music.dev/cli/algo/ncm"
|
||||
_ "unlock-music.dev/cli/algo/qmc"
|
||||
"unlock-music.dev/cli/algo/qmc"
|
||||
_ "unlock-music.dev/cli/algo/tm"
|
||||
_ "unlock-music.dev/cli/algo/xiami"
|
||||
_ "unlock-music.dev/cli/algo/ximalaya"
|
||||
@ -33,7 +33,7 @@ import (
|
||||
"unlock-music.dev/cli/internal/utils"
|
||||
)
|
||||
|
||||
var AppVersion = "v0.0.6"
|
||||
var AppVersion = "v0.2.2"
|
||||
|
||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
||||
|
||||
@ -50,6 +50,8 @@ func main() {
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
||||
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (.crc file also required)", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
|
||||
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
|
||||
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
|
||||
@ -130,6 +132,15 @@ func appMain(c *cli.Context) (err error) {
|
||||
return errors.New("output should be a writable directory")
|
||||
}
|
||||
|
||||
if mmkv := c.String("qmc-mmkv"); mmkv != "" {
|
||||
// If key is not set, the mmkv vault will be treated as unencrypted.
|
||||
key := c.String("qmc-mmkv-key")
|
||||
err := qmc.OpenMMKV(mmkv, key, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
proc := &processor{
|
||||
outputDir: output,
|
||||
skipNoopDecoder: c.Bool("skip-noop"),
|
||||
@ -243,7 +254,20 @@ func (p *processor) processFile(filePath string) error {
|
||||
if len(allDec) == 0 {
|
||||
logger.Fatal("skipping while no suitable decoder")
|
||||
}
|
||||
return p.process(filePath, allDec)
|
||||
|
||||
if err := p.process(filePath, allDec); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if source file need to be removed
|
||||
if p.removeSource {
|
||||
err := os.RemoveAll(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("source file removed after success conversion", zap.String("source", filePath))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error {
|
||||
@ -352,8 +376,6 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
if _, err := io.Copy(outFile, audio); err != nil {
|
||||
return err
|
||||
}
|
||||
outFile.Close()
|
||||
|
||||
} else {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
@ -363,16 +385,6 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
}
|
||||
}
|
||||
|
||||
// if source file need to be removed
|
||||
if p.removeSource {
|
||||
err := os.RemoveAll(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
} else {
|
||||
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
8
go.mod
8
go.mod
@ -10,10 +10,10 @@ require (
|
||||
github.com/samber/lo v1.39.0
|
||||
github.com/urfave/cli/v2 v2.27.1
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/crypto v0.22.0
|
||||
golang.org/x/crypto v0.26.0
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
|
||||
golang.org/x/text v0.14.0
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a
|
||||
golang.org/x/text v0.17.0
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b
|
||||
)
|
||||
|
||||
require (
|
||||
@ -21,6 +21,6 @@ require (
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -26,14 +26,22 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a h1:UW0sxgwsfGGC/SrKvvAbZ4HZyOQ3fqs8qr3lBxG6Fzo=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b h1:VIJ0mDqj0OgX1ZvL9gbAH8kkqyrDlpVt5yUeGYSJ1/s=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||
|
Loading…
Reference in New Issue
Block a user