1#![warn(missing_docs)]
2
3use std::path::Path;
7
8use anyhow::anyhow;
9use id3::TagLike;
10
11pub fn embed_image(music_filename: &Path, image_filename: &Path) -> anyhow::Result<()> {
18 let mut tag = read_tag(music_filename)?;
19 let image = image::open(&image_filename).
20 map_err(|e| anyhow!("Error reading image {:?}: {}", image_filename, e))?;
21
22 let mut encoded_image_bytes = Vec::new();
23 image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::Jpeg(90)).unwrap();
25
26 tag.add_frame(id3::frame::Picture {
27 mime_type: "image/jpeg".to_string(),
28 picture_type: id3::frame::PictureType::CoverFront,
29 description: String::new(),
30 data: encoded_image_bytes,
31 });
32
33 tag.write_to_path(music_filename, id3::Version::Id3v23).
34 map_err(|e| anyhow!("Error writing image to music file {:?}: {}", music_filename, e))?;
35
36 Ok(())
37}
38
39pub fn extract_first_image(music_filename: &Path, image_filename: &Path) -> anyhow::Result<()> {
46 let tag = read_tag(music_filename)?;
47 let first_picture = tag.pictures().next();
48
49 if let Some(p) = first_picture {
50 match image::load_from_memory(&p.data) {
51 Ok(image) => {
52 image.save(&image_filename).
53 map_err(|e| anyhow!("Couldn't write image file {:?}: {}", image_filename, e))?;
54 },
55 Err(e) => return Err(anyhow!("Couldn't load image: {}", e)),
56 };
57
58 Ok(())
59 } else {
60 Err(anyhow!("No image found in music file"))
61 }
62}
63
64pub fn remove_images(music_filename: &Path) -> anyhow::Result<()> {
70 let mut tag = read_tag(music_filename)?;
71 tag.remove("APIC");
72
73 tag.write_to_path(music_filename, id3::Version::Id3v23).
74 map_err(|e| anyhow!("Error updating music file {:?}: {}", music_filename, e))?;
75
76 Ok(())
77}
78
79fn read_tag(path: &Path) -> anyhow::Result<id3::Tag> {
80 id3::Tag::read_from_path(&path).or_else(|e| {
81 eprintln!("Warning: file metadata is corrupted, trying to read partial tag: {}", path.display());
82 e.partial_tag.clone().ok_or_else(|| anyhow!("Error reading music file {:?}: {}", path, e))
83 })
84}