2019-02-24 09:23:58 +00:00
|
|
|
use std::path::Path;
|
2019-02-16 08:52:45 +00:00
|
|
|
use std::error::Error;
|
|
|
|
|
2019-02-24 09:23:58 +00:00
|
|
|
pub fn embed_image(music_filename: &Path, image_filename: &Path) -> Result<(), Box<Error>> {
|
2019-02-16 08:52:45 +00:00
|
|
|
let mut tag = id3::Tag::read_from_path(&music_filename).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
let image = image::open(&image_filename).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error reading image {:?}: {}", image_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
let mut encoded_image_bytes = Vec::new();
|
|
|
|
// Unwrap: Writing to a Vec should always succeed;
|
|
|
|
image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::JPEG(90)).unwrap();
|
|
|
|
|
|
|
|
tag.add_picture(id3::frame::Picture {
|
|
|
|
mime_type: "image/jpeg".to_string(),
|
2019-02-17 14:11:07 +00:00
|
|
|
picture_type: id3::frame::PictureType::CoverFront,
|
2019-02-16 08:52:45 +00:00
|
|
|
description: String::new(),
|
|
|
|
data: encoded_image_bytes,
|
|
|
|
});
|
|
|
|
|
|
|
|
tag.write_to_path(music_filename, id3::Version::Id3v23).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error writing image to music file {:?}: {}", music_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:20:14 +00:00
|
|
|
pub fn extract_first_image(music_filename: &Path, image_filename: &Path) -> Result<(), Box<Error>> {
|
2019-02-16 08:52:45 +00:00
|
|
|
let tag = id3::Tag::read_from_path(&music_filename).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
let first_picture = tag.pictures().next();
|
|
|
|
|
|
|
|
if let Some(p) = first_picture {
|
|
|
|
match image::load_from_memory(&p.data) {
|
|
|
|
Ok(image) => {
|
2019-02-17 19:55:04 +00:00
|
|
|
image.save(&image_filename).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Couldn't write image file {:?}: {}", image_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
},
|
|
|
|
Err(e) => return Err(format!("Couldn't load image: {}", e).into()),
|
|
|
|
};
|
|
|
|
|
2019-02-24 15:13:28 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("No image found in music file".into())
|
|
|
|
}
|
2019-02-16 08:52:45 +00:00
|
|
|
}
|
2019-02-17 19:55:04 +00:00
|
|
|
|
2019-02-24 09:23:58 +00:00
|
|
|
pub fn remove_images(music_filename: &Path) -> Result<(), Box<Error>> {
|
2019-02-17 19:55:04 +00:00
|
|
|
let mut tag = id3::Tag::read_from_path(&music_filename).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error reading music file {:?}: {}", music_filename, e))?;
|
2019-02-17 19:55:04 +00:00
|
|
|
|
|
|
|
tag.remove("APIC");
|
|
|
|
|
|
|
|
tag.write_to_path(music_filename, id3::Version::Id3v23).
|
2019-02-24 09:23:58 +00:00
|
|
|
map_err(|e| format!("Error updating music file {:?}: {}", music_filename, e))?;
|
2019-02-17 19:55:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|