Compare commits

...

3 Commits

Author SHA1 Message Date
6493b2c5fc fix #78: skip parsing cover art if image is unsupported
All checks were successful
continuous-integration/drone/push Build is passing
2024-11-02 13:49:40 +09:00
f753b9c67d fix #78 #106: app crash due to imcompatible ncm metadata json 2024-11-02 13:44:29 +09:00
8829a3b3ba refactor: rework on logging 2024-11-02 13:43:56 +09:00
5 changed files with 92 additions and 46 deletions

View File

@ -1,6 +1,7 @@
package ncm package ncm
import ( import (
"go.uber.org/zap"
"strings" "strings"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
@ -17,33 +18,49 @@ type ncmMeta interface {
} }
type ncmMetaMusic struct { type ncmMetaMusic struct {
Format string `json:"format"` logger *zap.Logger
MusicName string `json:"musicName"`
Artist [][]interface{} `json:"artist"` Format string `json:"format"`
Album string `json:"album"` MusicName string `json:"musicName"`
AlbumPicDocID interface{} `json:"albumPicDocId"` Artist interface{} `json:"artist"`
AlbumPic string `json:"albumPic"` Album string `json:"album"`
Flag int `json:"flag"` AlbumPicDocID interface{} `json:"albumPicDocId"`
Bitrate int `json:"bitrate"` AlbumPic string `json:"albumPic"`
Duration int `json:"duration"` Flag int `json:"flag"`
Alias []interface{} `json:"alias"` Bitrate int `json:"bitrate"`
TransNames []interface{} `json:"transNames"` Duration int `json:"duration"`
Alias []interface{} `json:"alias"`
TransNames []interface{} `json:"transNames"`
}
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
ncm := new(ncmMetaMusic)
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
return ncm
} }
func (m *ncmMetaMusic) GetAlbumImageURL() string { func (m *ncmMetaMusic) GetAlbumImageURL() string {
return m.AlbumPic return m.AlbumPic
} }
func (m *ncmMetaMusic) GetArtists() (artists []string) { func (m *ncmMetaMusic) GetArtists() []string {
for _, artist := range m.Artist { m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
for _, item := range artist {
name, ok := item.(string) var artists []string = nil
if ok { if jsonArtists, ok := m.Artist.([][]string); ok {
for _, artist := range jsonArtists {
for _, name := range artist {
artists = append(artists, name) artists = append(artists, name)
} }
} }
} else if artist, ok := m.Artist.(string); ok {
// #78: artist is a string type.
// https://git.unlock-music.dev/um/cli/issues/78
artists = []string{artist}
} else {
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
} }
return return artists
} }
func (m *ncmMetaMusic) GetTitle() string { func (m *ncmMetaMusic) GetTitle() string {

View File

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.uber.org/zap"
"io" "io"
"net/http" "net/http"
"strings" "strings"
@ -30,11 +31,12 @@ var (
) )
func NewDecoder(p *common.DecoderParams) common.Decoder { func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader} return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))}
} }
type Decoder struct { type Decoder struct {
rd io.ReadSeeker // rd is the original file reader logger *zap.Logger
rd io.ReadSeeker // rd is the original file reader
offset int offset int
cipher common.StreamDecoder cipher common.StreamDecoder
@ -74,7 +76,7 @@ func (d *Decoder) Validate() error {
} }
if err := d.parseMeta(); err != nil { if err := d.parseMeta(); err != nil {
return fmt.Errorf("parse meta failed: %w", err) return fmt.Errorf("parse meta failed: %w (raw json=%s)", err, string(d.metaRaw))
} }
d.cipher = newNcmCipher(keyData) d.cipher = newNcmCipher(keyData)
@ -181,7 +183,7 @@ func (d *Decoder) readCoverData() error {
func (d *Decoder) parseMeta() error { func (d *Decoder) parseMeta() error {
switch d.metaType { switch d.metaType {
case "music": case "music":
d.meta = new(ncmMetaMusic) d.meta = newNcmMetaMusic(d.logger)
return json.Unmarshal(d.metaRaw, d.meta) return json.Unmarshal(d.metaRaw, d.meta)
case "dj": case "dj":
d.meta = new(ncmMetaDJ) d.meta = new(ncmMetaDJ)

View File

@ -5,6 +5,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io" "io"
"os" "os"
"os/signal" "os/signal"
@ -15,11 +19,6 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
_ "unlock-music.dev/cli/algo/kgm" _ "unlock-music.dev/cli/algo/kgm"
_ "unlock-music.dev/cli/algo/kwm" _ "unlock-music.dev/cli/algo/kwm"
@ -29,14 +28,13 @@ import (
_ "unlock-music.dev/cli/algo/xiami" _ "unlock-music.dev/cli/algo/xiami"
_ "unlock-music.dev/cli/algo/ximalaya" _ "unlock-music.dev/cli/algo/ximalaya"
"unlock-music.dev/cli/internal/ffmpeg" "unlock-music.dev/cli/internal/ffmpeg"
"unlock-music.dev/cli/internal/logging"
"unlock-music.dev/cli/internal/sniff" "unlock-music.dev/cli/internal/sniff"
"unlock-music.dev/cli/internal/utils" "unlock-music.dev/cli/internal/utils"
) )
var AppVersion = "v0.2.8" var AppVersion = "v0.2.8"
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger var logger = setupLogger(false) // TODO: inject logger to application, instead of using global logger
func main() { func main() {
module, ok := debug.ReadBuildInfo() module, ok := debug.ReadBuildInfo()
@ -55,6 +53,7 @@ func main() {
&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},
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false}, &cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
&cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false}, &cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false},
&cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false}, &cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false},
@ -67,6 +66,7 @@ func main() {
HideHelpCommand: true, HideHelpCommand: true,
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-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 {
logger.Fatal("run app failed", zap.Error(err)) logger.Fatal("run app failed", zap.Error(err))
@ -93,7 +93,27 @@ func printSupportedExtensions() {
} }
} }
func setupLogger(verbose bool) *zap.Logger {
logConfig := zap.NewProductionEncoderConfig()
logConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logConfig.EncodeTime = zapcore.RFC3339TimeEncoder
enabler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
if verbose {
return true
}
return level >= zapcore.InfoLevel
})
return zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(logConfig),
os.Stdout,
enabler,
))
}
func appMain(c *cli.Context) (err error) { func appMain(c *cli.Context) (err error) {
logger = setupLogger(c.Bool("verbose"))
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
return err return err
@ -155,6 +175,7 @@ func appMain(c *cli.Context) (err error) {
} }
proc := &processor{ proc := &processor{
logger: logger,
inputDir: inputDir, inputDir: inputDir,
outputDir: output, outputDir: output,
skipNoopDecoder: c.Bool("skip-noop"), skipNoopDecoder: c.Bool("skip-noop"),
@ -177,6 +198,7 @@ func appMain(c *cli.Context) (err error) {
} }
type processor struct { type processor struct {
logger *zap.Logger
inputDir string inputDir string
outputDir string outputDir string
@ -406,7 +428,7 @@ func (p *processor) process(inputFile string, allDec []common.DecoderFactory) er
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() defer cancel()
if err := ffmpeg.UpdateMeta(ctx, outPath, params); err != nil { if err := ffmpeg.UpdateMeta(ctx, outPath, params, logger); err != nil {
return err return err
} }
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"go.uber.org/zap"
"io" "io"
"os" "os"
"os/exec" "os/exec"
@ -43,9 +44,9 @@ type UpdateMetadataParams struct {
AlbumArtExt string // required if AlbumArt is not nil AlbumArtExt string // required if AlbumArt is not nil
} }
func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams) error { func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams, logger *zap.Logger) error {
if params.AudioExt == ".flac" { if params.AudioExt == ".flac" {
return updateMetaFlac(ctx, outPath, params) return updateMetaFlac(ctx, outPath, params, logger.With(zap.String("module", "updateMetaFlac")))
} else { } else {
return updateMetaFFmpeg(ctx, outPath, params) return updateMetaFFmpeg(ctx, outPath, params)
} }

View File

@ -2,6 +2,7 @@ package ffmpeg
import ( import (
"context" "context"
"go.uber.org/zap"
"mime" "mime"
"strings" "strings"
@ -11,7 +12,7 @@ import (
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams) error { func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams, logger *zap.Logger) error {
f, err := flac.ParseFile(m.Audio) f, err := flac.ParseFile(m.Audio)
if err != nil { if err != nil {
return err return err
@ -62,27 +63,30 @@ func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams)
} }
if m.AlbumArt != nil { if m.AlbumArt != nil {
coverMime := mime.TypeByExtension(m.AlbumArtExt)
logger.Debug("cover image mime detect", zap.String("mime", coverMime))
cover, err := flacpicture.NewFromImageData( cover, err := flacpicture.NewFromImageData(
flacpicture.PictureTypeFrontCover, flacpicture.PictureTypeFrontCover,
"Front cover", "Front cover",
m.AlbumArt, m.AlbumArt,
mime.TypeByExtension(m.AlbumArtExt), coverMime,
) )
if err != nil {
return err
}
coverBlock := cover.Marshal()
f.Meta = append(f.Meta, &coverBlock)
// add / replace flac cover if err != nil {
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool { logger.Warn("failed to create flac cover", zap.Error(err))
return b.Type == flac.Picture
})
if coverIdx < 0 {
f.Meta = append(f.Meta, &coverBlock)
} else { } else {
f.Meta[coverIdx] = &coverBlock coverBlock := cover.Marshal()
f.Meta = append(f.Meta, &coverBlock)
// add / replace flac cover
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
return b.Type == flac.Picture
})
if coverIdx < 0 {
f.Meta = append(f.Meta, &coverBlock)
} else {
f.Meta[coverIdx] = &coverBlock
}
} }
} }