18 Commits

Author SHA1 Message Date
鲁树人
0d071a82be feat #99: support recursive processDir 2024-10-08 22:20:12 +01:00
鲁树人
b8e6196248 chore: bump version to 0.2.3 2024-10-08 22:10:15 +01:00
鲁树人
1323fb9e1a Merge pull request '修正 #59 #99 转换后移除原始文件处理' (#100) from fix-59-99-remove-source into main
Reviewed-on: https://git.unlock-music.dev/um/cli/pulls/100
2024-10-08 21:06:44 +00:00
鲁树人
36df203bdd fix: record last error when calling processDir 2024-10-08 22:03:29 +01:00
鲁树人
2afc232eb1 fix: don't force exit when processFile fails. 2024-10-08 21:59:47 +01:00
鲁树人
2abdd47c9c fix: typo 2024-10-08 21:59:27 +01:00
鲁树人
8b59bc026d chore: ignore exe files 2024-10-08 21:59:19 +01:00
鲁树人
91855f8f5b fix #99: default output dir to where the input file is 2024-10-08 21:52:23 +01:00
鲁树人
7edd326b95 fix #59: processDir should call processFile instead. 2024-10-08 21:47:10 +01:00
鲁树人
0b3ad0d97c chore: bump version 2024-09-12 15:08:56 +01:00
鲁树人
c87204c78a fix #96: ncm file parsing when image cover 2 is not empty 2024-09-12 15:08:04 +01:00
鲁树人
3630fc0c78 Merge pull request '修正问题' (#95) from CLI-fix-59-71 into main
Reviewed-on: https://git.unlock-music.dev/um/cli/pulls/95
2024-09-04 22:14:02 +00:00
鲁树人
b3f7cd33df chore: update vulnerable deps 2024-09-04 23:09:43 +01:00
鲁树人
c6fa777db1 chore: bump version to v0.2.1 2024-09-04 23:06:24 +01:00
鲁树人
cab705a130 fix: typo in qmc-mmkv flag description 2024-09-04 23:03:35 +01:00
鲁树人
f258e3e8f2 fix: remove file after completion #59 2024-09-04 22:58:11 +01:00
鲁树人
59f3759d48 fix: add more extensions (Mac) #71 2024-09-04 22:47:02 +01:00
鲁树人
db547002f9 chore: update go-mmkv module hash 2024-09-01 19:09:34 +01:00
6 changed files with 93 additions and 55 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.idea .idea
/dist /dist
*.exe

View File

@@ -149,12 +149,18 @@ func (d *Decoder) readMetaData() error {
} }
func (d *Decoder) readCoverData() error { func (d *Decoder) readCoverData() error {
bCoverCRC := make([]byte, 4) bCoverFrameLen := make([]byte, 4)
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil { if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil {
return fmt.Errorf("ncm read cover crc: %w", err) 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 { if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
return fmt.Errorf("ncm read cover length: %w", err) return fmt.Errorf("ncm read cover length: %w", err)
} }
@@ -166,7 +172,10 @@ func (d *Decoder) readCoverData() error {
} }
d.cover = coverBuf d.cover = coverBuf
return nil offsetAudioData := coverFrameStartOffset + int64(coverFrameLen) + 4
_, err = d.rd.Seek(offsetAudioData, io.SeekStart)
return err
} }
func (d *Decoder) parseMeta() error { func (d *Decoder) parseMeta() error {

View File

@@ -254,12 +254,20 @@ func init() {
"6d3461", //QQ Music Weiyun M4a "6d3461", //QQ Music Weiyun M4a
"776176", //QQ Music Weiyun Wav "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 "mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
} }
for _, ext := range supportedExts { for _, ext := range supportedExts {
common.RegisterDecoder(ext, false, NewDecoder) 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)
}
}
} }

View File

