feat: package name; more params

This commit is contained in:
TaurusXin 2024-09-15 21:37:11 +08:00
parent 3f1a58f2aa
commit 0c9fc960ff
Signed by: taurusxin
GPG Key ID: C334DCA04AC2D2CC
5 changed files with 79 additions and 29 deletions

View File

@ -14,4 +14,50 @@ ncmdump test1.ncm test2.ncm...
ncmdump -d Music ncmdump -d Music
``` ```
注意:网易云音乐从 3.0 版本开始不再在 ncm 文件中嵌入封面图片,本工具支持从网易服务器上自动下载对应歌曲的封面图并写入到最终的音乐文件中 注意:网易云音乐从 3.0 版本开始不再在 ncm 文件中嵌入封面图片,本工具支持从网易服务器上自动下载对应歌曲的封面图并写入到最终的音乐文件中
## 开发
使用 go module 下载 ncmdump-go 包
```shell
go get -u github.com/taurusxin/ncmdump-go
```
导入并使用
```go
package main
import (
"fmt"
"github.com/taurusxin/ncmdump-go/ncmcrypt"
)
func main() {
filePath := "test.ncm"
// 创建实例
ncm, err := ncmcrypt.NewNeteaseCloudMusic(filePath)
if err != nil {
fmt.Printf("Reading '%s' failed: '%s'\n", filePath, err.Error())
return
}
// 转换格式,若目标文件夹为空,则保存在原目录
dumpResult, err := ncm.Dump("")
if err != nil {
fmt.Printf("Processing '%s' failed: '%s'\n", filePath, err.Error())
}
if dumpResult {
// 使用源文件的元数据修补转换后的音乐文件
// 注意:自网易云音乐 3.0 版本开始ncm 文件中不再内嵌专辑封面图片,参数若为 true 则表示从网易服务器上下载图片并嵌入到目标音乐文件(需要联网)
metadata, err := ncm.FixMetadata(true)
if !metadata {
fmt.Printf("Fix metadata for '%s' failed: '%s'", filePath, err.Error())
}
fmt.Printf("'%s' -> '%s'\n", filePath, ncm.GetDumpFilePath())
}
}
```

17
go.mod
View File

@ -1,13 +1,18 @@
module ncmdump module github.com/taurusxin/ncmdump-go
go 1.23.0 go 1.23.0
require github.com/tidwall/gjson v1.17.3 require github.com/tidwall/gjson v1.17.3
require github.com/go-flac/go-flac v1.0.0
require github.com/spf13/pflag v1.0.5 require github.com/go-flac/go-flac v1.0.0
require github.com/bogem/id3v2/v2 v2.1.4
require github.com/go-flac/flacpicture v0.3.0 require github.com/spf13/pflag v1.0.5
require github.com/TwiN/go-color v1.4.1
require github.com/bogem/id3v2/v2 v2.1.4
require github.com/go-flac/flacpicture v0.3.0
require github.com/TwiN/go-color v1.4.1
require ( require (
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect

4
go.sum
View File

@ -2,14 +2,10 @@ github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc=
github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s= github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s=
github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI= 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/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY=
github.com/frolovo22/tag v0.0.2 h1:gFv5P5nqE7purEipbKT7X/OjP286nx5gA30mjt/4SgA=
github.com/frolovo22/tag v0.0.2/go.mod h1:Bt1H06v6RQFTrplGixhtUXVzHA/RpmhGEVxC7wqWGIw=
github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I= 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/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI=
github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY= 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/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8=
github.com/go-flac/go-flac/v2 v2.0.1 h1:1zilNkbmmpK9DLsz2NbjLHG8avOmthYqUfVc9YKB/Ps=
github.com/go-flac/go-flac/v2 v2.0.1/go.mod h1:hvgeR2hElLbwk0Q1/vMazIDmIc2LAFSd9Bx/Fk6ViKo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94= github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=

View File

@ -2,8 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"ncmdump/ncmcrypt" "github.com/taurusxin/ncmdump-go/ncmcrypt"
"ncmdump/utils" "github.com/taurusxin/ncmdump-go/utils"
"os" "os"
"path/filepath" "path/filepath"
@ -22,7 +22,7 @@ func processFile(filePath string) error {
return err return err
} }
if dump { if dump {
metadata, err := currentFile.FixMetadata() metadata, err := currentFile.FixMetadata(true)
if !metadata { if !metadata {
utils.WarningPrintfln("Fix metadata for '%s' failed: '%s'", filePath, err.Error()) utils.WarningPrintfln("Fix metadata for '%s' failed: '%s'", filePath, err.Error())
return err return err

View File

@ -8,8 +8,8 @@ import (
"github.com/bogem/id3v2/v2" "github.com/bogem/id3v2/v2"
"github.com/go-flac/flacpicture" "github.com/go-flac/flacpicture"
"github.com/go-flac/go-flac" "github.com/go-flac/go-flac"
"github.com/taurusxin/ncmdump-go/utils"
"io" "io"
"ncmdump/utils"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -157,7 +157,23 @@ func (ncm *NeteaseCloudMusic) Dump(targetDir string) (bool, error) {
return true, nil return true, nil
} }
func (ncm *NeteaseCloudMusic) FixMetadata() (bool, error) { func (ncm *NeteaseCloudMusic) FixMetadata(fetchAlbumImageFromRemote bool) (bool, error) {
if fetchAlbumImageFromRemote {
// get the album pic from url
resp, err := http.Get(ncm.mAlbumPicUrl)
if err != nil {
return false, err
}
if resp != nil {
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
ncm.mImageData = bodyBytes
}
}
}
if ncm.mFormat == Mp3 { if ncm.mFormat == Mp3 {
audioFile, err := id3v2.Open(ncm.mDumpFilePath, id3v2.Options{Parse: true}) audioFile, err := id3v2.Open(ncm.mDumpFilePath, id3v2.Options{Parse: true})
if err != nil { if err != nil {
@ -323,19 +339,6 @@ func NewNeteaseCloudMusic(filePath string) (*NeteaseCloudMusic, error) {
if coverFrameDataLen > 0 { if coverFrameDataLen > 0 {
ncm.read(&ncm.mImageData, coverFrameDataLen) ncm.read(&ncm.mImageData, coverFrameDataLen)
} else {
// get the album pic from url
resp, err := http.Get(ncm.mAlbumPicUrl)
if err != nil {
}
if resp != nil {
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
}
ncm.mImageData = bodyBytes
}
}
} }
ncm.mFileStream.Seek(int64(coverFrameLenInt)-int64(coverFrameDataLen), 1) ncm.mFileStream.Seek(int64(coverFrameLenInt)-int64(coverFrameDataLen), 1)