2019-02-24 09:23:58 +00:00
|
|
|
use std::path::Path;
|
2021-02-22 19:56:59 +00:00
|
|
|
|
|
|
|
use anyhow::anyhow;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
2019-02-24 15:46:47 +00:00
|
|
|
/// Embed the image from `image_filename` into `music_filename`, in-place. Any errors reading ID3
|
2019-02-24 15:41:24 +00:00
|
|
|
/// tags from the music file or parsing the image get propagated upwards.
|
|
|
|
///
|
2019-02-24 15:46:47 +00:00
|
|
|
/// The image is encoded as a JPEG with a 90% quality setting, and embedded as a "Front cover".
|
|
|
|
/// Tags get written as ID3v2.3.
|
2019-02-24 15:41:24 +00:00
|
|
|
///
|
2021-02-22 19:56:59 +00:00
|
|
|
pub fn embed_image(music_filename: &Path, image_filename: &Path) -> anyhow::Result<()> {
|
|
|
|
let mut tag = read_tag(music_filename)?;
|
2019-02-16 08:52:45 +00:00
|
|
|
let image = image::open(&image_filename).
|
2021-02-22 19:56:59 +00:00
|
|
|
map_err(|e| anyhow!("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;
|
2020-08-31 14:22:32 +00:00
|
|
|
image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::Jpeg(90)).unwrap();
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
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).
|
2021-02-22 19:56:59 +00:00
|
|
|
map_err(|e| anyhow!("Error writing image to music file {:?}: {}", music_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-24 15:46:47 +00:00
|
|
|
/// Extract the first found embedded image from `music_filename` and write it as a file with the
|
|
|
|
/// given `image_filename`. The image file will be silently overwritten if it exists.
|
2019-02-24 15:41:24 +00:00
|
|
|
///
|
|
|
|
/// Any errors from parsing id3 tags will be propagated. The function will also return an error if
|
|
|
|
/// there's no embedded images in the mp3 file.
|
|
|
|
///
|
2021-02-22 19:56:59 +00:00
|
|
|
pub fn extract_first_image(music_filename: &Path, image_filename: &Path) -> anyhow::Result<()> {
|
|
|
|
let tag = read_tag(music_filename)?;
|
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).
|
2021-02-22 19:56:59 +00:00
|
|
|
map_err(|e| anyhow!("Couldn't write image file {:?}: {}", image_filename, e))?;
|
2019-02-16 08:52:45 +00:00
|
|
|
},
|
2021-02-22 19:56:59 +00:00
|
|
|
Err(e) => return Err(anyhow!("Couldn't load image: {}", e)),
|
2019-02-16 08:52:45 +00:00
|
|
|
};
|
|
|
|
|
2019-02-24 15:13:28 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-02-22 19:56:59 +00:00
|
|
|
Err(anyhow!("No image found in music file"))
|
2019-02-24 15:13:28 +00:00
|
|
|
}
|
2019-02-16 08:52:45 +00:00
|
|
|
}
|
2019-02-17 19:55:04 +00:00
|
|
|
|
2019-02-24 15:41:24 +00:00
|
|
|
/// Remove all embedded images from the given `music_filename`. In effect, this removes all tags of
|
|
|
|
/// type "APIC".
|
|
|
|
///
|
|
|
|
/// If the mp3 file's ID3 tags can't be parsed, the error will be propagated upwards.
|
|
|
|
///
|
2021-02-22 19:56:59 +00:00
|
|
|
pub fn remove_images(music_filename: &Path) -> anyhow::Result<()> {
|
|
|
|
let mut tag = read_tag(music_filename)?;
|
2019-02-17 19:55:04 +00:00
|
|
|
tag.remove("APIC");
|
|
|
|
|
|
|
|
tag.write_to_path(music_filename, id3::Version::Id3v23).
|
2021-02-22 19:56:59 +00:00
|
|
|
map_err(|e| anyhow!("Error updating music file {:?}: {}", music_filename, e))?;
|
2019-02-17 19:55:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-22 19:56:59 +00:00
|
|
|
|
|
|
|
fn read_tag(path: &Path) -> anyhow::Result<id3::Tag> {
|
|
|
|
id3::Tag::read_from_path(&path).or_else(|e| {
|
|
|
|
eprintln!("Warning: file metadata is corrupted, trying to read partial tag: {}", path.display());
|
|
|
|
e.partial_tag.clone().ok_or_else(|| anyhow!("Error reading music file {:?}: {}", path, e))
|
|
|
|
})
|
|
|
|
}
|