Album, playlist and multi-song editing

- Edit album: title, album artist, year, genre, and one cover (or none) for every song
- Edit info for several selected songs at once; untouched fields are never overwritten
- Edit playlist: name and cover image, saved as cover.jpg in the folder so other players show it
- Playlist covers appear on the playlist page, Home and cards
This commit is contained in:
Christian
2026-11-18 22:45:37 -05:00
parent ffec2710f8
commit 0cdf662dba
5 changed files with 169 additions and 19 deletions
+33
View File
@@ -580,3 +580,36 @@ mod loudness_tests {
assert_eq!(crate::library::parse_gain("-6.20 dB"), Some(-6.2));
}
}
#[tauri::command]
pub async fn tracks_edit(app: AppHandle, state: State<'_, AppState>, ids: Vec<String>, meta: TagFields) -> CmdResult<usize> {
let s = state.snapshot();
let ffmpeg = crate::engine::locate(&s.ytdlp_path, &s.ffmpeg_path).ok().and_then(|t| t.ffmpeg);
let (_, pl) = dirs(&state);
let mut done = 0;
let mut failed = Vec::new();
for (i, id) in ids.iter().enumerate() {
let Some(t) = state.library.track(id) else { continue };
match tagging::apply(Path::new(&t.path), &meta, "", ffmpeg.as_deref()).await {
Ok(()) => {
let _ = state.library.refresh(id, &pl);
done += 1;
}
Err(e) => failed.push(format!("{} ({e})", t.title)),
}
let _ = app.emit(LIBRARY_EVENT, serde_json::json!({ "type": "editing", "done": i + 1, "total": ids.len() }));
}
changed(&app);
if done == 0 && !failed.is_empty() {
return Err(format!("Couldn't update {}", failed.join(", ")));
}
Ok(done)
}
#[tauri::command]
pub fn playlist_cover(app: AppHandle, state: State<'_, AppState>, name: String, path: Option<String>) -> CmdResult<()> {
let (_, pl) = dirs(&state);
state.library.playlist_cover(&pl, &name, path.as_deref().map(Path::new)).map_err(err)?;
changed(&app);
Ok(())
}
+36 -1
View File
@@ -83,6 +83,19 @@ pub struct Playlist {
pub order: Vec<String>,
pub linked: bool,
pub missing: bool,
pub cover: Option<String>,
}
pub const COVER_NAMES: [&str; 4] = ["cover.jpg", "folder.jpg", "cover.png", "folder.png"];
fn folder_cover(folder: &Path) -> Option<String> {
let file = COVER_NAMES.iter().map(|n| folder.join(n)).find(|p| p.is_file())?;
let (size, mtime) = file_stamp(&file)?;
let key = hash(&format!("{}|{size}|{mtime}", file.to_string_lossy()));
if art_dir().join(format!("{key}.jpg")).exists() {
return Some(key);
}
save_art(&key, &std::fs::read(&file).ok()?)
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
@@ -372,7 +385,8 @@ impl Library {
let rel = |t: &Track| Path::new(&t.path).strip_prefix(&folder).map(|r| r.to_string_lossy().replace('\\', "/")).unwrap_or_default();
members.sort_by_key(|t| (order.iter().position(|o| *o == rel(t)).unwrap_or(usize::MAX), rel(t).to_lowercase()));
let missing = linked && !folder.is_dir();
out.push(Playlist { name, path: folder.to_string_lossy().into_owned(), order: members.iter().map(|t| t.id.clone()).collect(), linked, missing });
let cover = if missing { None } else { folder_cover(&folder) };
out.push(Playlist { name, path: folder.to_string_lossy().into_owned(), order: members.iter().map(|t| t.id.clone()).collect(), linked, missing, cover });
}
out.sort_by_key(|p| p.name.to_lowercase());
out
@@ -793,6 +807,27 @@ impl Library {
Ok(out)
}
pub fn playlist_cover(&self, dir: &Path, name: &str, image: Option<&Path>) -> Result<()> {
let folder = self.folder(dir, name);
if !folder.is_dir() {
bail!("That playlist's folder can't be found.");
}
for n in COVER_NAMES {
let p = folder.join(n);
if p.is_file() {
trash::delete(&p).map_err(|e| anyhow!("Couldn't remove the old cover ({e})"))?;
}
}
if let Some(src) = image {
let img = image::open(src).context("That image couldn't be read")?;
let img = if img.width() > 1200 || img.height() > 1200 { img.thumbnail(1200, 1200) } else { img };
let mut buf = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, 90).encode_image(&img.to_rgb8()).context("Couldn't save the cover")?;
std::fs::write(folder.join("cover.jpg"), buf).context("Couldn't save the cover")?;
}
Ok(())
}
pub fn set_gain(&self, id: &str, gain: f32) {
let changed = self.tracks.write().unwrap().get_mut(id).map(|t| t.gain_db = Some(gain)).is_some();
if changed {
+2
View File
@@ -533,6 +533,8 @@ fn main() {
libcmd::set_fullscreen,
libcmd::playlist_link,
libcmd::track_loudness,
libcmd::tracks_edit,
libcmd::playlist_cover,
libcmd::playlist_relocate,
libcmd::playlist_unlink
])