16 Commits

Author SHA1 Message Date
鲁树人
f819726f3e chore: bump version to v0.2.4 2024-10-21 03:59:52 +09:00
鲁树人
fbad7ec450 fix #102: support multi-part kgm extensions (kgm.flac/vpr.flac) 2024-10-21 03:58:21 +09:00
鲁树人
19bc6c466e refactor: allow multi-part extensions (#102) 2024-10-21 03:57:56 +09:00
鲁树人
b9e2a38f82 fix: do not throw error when file already exists 2024-10-21 03:56:52 +09:00
鲁树人
673ea8491f docs: add do not fork notice 2024-10-13 05:45:25 +09:00
鲁树人
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
6 changed files with 110 additions and 64 deletions

1
.gitignore vendored
View File

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

View File

@@ -6,6 +6,9 @@ Original: Web Edition https://git.unlock-music.dev/um/web
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest) - [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/) - [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
> **WARNING**
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
## Features ## Features
- [x] All Algorithm Supported By `unlock-music/web` - [x] All Algorithm Supported By `unlock-music/web`

View File

@@ -18,25 +18,32 @@ type DecoderParams struct {
} }
type NewDecoderFunc func(p *DecoderParams) Decoder type NewDecoderFunc func(p *DecoderParams) Decoder
type decoderItem struct { type DecoderFactory struct {
noop bool noop bool
decoder NewDecoderFunc Suffix string
Create NewDecoderFunc
} }
var DecoderRegistry = make(map[string][]decoderItem) var DecoderRegistry []DecoderFactory
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) { func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
DecoderRegistry[ext] = append(DecoderRegistry[ext], DecoderRegistry = append(DecoderRegistry,
decoderItem{noop: noop, decoder: dispatchFunc}) DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
} }
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) { func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), ".")) var result []DecoderFactory
for _, dec := range DecoderRegistry[ext] { // Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate
// all decoders for each extension.
name := strings.ToLower(filepath.Base(filename))
for _, dec := range DecoderRegistry {
if !strings.HasSuffix(name, dec.Suffix) {
continue
}
if skipNoop && dec.noop { if skipNoop && dec.noop {
continue continue
} }
rs = append(rs, dec.decoder) result = append(result, dec)
} }
return return result
} }

View File

@@ -61,4 +61,7 @@ func init() {
common.RegisterDecoder("kgma", false, NewDecoder) common.RegisterDecoder("kgma", false, NewDecoder)
// Viper // Viper
common.RegisterDecoder("vpr", false, NewDecoder) common.RegisterDecoder("vpr", false, NewDecoder)
// Kugou Android
common.RegisterDecoder("kgm.flac", false, NewDecoder)
common.RegisterDecoder("vpr.flac", false, NewDecoder)
} }

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

@@ -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.2.1" var AppVersion = "v0.2.4"
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
@@ -74,16 +75,30 @@ func main() {
func printSupportedExtensions() { func printSupportedExtensions() {
var exts []string var exts []string
for ext := range common.DecoderRegistry { extSet := make(map[string]int)
for _, factory := range common.DecoderRegistry {
ext := strings.TrimPrefix(factory.Suffix, ".")
if n, ok := extSet[ext]; ok {
extSet[ext] = n + 1
} else {
extSet[ext] = 1
}
}
for ext := range extSet {
exts = append(exts, ext) exts = append(exts, ext)
} }
sort.Strings(exts) sort.Strings(exts)
for _, ext := range exts { for _, ext := range exts {
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext])) fmt.Printf("%s: %d\n", ext, extSet[ext])
} }
} }
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 +107,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 +116,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 +152,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 +161,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 +174,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,29 +242,32 @@ 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")
} }
if err := p.process(filePath, allDec); err != nil { if err := p.process(filePath, allDec); err != nil {
@@ -270,7 +285,19 @@ func (p *processor) processFile(filePath string) error {
return nil return nil
} }
func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error { func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) {
for _, factory := range decoders {
dec := factory.Create(params)
err := dec.Validate()
if err == nil {
return &dec, &factory, nil
}
logger.Warn("try decode failed", zap.Error(err))
}
return nil, nil, errors.New("no any decoder can resolve the file")
}
func (p *processor) process(inputFile string, allDec []common.DecoderFactory) error {
file, err := os.Open(inputFile) file, err := os.Open(inputFile)
if err != nil { if err != nil {
return err return err
@@ -278,26 +305,16 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
defer file.Close() defer file.Close()
logger := logger.With(zap.String("source", inputFile)) logger := logger.With(zap.String("source", inputFile))
decParams := &common.DecoderParams{ pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
Reader: file, Reader: file,
Extension: filepath.Ext(inputFile), Extension: filepath.Ext(inputFile),
FilePath: inputFile, FilePath: inputFile,
Logger: logger, Logger: logger,
})
if err != nil {
return err
} }
dec := *pDec
var dec common.Decoder
for _, decFunc := range allDec {
dec = decFunc(decParams)
if err := dec.Validate(); err == nil {
break
} else {
logger.Warn("try decode failed", zap.Error(err))
dec = nil
}
}
if dec == nil {
return errors.New("no any decoder can resolve the file")
}
params := &ffmpeg.UpdateMetadataParams{} params := &ffmpeg.UpdateMetadataParams{}
@@ -354,13 +371,19 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
} }
} }
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile)) inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt) if err != nil {
return fmt.Errorf("get relative dir failed: %w", err)
}
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
if !p.overwriteOutput { if !p.overwriteOutput {
_, err := os.Stat(outPath) _, err := os.Stat(outPath)
if err == nil { if err == nil {
return fmt.Errorf("output file %s is already exist", outPath) logger.Warn("output file already exist, skip", zap.String("destination", outPath))
return nil
} else if !errors.Is(err, os.ErrNotExist) { } else if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("stat output file failed: %w", err) return fmt.Errorf("stat output file failed: %w", err)
} }