From 3f3980de389283b0cd68cec85298d78c9e77dd51 Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Thu, 11 Nov 2021 23:43:20 +0800 Subject: [PATCH] fix #18: skip noop decoder --- algo/common/dispatch.go | 22 +++++++++++++++++----- algo/common/raw.go | 14 +++++++------- algo/kgm/kgm.go | 6 +++--- algo/kwm/kwm.go | 4 ++-- algo/ncm/ncm.go | 2 +- algo/qmc/qmc.go | 36 ++++++++++++++++++------------------ algo/tm/tm.go | 8 ++++---- algo/xm/xm.go | 10 +++++----- cmd/um/main.go | 13 ++++++++----- 9 files changed, 65 insertions(+), 50 deletions(-) diff --git a/algo/common/dispatch.go b/algo/common/dispatch.go index ff42fdf..faefa2a 100644 --- a/algo/common/dispatch.go +++ b/algo/common/dispatch.go @@ -7,12 +7,24 @@ import ( type NewDecoderFunc func([]byte) Decoder -var decoderRegistry = make(map[string][]NewDecoderFunc) +type decoderItem struct { + noop bool + decoder NewDecoderFunc +} -func RegisterDecoder(ext string, dispatchFunc NewDecoderFunc) { - decoderRegistry[ext] = append(decoderRegistry[ext], dispatchFunc) +var decoderRegistry = make(map[string][]decoderItem) + +func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) { + decoderRegistry[ext] = append(decoderRegistry[ext], + decoderItem{noop: noop, decoder: dispatchFunc}) } -func GetDecoder(filename string) []NewDecoderFunc { +func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) { ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), ".")) - return decoderRegistry[ext] + for _, dec := range decoderRegistry[ext] { + if skipNoop && dec.noop { + continue + } + rs = append(rs, dec.decoder) + } + return } diff --git a/algo/common/raw.go b/algo/common/raw.go index 3bb8e0b..6f901b2 100644 --- a/algo/common/raw.go +++ b/algo/common/raw.go @@ -45,11 +45,11 @@ func (d RawDecoder) GetMeta() Meta { } func init() { - RegisterDecoder("mp3", NewRawDecoder) - RegisterDecoder("flac", NewRawDecoder) - RegisterDecoder("ogg", NewRawDecoder) - RegisterDecoder("m4a", NewRawDecoder) - RegisterDecoder("wav", NewRawDecoder) - RegisterDecoder("wma", NewRawDecoder) - RegisterDecoder("aac", NewRawDecoder) + RegisterDecoder("mp3", true, NewRawDecoder) + RegisterDecoder("flac", true, NewRawDecoder) + RegisterDecoder("ogg", true, NewRawDecoder) + RegisterDecoder("m4a", true, NewRawDecoder) + RegisterDecoder("wav", true, NewRawDecoder) + RegisterDecoder("wma", true, NewRawDecoder) + RegisterDecoder("aac", true, NewRawDecoder) } diff --git a/algo/kgm/kgm.go b/algo/kgm/kgm.go index 0a72f86..37595c0 100644 --- a/algo/kgm/kgm.go +++ b/algo/kgm/kgm.go @@ -87,8 +87,8 @@ func (d *Decoder) Decode() error { } func init() { // Kugou - common.RegisterDecoder("kgm", NewDecoder) - common.RegisterDecoder("kgma", NewDecoder) + common.RegisterDecoder("kgm", false, NewDecoder) + common.RegisterDecoder("kgma", false, NewDecoder) // Viper - common.RegisterDecoder("vpr", NewDecoder) + common.RegisterDecoder("vpr", false, NewDecoder) } diff --git a/algo/kwm/kwm.go b/algo/kwm/kwm.go index e846ac7..a9fa2d6 100644 --- a/algo/kwm/kwm.go +++ b/algo/kwm/kwm.go @@ -124,6 +124,6 @@ func padOrTruncate(raw string, length int) string { func init() { // Kuwo Mp3/Flac - common.RegisterDecoder("kwm", NewDecoder) - common.RegisterDecoder("kwm", common.NewRawDecoder) + common.RegisterDecoder("kwm", false, NewDecoder) + common.RegisterDecoder("kwm", false, common.NewRawDecoder) } diff --git a/algo/ncm/ncm.go b/algo/ncm/ncm.go index 620cb18..80da8b3 100644 --- a/algo/ncm/ncm.go +++ b/algo/ncm/ncm.go @@ -246,5 +246,5 @@ func (d Decoder) GetMeta() common.Meta { func init() { // Netease Mp3/Flac - common.RegisterDecoder("ncm", NewDecoder) + common.RegisterDecoder("ncm", false, NewDecoder) } diff --git a/algo/qmc/qmc.go b/algo/qmc/qmc.go index 6f6586e..ed284c5 100644 --- a/algo/qmc/qmc.go +++ b/algo/qmc/qmc.go @@ -102,27 +102,27 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc { //goland:noinspection SpellCheckingInspection func init() { - common.RegisterDecoder("qmc0", DecoderFuncWithExt("mp3")) //QQ Music Mp3 - common.RegisterDecoder("qmc3", DecoderFuncWithExt("mp3")) //QQ Music Mp3 + common.RegisterDecoder("qmc0", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3 + common.RegisterDecoder("qmc3", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3 - common.RegisterDecoder("qmc2", DecoderFuncWithExt("m4a")) //QQ Music M4A - common.RegisterDecoder("qmc4", DecoderFuncWithExt("m4a")) //QQ Music M4A - common.RegisterDecoder("qmc6", DecoderFuncWithExt("m4a")) //QQ Music M4A - common.RegisterDecoder("qmc8", DecoderFuncWithExt("m4a")) //QQ Music M4A + common.RegisterDecoder("qmc2", false, DecoderFuncWithExt("m4a")) //QQ Music M4A + common.RegisterDecoder("qmc4", false, DecoderFuncWithExt("m4a")) //QQ Music M4A + common.RegisterDecoder("qmc6", false, DecoderFuncWithExt("m4a")) //QQ Music M4A + common.RegisterDecoder("qmc8", false, DecoderFuncWithExt("m4a")) //QQ Music M4A - common.RegisterDecoder("qmcflac", DecoderFuncWithExt("flac")) //QQ Music Flac - common.RegisterDecoder("qmcogg", DecoderFuncWithExt("ogg")) //QQ Music Ogg - common.RegisterDecoder("tkm", DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a + common.RegisterDecoder("qmcflac", false, DecoderFuncWithExt("flac")) //QQ Music Flac + common.RegisterDecoder("qmcogg", false, DecoderFuncWithExt("ogg")) //QQ Music Ogg + common.RegisterDecoder("tkm", false, DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a - common.RegisterDecoder("bkcmp3", DecoderFuncWithExt("mp3")) //Moo Music Mp3 - common.RegisterDecoder("bkcflac", DecoderFuncWithExt("flac")) //Moo Music Flac + common.RegisterDecoder("bkcmp3", false, DecoderFuncWithExt("mp3")) //Moo Music Mp3 + common.RegisterDecoder("bkcflac", false, DecoderFuncWithExt("flac")) //Moo Music Flac - common.RegisterDecoder("666c6163", DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac - common.RegisterDecoder("6d7033", DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3 - common.RegisterDecoder("6f6767", DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg - common.RegisterDecoder("6d3461", DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a - common.RegisterDecoder("776176", DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav + common.RegisterDecoder("666c6163", false, DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac + common.RegisterDecoder("6d7033", false, DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3 + common.RegisterDecoder("6f6767", false, DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg + common.RegisterDecoder("6d3461", false, DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a + common.RegisterDecoder("776176", false, DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav - common.RegisterDecoder("mgg", NewMgg256Decoder) //QQ Music New Ogg - common.RegisterDecoder("mflac", NewMflac256Decoder) //QQ Music New Flac + common.RegisterDecoder("mgg", false, NewMgg256Decoder) //QQ Music New Ogg + common.RegisterDecoder("mflac", false, NewMflac256Decoder) //QQ Music New Flac } diff --git a/algo/tm/tm.go b/algo/tm/tm.go index 40c6ab4..d1ccc18 100644 --- a/algo/tm/tm.go +++ b/algo/tm/tm.go @@ -70,10 +70,10 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc { func init() { // QQ Music IOS M4a - common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a")) - common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a")) + common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a")) + common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a")) // QQ Music IOS Mp3 - common.RegisterDecoder("tm0", common.NewRawDecoder) - common.RegisterDecoder("tm3", common.NewRawDecoder) + common.RegisterDecoder("tm0", false, common.NewRawDecoder) + common.RegisterDecoder("tm3", false, common.NewRawDecoder) } diff --git a/algo/xm/xm.go b/algo/xm/xm.go index de5bbcb..42a88ba 100644 --- a/algo/xm/xm.go +++ b/algo/xm/xm.go @@ -97,10 +97,10 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc { func init() { // Xiami Wav/M4a/Mp3/Flac - common.RegisterDecoder("xm", NewDecoder) + common.RegisterDecoder("xm", false, NewDecoder) // Xiami Typed Format - common.RegisterDecoder("wav", DecoderFuncWithExt("wav")) - common.RegisterDecoder("mp3", DecoderFuncWithExt("mp3")) - common.RegisterDecoder("flac", DecoderFuncWithExt("flac")) - common.RegisterDecoder("m4a", DecoderFuncWithExt("m4a")) + common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav")) + common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3")) + common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac")) + common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a")) } diff --git a/cmd/um/main.go b/cmd/um/main.go index 4c59a08..c768cc5 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -28,12 +28,13 @@ func main() { Flags: []cli.Flag{ &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.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true}, }, Action: appMain, Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE", HideHelpCommand: true, - UsageText: "um [-o /path/to/output/dir] [-i] /path/to/input", + UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input", } err := app.Run(os.Args) if err != nil { @@ -56,6 +57,8 @@ func appMain(c *cli.Context) error { } } + skipNoop := c.Bool("skip-noop") + inputStat, err := os.Stat(input) if err != nil { return err @@ -74,9 +77,9 @@ func appMain(c *cli.Context) error { } if inputStat.IsDir() { - return dealDirectory(input, output) + return dealDirectory(input, output, skipNoop) } else { - allDec := common.GetDecoder(inputStat.Name()) + allDec := common.GetDecoder(inputStat.Name(), skipNoop) if len(allDec) == 0 { logging.Log().Fatal("skipping while no suitable decoder") } @@ -84,7 +87,7 @@ func appMain(c *cli.Context) error { } } -func dealDirectory(inputDir string, outputDir string) error { +func dealDirectory(inputDir string, outputDir string, skipNoop bool) error { items, err := os.ReadDir(inputDir) if err != nil { return err @@ -93,7 +96,7 @@ func dealDirectory(inputDir string, outputDir string) error { if item.IsDir() { continue } - allDec := common.GetDecoder(item.Name()) + allDec := common.GetDecoder(item.Name(), skipNoop) if len(allDec) == 0 { logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name())) continue