fix: changed some config behaviour.

This commit is contained in:
Didier Slof 2023-05-08 10:27:06 +02:00
parent ea076dcf72
commit fe55238b10
Signed by: didier
GPG Key ID: 01E71F18AA4398E5
6 changed files with 39 additions and 52 deletions

View File

@ -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

View File

@ -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 },
}

View File

@ -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<String>,
pub cleanup: ConfigFilesCleanup,
pub cleanup: Option<ConfigFilesCleanup>,
}
#[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<Vec<String>>,
pub overwrite: Option<bool>,
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),

View File

@ -11,6 +11,10 @@ pub struct FFmpegCommandOptions {
pub audio_codec: String,
pub audio_bitrate: Option<u32>,
pub overwrite: Option<bool>,
pub extra_args: Vec<String>,
pub threads: Option<u8>,
pub niceness: Option<u8>
}
@ -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
}

View File

@ -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);

View File

@ -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<Child, error::Error> {
debug!("Running job: {:#?}", &self.input);
let mut command = self.build_command(ffmpeg_config);
match command.spawn() {
Ok(child) => Ok(child),