移除dhowden/tag,使用ffprobe替代
All checks were successful
Go CI / test-and-build (push) Successful in 1m1s
Web CI / lint-test-build (push) Successful in 22s

This commit is contained in:
2026-04-06 16:10:34 +08:00
parent fcb5732b40
commit 744fa578f1
4 changed files with 51 additions and 30 deletions

View File

@@ -1,12 +1,14 @@
package scanner
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/dhowden/tag"
)
type ScannedSong struct {
@@ -17,13 +19,28 @@ type ScannedSong struct {
Path string
}
// ffprobeFormat holds the JSON output from ffprobe format section
type ffprobeFormat struct {
Tags map[string]string `json:"tags"`
Duration string `json:"duration"`
}
// ffprobeOutput holds the full ffprobe JSON output
type ffprobeOutput struct {
Format ffprobeFormat `json:"format"`
}
func ScanDirectory(dirPath string) ([]ScannedSong, error) {
songs := []ScannedSong{}
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
return songs, err
if _, err := exec.LookPath("ffprobe"); err != nil {
return songs, fmt.Errorf("ffprobe not found in PATH: %v", err)
}
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
@@ -34,48 +51,61 @@ func ScanDirectory(dirPath string) ([]ScannedSong, error) {
}
if song, err := processAudioFile(path); err != nil {
return err
// Log error but continue scanning other files
return nil
} else {
songs = append(songs, song)
}
return nil
})
return songs, nil
return songs, err
}
func processAudioFile(filePath string) (ScannedSong, error) {
file, err := os.Open(filePath)
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", filePath)
output, err := cmd.Output()
if err != nil {
return ScannedSong{}, fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
metadata, err := tag.ReadFrom(file)
if err != nil {
return ScannedSong{}, fmt.Errorf("failed to read metadata: %v", err)
return ScannedSong{}, fmt.Errorf("ffprobe failed for %s: %v", filePath, err)
}
title := metadata.Title()
var probeOutput ffprobeOutput
if err := json.Unmarshal(output, &probeOutput); err != nil {
return ScannedSong{}, fmt.Errorf("failed to parse ffprobe output for %s: %v", filePath, err)
}
tags := probeOutput.Format.Tags
if tags == nil {
tags = make(map[string]string)
}
title := tags["title"]
if title == "" {
title = filepath.Base(filePath)
}
artist := metadata.Artist()
artist := tags["artist"]
if artist == "" {
artist = "Unknown Artist"
}
album := metadata.Album()
album := tags["album"]
if album == "" {
album = "Unknown Album"
}
duration := 0
if probeOutput.Format.Duration != "" {
if d, err := strconv.ParseFloat(probeOutput.Format.Duration, 64); err == nil {
duration = int(d)
}
}
return ScannedSong{
Title: title,
Artist: artist,
Album: album,
Duration: 100,
Duration: duration,
Path: filePath,
}, nil
}
@@ -83,11 +113,5 @@ func processAudioFile(filePath string) (ScannedSong, error) {
func isAudioFile(path string) bool {
ext := strings.ToLower(filepath.Ext(path))
audioExtensions := []string{".mp3", ".flac", ".m4a", ".wav", ".ogg"}
for _, audioExt := range audioExtensions {
if ext == audioExt {
return true
}
}
return false
return slices.Contains(audioExtensions, ext)
}