From be81c22e4912b0b9820ae7b4cb79ef8e1aaae3da Mon Sep 17 00:00:00 2001 From: TaurusXin Date: Wed, 23 Oct 2024 19:17:51 +0800 Subject: [PATCH] fix: incorrect metadata structure --- go.mod | 1 + go.sum | 2 ++ ncmcrypt/ncmcrypt.go | 33 +++++++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index fc0b713..f7c1e61 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require github.com/bogem/id3v2/v2 v2.1.4 require ( github.com/TwiN/go-color v1.4.1 github.com/go-flac/flacpicture v0.3.0 + github.com/go-flac/flacvorbis v0.2.0 github.com/go-flac/go-flac v1.0.0 ) diff --git a/go.sum b/go.sum index 0b27785..3eb35a8 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI= github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY= github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I= github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI= +github.com/go-flac/flacvorbis v0.2.0 h1:KH0xjpkNTXFER4cszH4zeJxYcrHbUobz/RticWGOESs= +github.com/go-flac/flacvorbis v0.2.0/go.mod h1:uIysHOtuU7OLGoCRG92bvnkg7QEqHx19qKRV6K1pBrI= github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY= github.com/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/ncmcrypt/ncmcrypt.go b/ncmcrypt/ncmcrypt.go index 9158a74..20a4bc7 100644 --- a/ncmcrypt/ncmcrypt.go +++ b/ncmcrypt/ncmcrypt.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/bogem/id3v2/v2" "github.com/go-flac/flacpicture" + "github.com/go-flac/flacvorbis" "github.com/go-flac/go-flac" "github.com/taurusxin/ncmdump-go/utils" "io" @@ -217,12 +218,36 @@ func (ncm *NeteaseCloudMusic) FixMetadata(fetchAlbumImageFromRemote bool) (bool, pictureMeta := pic.Marshal() audioFile.Meta = append(audioFile.Meta, &pictureMeta) } - generalMeta := &flac.MetaDataBlock{ - Type: flac.VorbisComment, - Data: []byte(fmt.Sprintf("title=%s\nartist=%s\nalbum=%s", ncm.mMetadata.mName, ncm.mMetadata.mArtist, ncm.mMetadata.mAlbum)), + + var cmts *flacvorbis.MetaDataBlockVorbisComment + var cmtIdx int + for idx, meta := range audioFile.Meta { + if meta.Type == flac.VorbisComment { + cmts, err = flacvorbis.ParseFromMetaDataBlock(*meta) + cmtIdx = idx + if err != nil { + return false, err + } + } } - audioFile.Meta = append(audioFile.Meta, generalMeta) + if cmts == nil && cmtIdx > 0 { + cmts = flacvorbis.New() + } + + _ = cmts.Add(flacvorbis.FIELD_TITLE, ncm.mMetadata.mName) + _ = cmts.Add(flacvorbis.FIELD_ARTIST, ncm.mMetadata.mArtist) + _ = cmts.Add(flacvorbis.FIELD_ALBUM, ncm.mMetadata.mAlbum) + + cmtsmeta := cmts.Marshal() + + if cmtIdx > 0 { + audioFile.Meta[cmtIdx] = &cmtsmeta + } else { + audioFile.Meta = append(audioFile.Meta, &cmtsmeta) + } + err = audioFile.Save(ncm.mDumpFilePath) + if err != nil { return false, err }