fix #18: skip noop decoder
This commit is contained in:
parent
6fd5bd5863
commit
3f3980de38
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -246,5 +246,5 @@ func (d Decoder) GetMeta() common.Meta {
|
||||
|
||||
func init() {
|
||||
// Netease Mp3/Flac
|
||||
common.RegisterDecoder("ncm", NewDecoder)
|
||||
common.RegisterDecoder("ncm", false, NewDecoder)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user