实现增量扫描
All checks were successful
Go CI / test-and-build (push) Successful in 13s
Web CI / lint-test-build (push) Successful in 31s

This commit is contained in:
2026-04-11 14:38:23 +08:00
parent 3f82e29c6c
commit 79d47c81e7
10 changed files with 351 additions and 91 deletions

View File

@@ -11,34 +11,51 @@ import (
"strings"
)
type DiscoveredAudioFile struct {
Path string
FileSize int64
FileMtimeNs int64
}
type ScannedSong struct {
Title string
Artist string
Album string
Duration int
Path string
Title string
Artist string
Album string
Duration int
Path string
Format string
BitRate int
SampleRate int
}
// ffprobeFormat holds the JSON output from ffprobe format section
type ffprobeFormat struct {
Tags map[string]string `json:"tags"`
Duration string `json:"duration"`
Tags map[string]string `json:"tags"`
Duration string `json:"duration"`
BitRate string `json:"bit_rate"`
FormatName string `json:"format_name"`
}
type ffprobeStream struct {
CodecType string `json:"codec_type"`
SampleRate string `json:"sample_rate"`
}
// ffprobeOutput holds the full ffprobe JSON output
type ffprobeOutput struct {
Format ffprobeFormat `json:"format"`
Format ffprobeFormat `json:"format"`
Streams []ffprobeStream `json:"streams"`
}
func ScanDirectory(dirPath string) ([]ScannedSong, error) {
paths, err := ListAudioFiles(dirPath)
files, err := ListAudioFiles(dirPath)
if err != nil {
return nil, err
}
songs := make([]ScannedSong, 0, len(paths))
for _, path := range paths {
song, err := ProbeAudioFile(path)
songs := make([]ScannedSong, 0, len(files))
for _, file := range files {
song, err := ProbeAudioFile(file.Path)
if err != nil {
continue
}
@@ -48,8 +65,8 @@ func ScanDirectory(dirPath string) ([]ScannedSong, error) {
return songs, nil
}
func ListAudioFiles(dirPath string) ([]string, error) {
paths := []string{}
func ListAudioFiles(dirPath string) ([]DiscoveredAudioFile, error) {
files := []DiscoveredAudioFile{}
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
@@ -64,11 +81,15 @@ func ListAudioFiles(dirPath string) ([]string, error) {
return nil
}
paths = append(paths, path)
files = append(files, DiscoveredAudioFile{
Path: path,
FileSize: info.Size(),
FileMtimeNs: info.ModTime().UnixNano(),
})
return nil
})
return paths, err
return files, err
}
func ProbeAudioFile(filePath string) (ScannedSong, error) {
@@ -80,7 +101,7 @@ func ProbeAudioFile(filePath string) (ScannedSong, error) {
}
func processAudioFile(filePath string) (ScannedSong, error) {
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", filePath)
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", filePath)
output, err := cmd.Output()
if err != nil {
return ScannedSong{}, fmt.Errorf("ffprobe failed for %s: %v", filePath, err)
@@ -118,12 +139,38 @@ func processAudioFile(filePath string) (ScannedSong, error) {
}
}
bitRate := 0
if probeOutput.Format.BitRate != "" {
if value, err := strconv.Atoi(probeOutput.Format.BitRate); err == nil {
bitRate = value
}
}
sampleRate := 0
for _, stream := range probeOutput.Streams {
if stream.CodecType != "audio" || stream.SampleRate == "" {
continue
}
if value, err := strconv.Atoi(stream.SampleRate); err == nil {
sampleRate = value
break
}
}
format := probeOutput.Format.FormatName
if format == "" {
format = strings.TrimPrefix(strings.ToLower(filepath.Ext(filePath)), ".")
}
return ScannedSong{
Title: title,
Artist: artist,
Album: album,
Duration: duration,
Path: filePath,
Title: title,
Artist: artist,
Album: album,
Duration: duration,
Path: filePath,
Format: format,
BitRate: bitRate,
SampleRate: sampleRate,
}, nil
}