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
import (
"go.uber.org/zap"
"strings"
"unlock-music.dev/cli/algo/common"
@ -17,33 +18,49 @@ type ncmMeta interface {
}
type ncmMetaMusic struct {
Format string `json:"format"`
MusicName string `json:"musicName"`
Artist [][]interface{} `json:"artist"`
Album string `json:"album"`
AlbumPicDocID interface{} `json:"albumPicDocId"`
AlbumPic string `json:"albumPic"`
Flag int `json:"flag"`
Bitrate int `json:"bitrate"`
Duration int `json:"duration"`
Alias []interface{} `json:"alias"`
TransNames []interface{} `json:"transNames"`
logger *zap.Logger
Format string `json:"format"`
MusicName string `json:"musicName"`
Artist interface{} `json:"artist"`
Album string `json:"album"`
AlbumPicDocID interface{} `json:"albumPicDocId"`
AlbumPic string `json:"albumPic"`
Flag int `json:"flag"`
Bitrate int `json:"bitrate"`
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 {
return m.AlbumPic
}
func (m *ncmMetaMusic) GetArtists() (artists []string) {
for _, artist := range m.Artist {
for _, item := range artist {
name, ok := item.(string)
if ok {
func (m *ncmMetaMusic) GetArtists() []string {
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
var artists []string = nil
if jsonArtists, ok := m.Artist.([][]string); ok {
for _, artist := range jsonArtists {
for _, name := range artist {
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 {

View File

@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"go.uber.org/zap"
"io"
"net/http"
"strings"
@ -30,11 +31,12 @@ var (
)
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 {
rd io.ReadSeeker // rd is the original file reader
logger *zap.Logger
rd io.ReadSeeker // rd is the original file reader
offset int
cipher common.StreamDecoder
@ -74,7 +76,7 @@ func (d *Decoder) Validate() error {
}
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)
@ -181,7 +183,7 @@ func (d *Decoder) readCoverData() error {
func (d *Decoder) parseMeta() error {
switch d.metaType {
case "music":
d.meta = new(ncmMetaMusic)
d.meta = newNcmMetaMusic(d.logger)
return json.Unmarshal(d.metaRaw, d.meta)
case "dj":
d.meta = new(ncmMetaDJ)

View File

@ -5,6 +5,10 @@ import (
"context"
"errors"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"os"
"os/signal"
@ -15,11 +19,6 @@ import (
"sort"
"strings"
"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/kgm"
_ "unlock-music.dev/cli/algo/kwm"
@ -29,14 +28,13 @@ import (
_ "unlock-music.dev/cli/algo/xiami"
_ "unlock-music.dev/cli/algo/ximalaya"
"unlock-music.dev/cli/internal/ffmpeg"
"unlock-music.dev/cli/internal/logging"
"unlock-music.dev/cli/internal/sniff"
"unlock-music.dev/cli/internal/utils"
)
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() {
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.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: "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: "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},
@ -67,6 +66,7 @@ func main() {
HideHelpCommand: true,
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
}
err := app.Run(os.Args)
if err != nil {
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) {
logger = setupLogger(c.Bool("verbose"))
cwd, err := os.Getwd()
if err != nil {
return err
@ -155,6 +175,7 @@ func appMain(c *cli.Context) (err error) {
}
proc := &processor{
logger: logger,
inputDir: inputDir,
outputDir: output,
skipNoopDecoder: c.Bool("skip-noop"),
@ -177,6 +198,7 @@ func appMain(c *cli.Context) (err error) {
}
type processor struct {
logger *zap.Logger
inputDir 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)
defer cancel()
if err := ffmpeg.UpdateMeta(ctx, outPath, params); err != nil {
if err := ffmpeg.UpdateMeta(ctx, outPath, params, logger); err != nil {
return err
}
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"io"
"os"
"os/exec"
@ -43,9 +44,9 @@ type UpdateMetadataParams struct {
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" {
return updateMetaFlac(ctx, outPath, params)
return updateMetaFlac(ctx, outPath, params, logger.With(zap.String("module", "updateMetaFlac")))
} else {
return updateMetaFFmpeg(ctx, outPath, params)
}

View File

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