Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Dec 13, 2023
1 parent 53400a5 commit 0d8a5cb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
42 changes: 32 additions & 10 deletions garage-cli/src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down Expand Up @@ -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(),
Expand All @@ -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) {
Expand All @@ -78,27 +88,39 @@ pub fn parse_ffmpeg_cmd(sub_cmd: &str, args: &ArgMatches) {
.to_owned();
let input_format = args.get_one::<String>("input_format").expect("").to_owned();
let output_path = args.get_one::<PathBuf>("output_path").expect("").to_owned();
let output_formart = args
let output_format = args
.get_one::<String>("output_format")
.expect("")
.to_owned();
let advance = args.try_get_one::<String>("advance").expect("").to_owned();
let exec = args.get_flag("exec");
// println!("input path: {:?}", input_path);

match sub_cmd {
"convert" => {
let opt = BatchffmpegOptions::new()
.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::<PathBuf>("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!()
}
Expand Down
47 changes: 42 additions & 5 deletions garage-ffmpeg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod option;
use std::fmt::format;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -77,23 +78,59 @@ 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])
}
}
}

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<String> {
let mut file_list: Vec<String> = 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
}
}
7 changes: 7 additions & 0 deletions garage-ffmpeg/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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
Expand Down

0 comments on commit 0d8a5cb

Please sign in to comment.