From 5a0d224be76624c09773338876306a9d63c7b97c Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sun, 10 Feb 2019 12:52:23 +0800 Subject: [PATCH] Binary to extract image file --- src/bin/id3-image-extract.rs | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/bin/id3-image-extract.rs diff --git a/src/bin/id3-image-extract.rs b/src/bin/id3-image-extract.rs new file mode 100644 index 0000000..6cab190 --- /dev/null +++ b/src/bin/id3-image-extract.rs @@ -0,0 +1,44 @@ +use std::env; +use std::process; +use std::path::{Path, PathBuf}; + +fn main() { + let args: Vec<_> = env::args().collect(); + + if args.len() < 2 { + eprintln!("USAGE: id3-image-extract [image-file]"); + process::exit(1); + } + + 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); + } + }; + } +} + +fn replace_extension(path: &str, replacement: &str) -> String { + let mut path = PathBuf::from(&path); + path.set_extension(replacement); + path.to_string_lossy().to_string() +}