|
| 1 | +package media |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + stdpath "path" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/OpenListTeam/OpenList/v4/internal/fs" |
| 12 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 13 | +) |
| 14 | + |
| 15 | +// FillMusicFromLocal 在刮削前/失败时,使用音乐文件本身的元数据(ID3 / Vorbis Comment) |
| 16 | +// 以及同目录 .lrc 歌词,**只填充 item 中为空的字段**。 |
| 17 | +// |
| 18 | +// 适用场景: |
| 19 | +// 1. 用户清空刮削后重新刮削,原本由扫描阶段填入的本地数据(封面/歌词等)已丢失 |
| 20 | +// 2. 在线刮削失败,退而求其次用文件本地信息补全 |
| 21 | +// |
| 22 | +// 行为: |
| 23 | +// - is_folder=true 的合并文件夹模式:使用 episodes 中第一首音乐文件读取 |
| 24 | +// - is_folder=false:使用 folder_path/file_name 直接读取 |
| 25 | +// |
| 26 | +// 错误以静默忽略为主——本地兜底失败不应阻塞主流程。 |
| 27 | +func FillMusicFromLocal(ctx context.Context, item *model.MediaItem) { |
| 28 | + if item == nil || item.MediaType != model.MediaTypeMusic { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + musicPath := pickMusicFilePath(item) |
| 33 | + if musicPath == "" { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // 解析 tag(mp3 / flac / 其它) |
| 38 | + ext := strings.ToLower(stdpath.Ext(musicPath)) |
| 39 | + var tag *MusicTag |
| 40 | + readCtx, readCancel := context.WithTimeout(ctx, 15*time.Second) |
| 41 | + if reader := FetchFileReader(readCtx, musicPath); reader != nil { |
| 42 | + switch ext { |
| 43 | + case ".flac": |
| 44 | + tag, _ = ParseFLACVorbisComment(reader) |
| 45 | + default: |
| 46 | + tag, _ = ParseID3v2(reader) |
| 47 | + } |
| 48 | + _ = reader.Close() |
| 49 | + } |
| 50 | + readCancel() |
| 51 | + |
| 52 | + // 应用 tag 到空字段 |
| 53 | + if tag != nil { |
| 54 | + if item.AlbumName == "" && tag.Album != "" { |
| 55 | + item.AlbumName = tag.Album |
| 56 | + } |
| 57 | + if item.AlbumArtist == "" { |
| 58 | + if tag.AlbumArtist != "" { |
| 59 | + item.AlbumArtist = tag.AlbumArtist |
| 60 | + } else if tag.Artist != "" { |
| 61 | + item.AlbumArtist = tag.Artist |
| 62 | + } |
| 63 | + } |
| 64 | + if item.ScrapedName == "" { |
| 65 | + if tag.Title != "" { |
| 66 | + item.ScrapedName = tag.Title |
| 67 | + } else if item.AlbumName != "" { |
| 68 | + item.ScrapedName = item.AlbumName |
| 69 | + } |
| 70 | + } |
| 71 | + if item.TrackNumber == 0 && tag.TrackNumber > 0 { |
| 72 | + item.TrackNumber = tag.TrackNumber |
| 73 | + } |
| 74 | + if item.ReleaseDate == "" && len(tag.Year) >= 4 { |
| 75 | + item.ReleaseDate = tag.Year[:4] + "-01-01" |
| 76 | + } |
| 77 | + if item.Genre == "" && tag.Genre != "" { |
| 78 | + item.Genre = tag.Genre |
| 79 | + } |
| 80 | + if item.Authors == "" && tag.Artist != "" { |
| 81 | + if b, err := json.Marshal([]string{tag.Artist}); err == nil { |
| 82 | + item.Authors = string(b) |
| 83 | + } |
| 84 | + } |
| 85 | + if item.Cover == "" && len(tag.CoverData) > 0 { |
| 86 | + mimeType := tag.CoverMIME |
| 87 | + if mimeType == "" { |
| 88 | + mimeType = "image/jpeg" |
| 89 | + } |
| 90 | + item.Cover = "data:" + mimeType + ";base64," + base64.StdEncoding.EncodeToString(tag.CoverData) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // 歌词:若为空则尝试 .lrc 同名文件 / 内嵌歌词 |
| 95 | + if item.Lyrics == "" { |
| 96 | + if lrc := loadLyricsForMusic(ctx, musicPath, tag); lrc != "" { |
| 97 | + item.Lyrics = lrc |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// pickMusicFilePath 从 MediaItem 推断出可读取的音乐文件 VFS 路径 |
| 103 | +// 合并模式 (is_folder=true): 取 episodes 中第一首;解析失败回退到 folder_path/file_name |
| 104 | +// 普通模式: folder_path/file_name |
| 105 | +func pickMusicFilePath(item *model.MediaItem) string { |
| 106 | + if item == nil { |
| 107 | + return "" |
| 108 | + } |
| 109 | + if item.IsFolder { |
| 110 | + // episodes 是 JSON 数组:[{file_name,index,title,...}] |
| 111 | + if item.Episodes != "" { |
| 112 | + var eps []EpisodeInfo |
| 113 | + if err := json.Unmarshal([]byte(item.Episodes), &eps); err == nil && len(eps) > 0 { |
| 114 | + // 文件夹模式下 folder_path 是扫描根,file_name 是文件夹名 |
| 115 | + return stdpath.Join(item.FolderPath, item.FileName, eps[0].FileName) |
| 116 | + } |
| 117 | + } |
| 118 | + // 回退:尝试列出文件夹内第一首音乐 |
| 119 | + folder := stdpath.Join(item.FolderPath, item.FileName) |
| 120 | + if entries, err := fs.List(context.Background(), folder, &fs.ListArgs{NoLog: true}); err == nil { |
| 121 | + for _, e := range entries { |
| 122 | + if !e.IsDir() && isMediaFile(e.GetName(), model.MediaTypeMusic) { |
| 123 | + return stdpath.Join(folder, e.GetName()) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return "" |
| 128 | + } |
| 129 | + return stdpath.Join(item.FolderPath, item.FileName) |
| 130 | +} |
0 commit comments