diff --git a/garage-cli/src/ffmpeg.rs b/garage-cli/src/ffmpeg.rs index 84d2d46..05eba04 100644 --- a/garage-cli/src/ffmpeg.rs +++ b/garage-cli/src/ffmpeg.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; struct Args { input_path: Arg, + font_path: Arg, input_format: Arg, output_path: Arg, output_format: Arg, @@ -23,8 +24,13 @@ pub fn ffmpeg_cmd() -> Command { .long("input_format") .value_name("FILE Extension") .value_parser(value_parser!(String)) - .default_value("mp4") .help("input directory path"), + font_path: Arg::new("font_path") + .long("font_path") + .value_name("FILE") + .value_parser(value_parser!(PathBuf)) + .required(true) + .help("input fonts directory path"), output_path: Arg::new("output_path") .long("output_path") .value_name("FILE") @@ -53,7 +59,7 @@ pub fn ffmpeg_cmd() -> Command { .about("ffmpeg batch") .subcommand(Command::new("convert").about("").args([ args.input_path.clone(), - args.input_format.clone(), + args.input_format.clone().default_value("mp4"), args.output_path.clone(), args.output_format.clone(), args.advance.clone(), @@ -64,11 +70,15 @@ pub fn ffmpeg_cmd() -> Command { .about("") .args([args.input_path.clone()]), ) - .subcommand( - Command::new("add_fonts") - .about("") - .args([args.input_path.clone()]), - ) + .subcommand(Command::new("add_fonts").about("").args([ + args.input_path.clone(), + args.input_format.clone().default_value("mkv"), + args.font_path.clone(), + args.output_path.clone(), + args.output_format.clone(), + args.advance.clone(), + args.exec.clone(), + ])) } pub fn parse_ffmpeg_cmd(sub_cmd: &str, args: &ArgMatches) { @@ -78,12 +88,13 @@ pub fn parse_ffmpeg_cmd(sub_cmd: &str, args: &ArgMatches) { .to_owned(); let input_format = args.get_one::("input_format").expect("").to_owned(); let output_path = args.get_one::("output_path").expect("").to_owned(); - let output_formart = args + let output_format = args .get_one::("output_format") .expect("") .to_owned(); let advance = args.try_get_one::("advance").expect("").to_owned(); let exec = args.get_flag("exec"); + // println!("input path: {:?}", input_path); match sub_cmd { "convert" => { @@ -91,14 +102,25 @@ pub fn parse_ffmpeg_cmd(sub_cmd: &str, args: &ArgMatches) { .input_path(input_path) .input_format(input_format) .output_path(output_path) - .output_format(output_formart) + .output_format(output_format) .advance(advance) .exec(exec); let f = Batchffmpeg::new(opt); f.convert(); } "add_sub" => {} - "add_fonts" => {} + "add_fonts" => { + let font_path = args.get_one::("font_path").expect("").to_owned(); + let opt = BatchffmpegOptions::new() + .input_path(input_path) + .input_format(input_format) + .output_path(output_path) + .font_path(font_path) + .advance(advance) + .exec(exec); + let f = Batchffmpeg::new(opt); + f.add_fonts(); + } _ => { todo!() } diff --git a/garage-ffmpeg/src/lib.rs b/garage-ffmpeg/src/lib.rs index 018c79b..6fd45e1 100644 --- a/garage-ffmpeg/src/lib.rs +++ b/garage-ffmpeg/src/lib.rs @@ -1,4 +1,5 @@ mod option; +use std::fmt::format; use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -77,15 +78,17 @@ impl Batchffmpeg { ]; args.push(arg); } - for arg in args { - if self.option.exec { + if self.option.exec { + for arg in args { let mut child = Command::new("ffmpeg") .args(arg) .stdout(Stdio::inherit()) .spawn() .expect("fail to execute"); child.wait().unwrap(); - } else { + } + } else { + for arg in args { info!("ffmpeg {} {} {}", arg[0], arg[1], arg[2]) } } @@ -93,7 +96,41 @@ impl Batchffmpeg { pub fn add_sub(&self) {} - pub fn add_fonts(&self) {} + pub fn add_fonts(&self) { + let video_list = self.get_video_list( + self.option.input_path.clone(), + self.option.input_format.clone(), + ); + let font_list = self.get_fonts_params(self.option.font_path.clone()); + for f in font_list { + println!("font: {}", f); + } + } - pub fn get_fonts_params(&self) {} + pub fn get_fonts_params(&self, font_path: PathBuf) -> Vec { + let mut file_list: Vec = Vec::new(); + let font_format_list = vec!["oft", "ttf"]; + for entry in WalkDir::new(font_path) { + match entry { + Err(err) => { + println!("error: {:?}", err); + } + Ok(dir_entry) => { + if dir_entry.file_type().is_file() { + match Path::new(dir_entry.file_name()).extension() { + Some(format) => { + let file_name = dir_entry.file_name().to_str().unwrap(); + let font_format = format.to_str().unwrap(); + if font_format_list.contains(&font_format) { + file_list.push(format!("{}.{}", file_name, font_format)) + } + } + None => {} + } + } + } + } + } + file_list + } } diff --git a/garage-ffmpeg/src/option.rs b/garage-ffmpeg/src/option.rs index 1e2cac3..49abc21 100644 --- a/garage-ffmpeg/src/option.rs +++ b/garage-ffmpeg/src/option.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; pub struct BatchffmpegOptions { pub input_path: PathBuf, pub input_format: String, + pub font_path: PathBuf, pub output_format: String, pub output_path: PathBuf, pub advance: String, @@ -15,6 +16,7 @@ impl BatchffmpegOptions { BatchffmpegOptions { input_path: PathBuf::new(), input_format: "".to_string(), + font_path: PathBuf::new(), output_path: PathBuf::new(), output_format: "".to_string(), advance: "".to_string(), @@ -32,6 +34,11 @@ impl BatchffmpegOptions { self } + pub fn font_path(mut self, font_path: PathBuf) -> Self { + self.font_path = font_path; + self + } + pub fn output_path(mut self, path: PathBuf) -> Self { self.output_path = self.output_path.join(path); self