From 169b2c1858b87774f54ed6e21297ed3672d72155 Mon Sep 17 00:00:00 2001 From: lzw-723 Date: Wed, 8 Apr 2026 20:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=B0=81=E9=9D=A2=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/cover_svc.go | 11 +++++------ internal/util/ffmpeg.go | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/internal/service/cover_svc.go b/internal/service/cover_svc.go index 201f01c..64a40c1 100644 --- a/internal/service/cover_svc.go +++ b/internal/service/cover_svc.go @@ -1,13 +1,10 @@ package service import ( - "butterfliu/config" "butterfliu/internal/model" "butterfliu/internal/repository" "butterfliu/internal/util" "errors" - "path" - "strconv" ) type CoverService struct { @@ -37,9 +34,11 @@ func (s *CoverService) GetSongCover(id int) (string, error) { if err != nil { return "", err } - util.ExtractCover(file.Path, id) - conf := config.LoadConfig() - return path.Join(conf.GetCachePath("cover"), strconv.Itoa(id)+".jpg"), nil + cover, err := util.ExtractCover(file.Path, id) + if err != nil { + return "", err + } + return cover, nil } func (s *CoverService) GetAlbumCover(id int) (string, error) { diff --git a/internal/util/ffmpeg.go b/internal/util/ffmpeg.go index 91e150f..d86f424 100644 --- a/internal/util/ffmpeg.go +++ b/internal/util/ffmpeg.go @@ -1,20 +1,30 @@ package util import ( + "butterfliu/config" + "os" "os/exec" + "path" "strconv" ) -func ExtractCover(filePath string, id int) bool { - if _, err := exec.LookPath("ffmpeg"); err != nil { - return false +func ExtractCover(filePath string, id int) (string, error) { + config := config.LoadConfig() + coverPath := path.Join(config.GetCachePath("cover"), strconv.Itoa(id)+".jpg") + + _, err := os.Stat(coverPath) + if err == nil { + return coverPath, nil + } + + if _, err := exec.LookPath("ffmpeg"); err != nil { + return "", err } - coverPath := "cache/cover/" + strconv.Itoa(id) + ".jpg" cmd := exec.Command("ffmpeg", "-i", filePath, "-an", "-vcodec", "mjpeg", "-q:v", "5", "-f", "image2", "-y", coverPath) if err := cmd.Run(); err != nil { - return false + return "", err } - return true + return coverPath, nil }