Extract logic into lib.rs
This commit is contained in:
parent
5a0d224be7
commit
03cb9fe656
@ -1,6 +1,8 @@
|
||||
use std::env;
|
||||
use std::process;
|
||||
|
||||
use id3_image::embed_image;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = env::args().collect();
|
||||
|
||||
@ -12,35 +14,8 @@ fn main() {
|
||||
let music_filename = args[1].clone();
|
||||
let image_filename = args[2].clone();
|
||||
|
||||
let mut tag = match id3::Tag::read_from_path(&music_filename) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading music file {}: {}", music_filename, e);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
let image = match image::open(&image_filename) {
|
||||
Ok(i) => i,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading image {}: {}", image_filename, e);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
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(),
|
||||
picture_type: id3::frame::PictureType::Other,
|
||||
description: String::new(),
|
||||
data: encoded_image_bytes,
|
||||
});
|
||||
|
||||
if let Err(e) = tag.write_to_path(music_filename, id3::Version::Id3v23) {
|
||||
eprintln!("Error writing image to file: {}", e);
|
||||
if let Err(e) = embed_image(&music_filename, &image_filename) {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
use std::env;
|
||||
use std::process;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use id3_image::extract_image;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = env::args().collect();
|
||||
@ -13,27 +15,9 @@ fn main() {
|
||||
let music_filename = args[1].clone();
|
||||
let image_filename = args.get(2).cloned().unwrap_or_else(|| replace_extension(&music_filename, "jpg"));
|
||||
|
||||
let tag = match id3::Tag::read_from_path(&music_filename) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading music file {}: {}", music_filename, e);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
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);
|
||||
println!("{}", image_filename);
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Couldn't load image: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
if let Err(e) = extract_image(&music_filename, &image_filename) {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
44
src/lib.rs
Normal file
44
src/lib.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use std::error::Error;
|
||||
|
||||
pub fn embed_image(music_filename: &str, image_filename: &str) -> 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();
|
||||
// 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(),
|
||||
picture_type: id3::frame::PictureType::Other,
|
||||
description: String::new(),
|
||||
data: encoded_image_bytes,
|
||||
});
|
||||
|
||||
tag.write_to_path(music_filename, id3::Version::Id3v23).
|
||||
map_err(|e| format!("Error writing image to file: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn extract_image(music_filename: &str, image_filename: &str) -> 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);
|
||||
println!("{}", image_filename);
|
||||
},
|
||||
Err(e) => return Err(format!("Couldn't load image: {}", e).into()),
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user