1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::path::Path;
use std::error::Error;
pub fn embed_image(music_filename: &Path, image_filename: &Path) -> Result<(), Box<Error>> {
let mut tag = id3::Tag::read_from_path(&music_filename).
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
let image = image::open(&image_filename).
map_err(|e| format!("Error reading image {:?}: {}", image_filename, e))?;
let mut encoded_image_bytes = Vec::new();
image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::JPEG(90)).unwrap();
tag.add_picture(id3::frame::Picture {
mime_type: "image/jpeg".to_string(),
picture_type: id3::frame::PictureType::CoverFront,
description: String::new(),
data: encoded_image_bytes,
});
tag.write_to_path(music_filename, id3::Version::Id3v23).
map_err(|e| format!("Error writing image to music file {:?}: {}", music_filename, e))?;
Ok(())
}
pub fn extract_first_image(music_filename: &Path, image_filename: &Path) -> Result<(), Box<Error>> {
let tag = id3::Tag::read_from_path(&music_filename).
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
let first_picture = tag.pictures().next();
if let Some(p) = first_picture {
match image::load_from_memory(&p.data) {
Ok(image) => {
image.save(&image_filename).
map_err(|e| format!("Couldn't write image file {:?}: {}", image_filename, e))?;
},
Err(e) => return Err(format!("Couldn't load image: {}", e).into()),
};
Ok(())
} else {
Err("No image found in music file".into())
}
}
pub fn remove_images(music_filename: &Path) -> Result<(), Box<Error>> {
let mut tag = id3::Tag::read_from_path(&music_filename).
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
tag.remove("APIC");
tag.write_to_path(music_filename, id3::Version::Id3v23).
map_err(|e| format!("Error updating music file {:?}: {}", music_filename, e))?;
Ok(())
}