fix #78: skip parsing cover art if image is unsupported

main
鲁树人 2 months ago
parent f753b9c67d
commit 6493b2c5fc

@ -428,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
} }
} }

@ -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)
} }

@ -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,16 +63,18 @@ 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 { if err != nil {
return err logger.Warn("failed to create flac cover", zap.Error(err))
} } else {
coverBlock := cover.Marshal() coverBlock := cover.Marshal()
f.Meta = append(f.Meta, &coverBlock) f.Meta = append(f.Meta, &coverBlock)
@ -85,6 +88,7 @@ func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams)
f.Meta[coverIdx] = &coverBlock f.Meta[coverIdx] = &coverBlock
} }
} }
}
return f.Save(outPath) return f.Save(outPath)
} }

Loading…
Cancel
Save