Extract logic into lib.rs
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user