cover art

This commit is contained in:
2026-08-24 16:46:42 -04:00
parent e6769164d6
commit 6b45477a9d
4 changed files with 49 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use crate::{import, musicbrainz::get_disc_info, tagging}; use crate::{coverartarchive::fetch_cover_art, import, musicbrainz::get_disc_info, tagging};
// adopt = rip a disc but ripping sounds like destroying so i'm not using it // adopt = rip a disc but ripping sounds like destroying so i'm not using it
pub fn adopt(timbre_dir: &Path) { pub fn adopt(timbre_dir: &Path) {
@@ -79,12 +79,12 @@ pub fn adopt(timbre_dir: &Path) {
} }
child.wait().expect("cdparanoia didn't finish cleanly :(\n"); child.wait().expect("cdparanoia didn't finish cleanly :(\n");
let flac_name = format!("track{track_num:02}.flac"); let wav_path = tmp_dir.join(format!("track{track_num:02}.wav"));
let flac_path = tmp_dir.join(&flac_name); let flac_path = tmp_dir.join(format!("track{track_num:02}.flac"));
Command::new("flac") Command::new("flac")
.arg("--best") // max compression, lossless .arg("--best") // max compression, lossless
.arg(&flac_path) // input WAV .arg(&wav_path) // input WAV
.output() .output()
.expect("flac wouldn't encode :( is it installed?\n"); .expect("flac wouldn't encode :( is it installed?\n");
@@ -94,6 +94,7 @@ pub fn adopt(timbre_dir: &Path) {
&current_track.artist_credit[0].name, &current_track.artist_credit[0].name,
&release.title, &release.title,
track_num as u32, track_num as u32,
fetch_cover_art(&release.id).as_deref(),
); );
import::import_file(&flac_path, timbre_dir); import::import_file(&flac_path, timbre_dir);

19
src/coverartarchive.rs Normal file
View File

@@ -0,0 +1,19 @@
use std::io::Read;
pub fn fetch_cover_art(release_id: &str) -> Option<Vec<u8>> {
let url = format!("https://coverartarchive.org/release/{}/front", release_id);
let response = ureq::get(&url)
.header("User-Agent", "Timbre/0.1 ( ...your repo... )")
.call();
match response {
Ok(resp) => {
let mut bytes = Vec::new();
resp.into_body()
.into_reader()
.read_to_end(&mut bytes)
.expect("i couldn't read the cover art :(\n");
Some(bytes)
}
Err(_) => None,
}
}

View File

@@ -1,8 +1,10 @@
mod adopt; mod adopt;
mod coverartarchive;
mod disc_id; mod disc_id;
mod import; mod import;
mod musicbrainz; mod musicbrainz;
mod tagging; mod tagging;
use clap::{ use clap::{
Parser, Subcommand, Parser, Subcommand,
builder::styling::{AnsiColor, Effects, Styles}, builder::styling::{AnsiColor, Effects, Styles},

View File

@@ -1,7 +1,20 @@
use lofty::{config::WriteOptions, prelude::*, probe::Probe, tag::Tag}; use lofty::{
config::WriteOptions,
picture::{MimeType, Picture, PictureType},
prelude::*,
probe::Probe,
tag::Tag,
};
use std::path::Path; use std::path::Path;
pub fn write_tags(path: &Path, title: &str, artist: &str, album: &str, track_num: u32) { pub fn write_tags(
path: &Path,
title: &str,
artist: &str,
album: &str,
track_num: u32,
cover: Option<&[u8]>,
) {
let mut tagged_file = Probe::open(path) let mut tagged_file = Probe::open(path)
.expect("couldn't open the flac :(\n") .expect("couldn't open the flac :(\n")
.read() .read()
@@ -24,6 +37,14 @@ pub fn write_tags(path: &Path, title: &str, artist: &str, album: &str, track_num
tag.set_album(album.to_string()); tag.set_album(album.to_string());
tag.set_track(track_num); tag.set_track(track_num);
if let Some(image_bytes) = cover {
let picture = Picture::unchecked(image_bytes.to_vec())
.pic_type(PictureType::CoverFront)
.mime_type(MimeType::Jpeg)
.build();
tag.set_picture(0, picture);
}
tag.save_to_path(path, WriteOptions::default()) tag.save_to_path(path, WriteOptions::default())
.expect("couldn't save the tags :(\n"); .expect("couldn't save the tags :(\n");
} }