@@ -8,6 +8,7 @@ import (
"io" "io"
"os" "os"
"os/signal" "os/signal"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
@@ -33,7 +34,7 @@ import (
"unlock-music.dev/cli/internal/utils" "unlock-music.dev/cli/internal/utils"
) )
var AppVersion = "v0.0.6" var AppVersion = "v0.2.3"
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
@@ -50,7 +51,7 @@ func main() {
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false}, &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: "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", 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.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: "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: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
@@ -84,6 +85,11 @@ func printSupportedExtensions() {
} }
func appMain(c *cli.Context) (err error) { func appMain(c *cli.Context) (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
}
if c.Bool("supported-ext") { if c.Bool("supported-ext") {
printSupportedExtensions() printSupportedExtensions()
return nil return nil
@@ -92,10 +98,7 @@ func appMain(c *cli.Context) (err error) {
if input == "" { if input == "" {
switch c.Args().Len() { switch c.Args().Len() {
case 0: case 0:
input, err = os.Getwd() input = cwd
if err != nil {
return err
}
case 1: case 1:
input = c.Args().Get(0) input = c.Args().Get(0)
default: default:
@@ -104,22 +107,20 @@ func appMain(c *cli.Context) (err error) {
} }
output := c.String("output") output := c.String("output")
if output == "" {
var err error
output, err = os.Getwd()
if err != nil {
return err
}
if input == output {
return errors.New("input and output path are same")
}
}
inputStat, err := os.Stat(input) inputStat, err := os.Stat(input)
if err != nil { if err != nil {
return err return err
} }
if output == "" {
// Default to where the input is
if inputStat.IsDir() {
output = input
} else {
output = path.Dir(input)
}
}
outputStat, err := os.Stat(output) outputStat, err := os.Stat(output)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
@@ -142,6 +143,7 @@ func appMain(c *cli.Context) (err error) {
} }
proc := &processor{ proc := &processor{
inputDir: input,
outputDir: output, outputDir: output,
skipNoopDecoder: c.Bool("skip-noop"), skipNoopDecoder: c.Bool("skip-noop"),
removeSource: c.Bool("remove-source"), removeSource: c.Bool("remove-source"),
@@ -150,8 +152,8 @@ func appMain(c *cli.Context) (err error) {
} }
if inputStat.IsDir() { if inputStat.IsDir() {
wacthDir := c.Bool("watch") watchDir := c.Bool("watch")
if !wacthDir { if !watchDir {
return proc.processDir(input) return proc.processDir(input)
} else { } else {
return proc.watchDir(input) return proc.watchDir(input)
@@ -163,6 +165,7 @@ func appMain(c *cli.Context) (err error) {
} }
type processor struct { type processor struct {
inputDir string
outputDir string outputDir string
skipNoopDecoder bool skipNoopDecoder bool
@@ -230,31 +233,47 @@ func (p *processor) processDir(inputDir string) error {
if err != nil { if err != nil {
return err return err
} }
var lastError error = nil
for _, item := range items { for _, item := range items {
if item.IsDir() {
continue
}
filePath := filepath.Join(inputDir, item.Name()) filePath := filepath.Join(inputDir, item.Name())
allDec := common.GetDecoder(filePath, p.skipNoopDecoder) if item.IsDir() {
if len(allDec) == 0 { if err = p.processDir(filePath); err != nil {
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name())) lastError = err
}
continue continue
} }
if err := p.process(filePath, allDec); err != nil { if err := p.processFile(filePath); err != nil {
lastError = err
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
} }
} }
if lastError != nil {
return fmt.Errorf("last error: %w", lastError)
}
return nil return nil
} }
func (p *processor) processFile(filePath string) error { func (p *processor) processFile(filePath string) error {
allDec := common.GetDecoder(filePath, p.skipNoopDecoder) allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 { if len(allDec) == 0 {
logger.Fatal("skipping while no suitable decoder") return errors.New("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 { func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error {
@@ -341,8 +360,13 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
} }
} }
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
if err != nil {
return fmt.Errorf("get relative dir failed: %w", err)
}
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile)) inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt) outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
if !p.overwriteOutput { if !p.overwriteOutput {
_, err := os.Stat(outPath) _, err := os.Stat(outPath)
@@ -363,8 +387,6 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
if _, err := io.Copy(outFile, audio); err != nil { if _, err := io.Copy(outFile, audio); err != nil {
return err return err
} }
outFile.Close()
} else { } else {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() defer cancel()
@@ -374,16 +396,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)) logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
}
return nil return nil
} }

8
go.mod
View File

@@ -10,10 +10,10 @@ require (
github.com/samber/lo v1.39.0 github.com/samber/lo v1.39.0
github.com/urfave/cli/v2 v2.27.1 github.com/urfave/cli/v2 v2.27.1
go.uber.org/zap v1.27.0 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/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/text v0.14.0 golang.org/x/text v0.17.0
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b
) )
require ( require (
@@ -21,6 +21,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.uber.org/multierr v1.11.0 // 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 google.golang.org/protobuf v1.33.0 // indirect
) )

8
go.sum
View File

@@ -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= 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 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= 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 h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= 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 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 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 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-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=