fix #18: skip noop decoder

This commit is contained in:
Emmm Monster 2021-11-11 23:43:20 +08:00
parent 6fd5bd5863
commit 3f3980de38
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
9 changed files with 65 additions and 50 deletions

View File

@ -7,12 +7,24 @@ import (
type NewDecoderFunc func([]byte) Decoder type NewDecoderFunc func([]byte) Decoder
var decoderRegistry = make(map[string][]NewDecoderFunc) type decoderItem struct {
noop bool
decoder NewDecoderFunc
}
func RegisterDecoder(ext string, dispatchFunc NewDecoderFunc) { var decoderRegistry = make(map[string][]decoderItem)
decoderRegistry[ext] = append(decoderRegistry[ext], dispatchFunc)
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), ".")) 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
} }

View File

@ -45,11 +45,11 @@ func (d RawDecoder) GetMeta() Meta {
} }
func init() { func init() {
RegisterDecoder("mp3", NewRawDecoder) RegisterDecoder("mp3", true, NewRawDecoder)
RegisterDecoder("flac", NewRawDecoder) RegisterDecoder("flac", true, NewRawDecoder)
RegisterDecoder("ogg", NewRawDecoder) RegisterDecoder("ogg", true, NewRawDecoder)
RegisterDecoder("m4a", NewRawDecoder) RegisterDecoder("m4a", true, NewRawDecoder)
RegisterDecoder("wav", NewRawDecoder) RegisterDecoder("wav", true, NewRawDecoder)
RegisterDecoder("wma", NewRawDecoder) RegisterDecoder("wma", true, NewRawDecoder)
RegisterDecoder("aac", NewRawDecoder) RegisterDecoder("aac", true, NewRawDecoder)
} }

View File

@ -87,8 +87,8 @@ func (d *Decoder) Decode() error {
} }
func init() { func init() {
// Kugou // Kugou
common.RegisterDecoder("kgm", NewDecoder) common.RegisterDecoder("kgm", false, NewDecoder)
common.RegisterDecoder("kgma", NewDecoder) common.RegisterDecoder("kgma", false, NewDecoder)
// Viper // Viper
common.RegisterDecoder("vpr", NewDecoder) common.RegisterDecoder("vpr", false, NewDecoder)
} }

View File

