From fe55238b100a903f78df4abf6f224c95d35f3d05 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 8 May 2023 10:27:06 +0200 Subject: [PATCH] fix: changed some config behaviour. --- config.example.toml | 4 ++-- src/configuration/ffmpeg.rs | 4 ++++ src/configuration/mod.rs | 21 ++++++------------ src/ffmpeg.rs | 43 +++++++++++++------------------------ src/main.rs | 11 +++++++--- src/transcode/job.rs | 8 +++---- 6 files changed, 39 insertions(+), 52 deletions(-) diff --git a/config.example.toml b/config.example.toml index 64cd838..4a7578e 100644 --- a/config.example.toml +++ b/config.example.toml @@ -2,7 +2,6 @@ input_path = "/data/input" output_path = "/data/output" include = [ 'mp4', 'avi', 'mkv' ] # file extensions to include -keep_file_structure = true # e.g. /data/input/foo/bar.mp4 -> /data/output/foo/bar.webm [files.cleanup] enabled = true # do cleanup? @@ -10,13 +9,14 @@ original_cleanup_behavior = "delete" # delete, archive or keep [files.cleanup.archive] path = "/data/archive" -keep_file_structure = true # e.g. /data/input/foo/bar.mp4 -> /data/archive/foo/bar.mp4 [files.cleanup.delete] remove_empty_directories = true # if folder is empty after deleting file, delete folder [ffmpeg] path = "/usr/bin/ffmpeg" # path to ffmpeg executable +allow_overwrite = false # allow overwriting existing files +extra_args = [] [ffmpeg.process] niceness = 10 # 0 = highest priority diff --git a/src/configuration/ffmpeg.rs b/src/configuration/ffmpeg.rs index 7fb802d..2f3a0aa 100644 --- a/src/configuration/ffmpeg.rs +++ b/src/configuration/ffmpeg.rs @@ -14,6 +14,10 @@ impl ConfigFFmpeg { audio_codec: self.output.audio.codec.clone(), audio_bitrate: if self.output.audio.bitrate > 0 { Some(self.output.audio.bitrate) } else { None }, + overwrite: if self.overwrite.is_some() { Some(self.overwrite.unwrap()) } else { None }, + + extra_args: if self.extra_args.is_some() { self.extra_args.clone().unwrap() } else { Vec::new() }, + threads: if self.process.threads.unwrap_or(0) > 0 { Some(self.process.threads.unwrap_or(0)) } else { None }, niceness: if self.process.niceness.unwrap_or(0) > 0 { Some(self.process.niceness.unwrap_or(0)) } else { None }, } diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index fb9b9b7..2e3a2e5 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -13,7 +13,6 @@ pub enum ConfigFilesCleanupOriginalBehavior { #[derive(Serialize, Deserialize, Debug)] pub struct ConfigFilesCleanupArchive { pub path: String, - pub keep_file_structure: bool, } #[derive(Serialize, Deserialize, Debug)] @@ -31,11 +30,10 @@ pub struct ConfigFilesCleanup { #[derive(Serialize, Deserialize, Debug)] pub struct ConfigFiles { - pub keep_file_structure: bool, pub input_path: String, pub output_path: String, pub include: Vec, - pub cleanup: ConfigFilesCleanup, + pub cleanup: Option, } #[derive(Serialize, Deserialize, Debug)] @@ -67,6 +65,8 @@ pub struct ConfigFFmpegOutput { #[derive(Serialize, Deserialize, Debug)] pub struct ConfigFFmpeg { pub path: String, + pub extra_args: Option>, + pub overwrite: Option, pub process: ConfigFFmpegProcess, pub output: ConfigFFmpegOutput, } @@ -83,24 +83,15 @@ impl Config { Config { debug: None, files: ConfigFiles { - keep_file_structure: false, input_path: String::from("/data/input"), output_path: String::from("/data/output"), include: Vec::new(), - cleanup: ConfigFilesCleanup { - enabled: false, - original_cleanup_behavior: ConfigFilesCleanupOriginalBehavior::delete, - archive: ConfigFilesCleanupArchive { - path: String::from("/data/archive"), - keep_file_structure: false, - }, - delete: ConfigFilesCleanupDelete { - remove_empty_directories: false - }, - }, + cleanup: None }, ffmpeg: ConfigFFmpeg { path: String::from("/usr/bin/ffmpeg"), + extra_args: None, + overwrite: None, process: ConfigFFmpegProcess { threads: Some(0), niceness: Some(0), diff --git a/src/ffmpeg.rs b/src/ffmpeg.rs index 74adb63..0649c94 100644 --- a/src/ffmpeg.rs +++ b/src/ffmpeg.rs @@ -11,6 +11,10 @@ pub struct FFmpegCommandOptions { pub audio_codec: String, pub audio_bitrate: Option, + pub overwrite: Option, + + pub extra_args: Vec, + pub threads: Option, pub niceness: Option } @@ -50,39 +54,22 @@ impl FFmpegCommandOptions { args.push(niceness.to_string()); } + if let Some(allow_overwrite) = self.overwrite { + if allow_overwrite { + args.push("-y".to_string()); + } + } + + args.append(&mut self.extra_args.clone()); args.push(self.output.clone()); args } -} -pub fn build_command(program: &str, options: &FFmpegCommandOptions) -> Command { - let mut command = Command::new(program); - command.arg("-i").arg(&options.input); - command.arg("-c:v").arg(&options.video_codec); - command.arg("-c:a").arg(&options.audio_codec); - - if let Some(bitrate) = options.video_bitrate { - command.arg("-b:v").arg(bitrate.to_string()); + pub fn to_command(&self, ffmpeg_path: &str) -> Command { + let mut command = Command::new(ffmpeg_path); + command.args(self.to_args()); + command } - if let Some(crf) = options.video_crf { - command.arg("-crf").arg(crf.to_string()); - } - - if let Some(bitrate) = options.audio_bitrate { - command.arg("-b:a").arg(bitrate.to_string()); - } - - if let Some(threads) = options.threads { - command.arg("-threads").arg(threads.to_string()); - } - - if let Some(niceness) = options.niceness { - command.arg("-threads").arg(niceness.to_string()); - } - - command.arg(&options.output); - - command } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 98e0cb7..467979a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn main() { let mut success_count = 0; for file in input_files { - let mut fail = |job: &TranscodeJob, remarks: &str| { + let fail = |job: &TranscodeJob, remarks: &str| { error!("Failed to process file {}: {}", job.input, remarks); }; @@ -46,9 +46,14 @@ fn main() { output_path.to_str().unwrap() ); + // check if overwriting if job.check_if_exists() { - info!("Skipping conversion for {} to {} as the output file already exists.", job.input, job.output); - continue; + if config.ffmpeg.overwrite.unwrap_or(false) { + info!("Overwriting file {}.", job.input); + } else { + info!("Skipping file {} as it already exists.", job.input); + continue; + } } info!("Processing file {}.", job.input); diff --git a/src/transcode/job.rs b/src/transcode/job.rs index 4fe40a3..4db7f8c 100644 --- a/src/transcode/job.rs +++ b/src/transcode/job.rs @@ -17,10 +17,9 @@ impl TranscodeJob { } pub fn build_command(&self, ffmpeg_config: &ConfigFFmpeg) -> Command { - let options = ffmpeg_config.build_command_options(&self.input, &self.output); - let mut command = Command::new("ffmpeg"); - command.args(options.to_args()); - command + ffmpeg_config + .build_command_options(&self.input, &self.output) + .to_command(&ffmpeg_config.path) } pub fn check_if_exists(&self) -> bool { @@ -28,6 +27,7 @@ impl TranscodeJob { } pub fn run(&self, ffmpeg_config: &ConfigFFmpeg) -> Result { + debug!("Running job: {:#?}", &self.input); let mut command = self.build_command(ffmpeg_config); match command.spawn() { Ok(child) => Ok(child),