iPod Music conversion tools (macOS Only)
at trunk 89 lines 2.3 kB view raw
1use clap::Parser; 2use std::{error::Error, path::PathBuf, process::Command}; 3use tempfile::TempPath; 4use walkdir::WalkDir; 5 6use crate::{ 7 art::{ArtArgs, add_art, is_flac}, 8 convert::{ToAacArgs, convert_to_aac}, 9}; 10 11mod art; 12mod convert; 13 14type Rail = Result<(), Box<dyn Error + Send + Sync>>; 15 16#[derive(Parser, Debug)] 17#[command(name = "podtools")] 18#[command(bin_name = "podtools")] 19enum AllCli { 20 /// Convert FLACs to AAC 21 ToAac(ToAacArgs), 22 23 /// Apply iPod-safe album art to AAC files 24 Art(ArtArgs), 25 26 /// FLAC to AAC, Album Art, Import 27 TheWorks(TheWorksArgs), 28} 29 30#[derive(clap::Args, Debug)] 31#[command(version, about, long_about = None)] 32pub struct TheWorksArgs { 33 #[arg(long)] 34 cover: PathBuf, 35 36 path: PathBuf, 37} 38 39fn main() -> Rail { 40 match AllCli::parse() { 41 AllCli::ToAac(to_aac_args) => convert_to_aac(to_aac_args)?, 42 AllCli::Art(args) => add_art(args)?, 43 AllCli::TheWorks(args) => { 44 println!("The works #1: Convert to AAC."); 45 convert_to_aac(ToAacArgs { 46 replace: false, 47 no_metadata: false, 48 recursive: true, 49 path: args.path.clone(), 50 })?; 51 52 println!("The works #2: Add art."); 53 add_art(ArtArgs { 54 recursive: true, 55 cover: args.cover, 56 path: args.path.clone(), 57 })?; 58 59 println!("The works #3: Import AACs into iTunes."); 60 let walker = WalkDir::new(&args.path); 61 let files: Result<Vec<_>, _> = walker.into_iter().filter_entry(is_flac).collect(); 62 let files = files?; 63 64 if files.is_empty() { 65 return Err("No files".into()); 66 } 67 68 for file in files { 69 if file.file_type().is_file() { 70 Command::new("open") 71 .args(["-a", "Music"]) 72 .arg(file.path()) 73 .output()?; 74 } 75 } 76 println!("Job done."); 77 } 78 } 79 80 Ok(()) 81} 82 83pub fn temp_file(suffix: Option<&str>) -> TempPath { 84 tempfile::Builder::new() 85 .suffix(suffix.unwrap_or("")) 86 .tempfile() 87 .unwrap() 88 .into_temp_path() 89}