@ -124,6 +124,6 @@ func padOrTruncate(raw string, length int) string {
func init() { func init() {
// Kuwo Mp3/Flac // Kuwo Mp3/Flac
common.RegisterDecoder("kwm", NewDecoder) common.RegisterDecoder("kwm", false, NewDecoder)
common.RegisterDecoder("kwm", common.NewRawDecoder) common.RegisterDecoder("kwm", false, common.NewRawDecoder)
} }

View File

@ -246,5 +246,5 @@ func (d Decoder) GetMeta() common.Meta {
func init() { func init() {
// Netease Mp3/Flac // Netease Mp3/Flac
common.RegisterDecoder("ncm", NewDecoder) common.RegisterDecoder("ncm", false, NewDecoder)
} }

View File

@ -102,27 +102,27 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
//goland:noinspection SpellCheckingInspection //goland:noinspection SpellCheckingInspection
func init() { func init() {
common.RegisterDecoder("qmc0", DecoderFuncWithExt("mp3")) //QQ Music Mp3 common.RegisterDecoder("qmc0", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3
common.RegisterDecoder("qmc3", DecoderFuncWithExt("mp3")) //QQ Music Mp3 common.RegisterDecoder("qmc3", false, DecoderFuncWithExt("mp3")) //QQ Music Mp3
common.RegisterDecoder("qmc2", DecoderFuncWithExt("m4a")) //QQ Music M4A common.RegisterDecoder("qmc2", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc4", DecoderFuncWithExt("m4a")) //QQ Music M4A common.RegisterDecoder("qmc4", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc6", DecoderFuncWithExt("m4a")) //QQ Music M4A common.RegisterDecoder("qmc6", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc8", DecoderFuncWithExt("m4a")) //QQ Music M4A common.RegisterDecoder("qmc8", false, DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmcflac", DecoderFuncWithExt("flac")) //QQ Music Flac common.RegisterDecoder("qmcflac", false, DecoderFuncWithExt("flac")) //QQ Music Flac
common.RegisterDecoder("qmcogg", DecoderFuncWithExt("ogg")) //QQ Music Ogg common.RegisterDecoder("qmcogg", false, DecoderFuncWithExt("ogg")) //QQ Music Ogg
common.RegisterDecoder("tkm", DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a common.RegisterDecoder("tkm", false, DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a
common.RegisterDecoder("bkcmp3", DecoderFuncWithExt("mp3")) //Moo Music Mp3 common.RegisterDecoder("bkcmp3", false, DecoderFuncWithExt("mp3")) //Moo Music Mp3
common.RegisterDecoder("bkcflac", DecoderFuncWithExt("flac")) //Moo Music Flac common.RegisterDecoder("bkcflac", false, DecoderFuncWithExt("flac")) //Moo Music Flac
common.RegisterDecoder("666c6163", DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac common.RegisterDecoder("666c6163", false, DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac
common.RegisterDecoder("6d7033", DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3 common.RegisterDecoder("6d7033", false, DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3
common.RegisterDecoder("6f6767", DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg common.RegisterDecoder("6f6767", false, DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg
common.RegisterDecoder("6d3461", DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a common.RegisterDecoder("6d3461", false, DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a
common.RegisterDecoder("776176", DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav common.RegisterDecoder("776176", false, DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav
common.RegisterDecoder("mgg", NewMgg256Decoder) //QQ Music New Ogg common.RegisterDecoder("mgg", false, NewMgg256Decoder) //QQ Music New Ogg
common.RegisterDecoder("mflac", NewMflac256Decoder) //QQ Music New Flac common.RegisterDecoder("mflac", false, NewMflac256Decoder) //QQ Music New Flac
} }

View File

@ -70,10 +70,10 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
func init() { func init() {
// QQ Music IOS M4a // QQ Music IOS M4a
common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a")) common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a")) common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
// QQ Music IOS Mp3 // QQ Music IOS Mp3
common.RegisterDecoder("tm0", common.NewRawDecoder) common.RegisterDecoder("tm0", false, common.NewRawDecoder)
common.RegisterDecoder("tm3", common.NewRawDecoder) common.RegisterDecoder("tm3", false, common.NewRawDecoder)
} }

View File

@ -97,10 +97,10 @@ func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
func init() { func init() {
// Xiami Wav/M4a/Mp3/Flac // Xiami Wav/M4a/Mp3/Flac
common.RegisterDecoder("xm", NewDecoder) common.RegisterDecoder("xm", false, NewDecoder)
// Xiami Typed Format // Xiami Typed Format
common.RegisterDecoder("wav", DecoderFuncWithExt("wav")) common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
common.RegisterDecoder("mp3", DecoderFuncWithExt("mp3")) common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
common.RegisterDecoder("flac", DecoderFuncWithExt("flac")) common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
common.RegisterDecoder("m4a", DecoderFuncWithExt("m4a")) common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
} }

View File

@ -28,12 +28,13 @@ 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.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
}, },
Action: appMain, Action: appMain,
Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE", Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE",
HideHelpCommand: true, 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) err := app.Run(os.Args)
if err != nil { if err != nil {
@ -56,6 +57,8 @@ func appMain(c *cli.Context) error {
} }
} }
skipNoop := c.Bool("skip-noop")
inputStat, err := os.Stat(input) inputStat, err := os.Stat(input)
if err != nil { if err != nil {
return err return err
@ -74,9 +77,9 @@ func appMain(c *cli.Context) error {
} }
if inputStat.IsDir() { if inputStat.IsDir() {
return dealDirectory(input, output) return dealDirectory(input, output, skipNoop)
} else { } else {
allDec := common.GetDecoder(inputStat.Name()) allDec := common.GetDecoder(inputStat.Name(), skipNoop)
if len(allDec) == 0 { if len(allDec) == 0 {
logging.Log().Fatal("skipping while no suitable decoder") 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) items, err := os.ReadDir(inputDir)
if err != nil { if err != nil {
return err return err
@ -93,7 +96,7 @@ func dealDirectory(inputDir string, outputDir string) error {
if item.IsDir() { if item.IsDir() {
continue continue
} }
allDec := common.GetDecoder(item.Name()) allDec := common.GetDecoder(item.Name(), skipNoop)
if len(allDec) == 0 { if len(allDec) == 0 {
logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name())) logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name()))
continue continue