Compare commits

..

No commits in common. "main" and "1.5.0" have entirely different histories.
main ... 1.5.0

9 changed files with 76 additions and 258 deletions

21
LICENSE
View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2018-present TaurusXin and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -2,73 +2,26 @@
基于 https://github.com/taurusxin/ncmdump 的 Golang 移植版
支持网易云音乐最新的 3.x 版本,但需要注意:从 3. x开始的某些网易云音乐版本不再在 ncm 文件中内置封面图片,本项目支持从网易服务器上自动下载对应歌曲的封面图并写入到最终的音乐文件中
你也可以去 https://git.taurusxin.com/taurusxin/ncmdump-gui 下载基于本项目的 gui 可视化图形应用,只需简单点击即可自动转换。
## 安装
你可以使用去 [releases](https://git.taurusxin.com/taurusxin/ncmdump-go/releases/latest) 下载最新版预编译好的二进制文件,或者你也可以用包管理器来安装
```shell
# Windows Scoop
scoop bucket add taurusxin https://git.taurusxin.com/taurusxin/scoop-bucket.git # 添加 scoop 源
scoop install ncmdump-go # 安装 ncmdump-go
# macOS & Linux 之后会支持
```
支持网易云音乐最新的 3.x 版本
## 使用方法
使用 `-h``--help` 参数来打印帮助
```shell
ncmdump-go -h
# 处理单个或多个文件
ncmdump test1.ncm test2.ncm...
# 处理 Music 文件夹下的所有文件
ncmdump -d Music
```
使用 `-v``--version` 参数来打印版本信息
```shell
ncmdump-go -v
```
处理单个或多个文件
```shell
ncmdump-go 1.ncm 2.ncm...
```
使用 `-d` 参数来指定一个文件夹,对文件夹下的所有以 ncm 为扩展名的文件进行批量处理
```shell
ncmdump-go -d source_dir
```
使用 `-r` 配合 `-d` 参数来递归处理文件夹下的所有以 ncm 为扩展名的文件
```shell
ncmdump-go -d source_dir -r
```
使用 `-o` 参数来指定输出目录,将转换后的文件输出到指定目录,该参数支持与 `-r` 参数一起使用
```shell
# 处理单个或多个文件并输出到指定目录
ncmdump-go 1.ncm 2.ncm -o output_dir
# 处理文件夹下的所有以 ncm 为扩展名并输出到指定目录,不包含子文件夹
ncmdump-go -d source_dir -o output_dir
# 递归处理文件夹并输出到指定目录,并保留目录结构
ncmdump-go -d source_dir -o output_dir -r
```
注意:网易云音乐从 3.0 版本开始不再在 ncm 文件中嵌入封面图片,本工具支持从网易服务器上自动下载对应歌曲的封面图并写入到最终的音乐文件中
## 开发
使用 go module 下载 ncmdump-go 包
```shell
go get -u git.taurusxin.com/taurusxin/ncmdump-go
go get -u github.com/taurusxin/ncmdump-go
```
导入并使用
@ -78,7 +31,7 @@ package main
import (
"fmt"
"git.taurusxin.com/taurusxin/ncmdump-go/ncmcrypt"
"github.com/taurusxin/ncmdump-go/ncmcrypt"
)
func main() {

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
VERSION=1.7.4
VERSION=1.5.0
# Clean up the build directory
rm -rf build
@ -8,30 +8,30 @@ mkdir build
# Linux amd64
echo "Building for Linux amd64..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump-go git.taurusxin.com/taurusxin/ncmdump-go
tar zcf build/ncmdump-go_linux_amd64_$VERSION.tar.gz -C build ncmdump-go
rm build/ncmdump-go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump github.com/taurusxin/ncmdump-go
tar zcf build/ncmdump_linux_amd64_$VERSION.tar.gz -C build ncmdump
rm build/ncmdump
# Linux arm64
echo "Building for Linux arm64..."
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o build/ncmdump-go git.taurusxin.com/taurusxin/ncmdump-go
tar zcf build/ncmdump-go_linux_arm64_$VERSION.tar.gz -C build ncmdump-go
rm build/ncmdump-go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o build/ncmdump github.com/taurusxin/ncmdump-go
tar zcf build/ncmdump_linux_arm64_$VERSION.tar.gz -C build ncmdump
rm build/ncmdump
# macOS amd64
echo "Building for macOS amd64..."
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump-go git.taurusxin.com/taurusxin/ncmdump-go
tar zcf build/ncmdump-go_darwin_amd64_$VERSION.tar.gz -C build ncmdump-go
rm build/ncmdump-go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump github.com/taurusxin/ncmdump-go
tar zcf build/ncmdump_darwin_amd64_$VERSION.tar.gz -C build ncmdump
rm build/ncmdump
# macOS arm64
echo "Building for macOS arm64..."
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o build/ncmdump-go git.taurusxin.com/taurusxin/ncmdump-go
tar zcf build/ncmdump-go_darwin_arm64_$VERSION.tar.gz -C build ncmdump-go
rm build/ncmdump-go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o build/ncmdump github.com/taurusxin/ncmdump-go
tar zcf build/ncmdump_darwin_arm64_$VERSION.tar.gz -C build ncmdump
rm build/ncmdump
# Windows amd64
echo "Building for Windows amd64..."
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump-go.exe git.taurusxin.com/taurusxin/ncmdump-go
zip -q -j build/ncmdump-go_windows_amd64_$VERSION.zip ./build/ncmdump-go.exe
rm build/ncmdump-go.exe
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o build/ncmdump.exe github.com/taurusxin/ncmdump-go
zip -q -j build/ncmdump_windows_amd64_$VERSION.zip ./build/ncmdump.exe
rm build/ncmdump.exe

13
go.mod
View File

@ -1,19 +1,18 @@
module git.taurusxin.com/taurusxin/ncmdump-go
module github.com/taurusxin/ncmdump-go
go 1.23.0
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/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
)
require github.com/go-flac/flacpicture v0.3.0
require github.com/TwiN/go-color v1.4.1
require (
github.com/tidwall/match v1.1.1 // indirect

2
go.sum
View File

@ -4,8 +4,6 @@ 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=

108
main.go
View File

@ -2,27 +2,21 @@ package main
import (
"fmt"
"git.taurusxin.com/taurusxin/ncmdump-go/ncmcrypt"
"git.taurusxin.com/taurusxin/ncmdump-go/utils"
"github.com/taurusxin/ncmdump-go/ncmcrypt"
"github.com/taurusxin/ncmdump-go/utils"
"os"
"path/filepath"
flag "github.com/spf13/pflag"
)
func processFile(filePath string, outputDir string) error {
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
return nil
}
// process the file
func processFile(filePath string) error {
currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath)
if err != nil {
utils.ErrorPrintfln("Reading '%s' failed: %s", filePath, err.Error())
return err
}
dump, err := currentFile.Dump(outputDir)
dump, err := currentFile.Dump(filepath.Dir(filePath))
if err != nil {
utils.ErrorPrintfln("Processing '%s' failed: %s", filePath, err.Error())
return err
@ -39,13 +33,10 @@ func processFile(filePath string, outputDir string) error {
}
func main() {
var sourceDir string
var outputDir string
var folderPath string
showHelp := flag.BoolP("help", "h", false, "Display help message")
showVersion := flag.BoolP("version", "v", false, "Display version information")
processRecursive := flag.BoolP("recursive", "r", false, "Process all files in the directory recursively")
flag.StringVarP(&outputDir, "output", "o", "", "Output directory for the dump files")
flag.StringVarP(&sourceDir, "dir", "d", "", "Process all files in the directory")
flag.StringVarP(&folderPath, "dir", "d", "", "Process all files in the directory")
flag.Parse()
if len(os.Args) == 1 {
@ -59,59 +50,27 @@ func main() {
}
if *showVersion {
fmt.Println("ncmdump version 1.7.4")
fmt.Println("ncmdump version 1.5.0")
os.Exit(0)
}
if !flag.Lookup("dir").Changed && sourceDir == "" && len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
if flag.Lookup("recursive").Changed && !flag.Lookup("dir").Changed {
utils.ErrorPrintfln("The -r option can only be used with the -d option")
os.Exit(1)
}
outputDirSpecified := flag.Lookup("output").Changed
if outputDirSpecified {
if utils.PathExists(outputDir) {
if !utils.IsDir(outputDir) {
utils.ErrorPrintfln("Output directory '%s' is not valid.", outputDir)
os.Exit(1)
}
} else {
_ = os.MkdirAll(outputDir, os.ModePerm)
}
}
if sourceDir != "" {
if !utils.IsDir(sourceDir) {
utils.ErrorPrintfln("The source directory '%s' is not valid.", sourceDir)
os.Exit(1)
}
if *processRecursive {
_ = filepath.WalkDir(sourceDir, func(p string, d os.DirEntry, err_ error) error {
if !outputDirSpecified {
outputDir = sourceDir
}
relativePath := utils.GetRelativePath(sourceDir, p)
destinationPath := filepath.Join(outputDir, relativePath)
if utils.IsRegularFile(p) {
parentDir := filepath.Dir(destinationPath)
_ = os.MkdirAll(parentDir, os.ModePerm)
_ = processFile(p, parentDir)
}
return nil
})
} else {
// dump files in the folder
files, err := os.ReadDir(sourceDir)
if folderPath != "" {
// check if the folder exists
info, err := os.Stat(folderPath)
if err != nil {
utils.ErrorPrintfln("Unable to read directory: '%s'", sourceDir)
utils.ErrorPrintfln("Unable to access directory: '%s'", folderPath)
os.Exit(1)
}
if !info.IsDir() {
utils.ErrorPrintfln("Not a directory: %s", folderPath)
os.Exit(1)
}
// dump files in the folder
files, err := os.ReadDir(folderPath)
if err != nil {
utils.ErrorPrintfln("Unable to read directory: '%s'", folderPath)
os.Exit(1)
}
@ -120,25 +79,26 @@ func main() {
continue
}
filePath := filepath.Join(sourceDir, file.Name())
if outputDirSpecified {
_ = processFile(filePath, outputDir)
} else {
_ = processFile(filePath, sourceDir)
filePath := filepath.Join(folderPath, file.Name())
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
continue
}
err = processFile(filePath)
if err != nil {
continue
}
}
} else {
// process files from args
// dump file from args
for _, filePath := range flag.Args() {
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
continue
}
if outputDirSpecified {
_ = processFile(filePath, outputDir)
} else {
_ = processFile(filePath, sourceDir)
err := processFile(filePath)
if err != nil {
continue
}
}
}

View File

@ -5,11 +5,10 @@ import (
"encoding/base64"
"encoding/binary"
"fmt"
"git.taurusxin.com/taurusxin/ncmdump-go/utils"
"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"
"net/http"
"os"
@ -162,8 +161,7 @@ func (ncm *NeteaseCloudMusic) Dump(targetDir string) (bool, error) {
// FixMetadata will fix the missing metadata for target music file, the source of the metadata comes from origin ncm file.
// Since NeteaseCloudMusic version 3.0, the album cover image is no longer embedded in the ncm file. If the parameter is true, it means downloading the image from the NetEase server and embedding it into the target music file (network connection required)
func (ncm *NeteaseCloudMusic) FixMetadata(fetchAlbumImageFromRemote bool) (bool, error) {
// only fetch album image from remote when it's not embedded in the ncm file
if len(ncm.mImageData) <= 0 && fetchAlbumImageFromRemote {
if fetchAlbumImageFromRemote {
// get the album pic from url
resp, err := http.Get(ncm.mAlbumPicUrl)
if err != nil {
@ -185,7 +183,6 @@ func (ncm *NeteaseCloudMusic) FixMetadata(fetchAlbumImageFromRemote bool) (bool,
return false, err
}
defer audioFile.Close()
audioFile.SetDefaultEncoding(id3v2.EncodingUTF8)
audioFile.SetTitle(ncm.mMetadata.mName)
audioFile.SetArtist(ncm.mMetadata.mArtist)
audioFile.SetAlbum(ncm.mMetadata.mAlbum)
@ -219,43 +216,12 @@ func (ncm *NeteaseCloudMusic) FixMetadata(fetchAlbumImageFromRemote bool) (bool,
pictureMeta := pic.Marshal()
audioFile.Meta = append(audioFile.Meta, &pictureMeta)
}
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
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)),
}
}
}
if cmts == nil && cmtIdx > 0 {
cmts = flacvorbis.New()
}
// flac 可能自带元数据 当且仅当没有该项时才向目标添加元数据
if res, _ := cmts.Get(flacvorbis.FIELD_TITLE); len(res) == 0 {
_ = cmts.Add(flacvorbis.FIELD_TITLE, ncm.mMetadata.mName)
}
if res, _ := cmts.Get(flacvorbis.FIELD_ARTIST); len(res) == 0 {
_ = cmts.Add(flacvorbis.FIELD_ARTIST, ncm.mMetadata.mArtist)
}
if res, _ := cmts.Get(flacvorbis.FIELD_ALBUM); len(res) == 0 {
_ = 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)
}
audioFile.Meta = append(audioFile.Meta, generalMeta)
err = audioFile.Save(ncm.mDumpFilePath)
if err != nil {
return false, err
}

View File

@ -1,7 +1,6 @@
package utils
import (
"os"
"path/filepath"
"strings"
)
@ -10,39 +9,3 @@ func ReplaceExtension(filepathStr, newExt string) string {
ext := filepath.Ext(filepathStr)
return strings.TrimSuffix(filepathStr, ext) + newExt
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func GetRelativePath(from, to string) string {
rel, err := filepath.Rel(from, to)
if err != nil {
return ""
}
return rel
}
func IsRegularFile(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.Mode().IsRegular()
}