From 6493b2c5fc46bf21e82372cc30a327c5245b10d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sat, 2 Nov 2024 13:49:40 +0900 Subject: [PATCH] fix #78: skip parsing cover art if image is unsupported --- cmd/um/main.go | 2 +- internal/ffmpeg/ffmpeg.go | 5 +++-- internal/ffmpeg/meta_flac.go | 34 +++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/um/main.go b/cmd/um/main.go index f6326d6..51dc60c 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -428,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 } } diff --git a/internal/ffmpeg/ffmpeg.go b/internal/ffmpeg/ffmpeg.go index 9fe9ac9..24418aa 100644 --- a/internal/ffmpeg/ffmpeg.go +++ b/internal/ffmpeg/ffmpeg.go @@ -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) } diff --git a/internal/ffmpeg/meta_flac.go b/internal/ffmpeg/meta_flac.go index 4ec6a9d..878f75d 100644 --- a/internal/ffmpeg/meta_flac.go +++ b/internal/ffmpeg/meta_flac.go @@ -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 + } } }