use lofty::{ config::WriteOptions, picture::{MimeType, Picture, PictureType}, prelude::*, probe::Probe, tag::Tag, }; use std::path::Path; 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) .expect("couldn't open the flac :(\n") .read() .expect("couldn't read the flac :(\n"); let tag = match tagged_file.primary_tag_mut() { Some(t) => t, None => { if let Some(first_tag) = tagged_file.first_tag_mut() { first_tag } else { let tag_type = tagged_file.primary_tag_type(); tagged_file.insert_tag(Tag::new(tag_type)); tagged_file.primary_tag_mut().unwrap() } } }; tag.set_title(title.to_string()); tag.set_artist(artist.to_string()); tag.set_album(album.to_string()); 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()) .expect("couldn't save the tags :(\n"); }