Skip to content

Commit dd95d2b

Browse files
author
Jonathan Frankel
committed
Better threading with rayon
1 parent 64a70b9 commit dd95d2b

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ image = "0.21.2"
1818
imageproc = "0.18.0"
1919
indicatif = "0.11.0"
2020
log = "0.4.8"
21+
num_cpus = "1.0"
2122
palette = "0.4.1"
2223
rand = "0.7.0"
2324
rayon = "1.1.0"

src/args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct Args {
4444
pub end_delay_percent: f32,
4545

4646
/// Do not process files that end with the given extensions.
47-
#[structopt(long, default_value = "vec![\"nfo\", \"jpg\", \"png\", \"srt\", \"txt\"]")]
47+
#[structopt(long)]
4848
pub exclude_extensions: Vec<String>,
4949

5050
/// Fast mode. Just make a contact sheet as fast as possible, regardless of output image quality. May mess up the terminal.
@@ -223,7 +223,7 @@ pub struct Args {
223223
#[structopt(long = "width", short = "w", default_value = "1500", required = false)]
224224
pub vcs_width: u32,
225225

226-
/// display verbose messages
226+
/// log to stdout as well as to the log file.
227227
#[structopt(long, short)]
228228
pub verbose: bool,
229229
}

src/main.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate exitcode;
77
extern crate image;
88
#[macro_use]
99
extern crate log;
10+
extern crate num_cpus;
1011
extern crate palette;
1112
extern crate rand;
1213
extern crate rayon;
@@ -20,14 +21,13 @@ mod errors;
2021
mod models;
2122
mod operations;
2223

23-
use flexi_logger::{Logger, opt_format};
24+
use flexi_logger::{opt_format, Cleanup, Criterion, Logger, Naming};
2425
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
2526
use std::{
2627
ffi::OsStr,
2728
io,
2829
path::{Path, PathBuf},
2930
process::{Command, Stdio},
30-
thread,
3131
};
3232
use walkdir::{DirEntry, WalkDir};
3333

@@ -52,12 +52,17 @@ use walkdir::{DirEntry, WalkDir};
5252
fn main() {
5353
let args = args::application_args();
5454

55-
Logger::with_str("vcsr=debug,info,warn")
56-
.log_to_file()
57-
.duplicate_to_stderr(flexi_logger::Duplicate::Info)
58-
.directory(dirs::home_dir().unwrap().join(".vcsr").join("logs"))
59-
.format(opt_format).start()
60-
.unwrap();
55+
let log_level = match &args.verbose {
56+
true => String::from("debug,info,warn,error"),
57+
false => String::from("info,warn,error"),
58+
};
59+
Logger::with_str(log_level)
60+
.log_to_file()
61+
.directory(dirs::home_dir().unwrap().join(".vcsr").join("logs"))
62+
.rotate(Criterion::Size(20000), Naming::Numbers, Cleanup::Never)
63+
.format(opt_format)
64+
.start()
65+
.unwrap();
6166

6267
let multi = MultiProgress::new();
6368
let bar_style = ProgressStyle::default_bar()
@@ -70,14 +75,17 @@ fn main() {
7075
.stderr(Stdio::null())
7176
.output()
7277
{
73-
Ok(_) => debug!("ffmpeg installed. Continuing."),
78+
Ok(_) => info!("ffmpeg installed. Continuing."),
7479
Err(_) => {
7580
error!("ffmpeg not installed. Exiting.");
7681
std::process::exit(exitcode::SOFTWARE)
7782
}
7883
};
7984
let mut walker: WalkDir;
80-
85+
let pool = rayon::ThreadPoolBuilder::new()
86+
.num_threads(num_cpus::get() * 2)
87+
.build()
88+
.unwrap();
8189
for path in &args.filenames {
8290
if !Path::new(path).exists() {
8391
error!("File does not exist, trying next: {}", path);
@@ -107,18 +115,22 @@ fn main() {
107115
bar.set_style(bar_style.clone());
108116
let mut current_args = args.clone();
109117
let entry_copy = entry.clone();
110-
let _ = thread::spawn(move || match process_file(entry, &mut current_args, &bar) {
118+
let _ = pool.spawn(move || match process_file(entry, &mut current_args, &bar) {
111119
Ok(file_name) => {
112120
let m = format!(
113121
"succesfully created {}",
114-
file_name.file_name().unwrap().to_string_lossy());
122+
file_name.file_name().unwrap().to_string_lossy()
123+
);
115124

116-
debug!("{}", &m);
125+
info!("{}", &m);
117126
bar.finish_with_message(&m);
118127
}
119128
Err(err) => {
120-
error!("Skipped {}: {}", entry_copy.file_name().to_string_lossy().to_owned(), err.to_string());
121-
// std::process::exit(-1);
129+
error!(
130+
"Skipped {}: {}",
131+
entry_copy.file_name().to_string_lossy().to_owned(),
132+
err.to_string()
133+
);
122134
}
123135
});
124136
}
@@ -140,7 +152,7 @@ fn process_file(
140152

141153
if !dir_entry.path().exists() {
142154
if args.ignore_errors {
143-
debug!("File does not exist, skipping {}: ", file_name_str);
155+
info!("File does not exist, skipping {}: ", file_name_str);
144156
return Ok(dir_entry.path().to_path_buf());
145157
} else {
146158
return Err(errors::CustomError::Io(io::Error::new(
@@ -171,7 +183,7 @@ fn process_file(
171183

172184
if args.no_overwrite {
173185
if Path::new(&output_path).exists() {
174-
debug!(
186+
info!(
175187
"contact sheet already exists, skipping {}",
176188
&output_path.to_string_lossy().to_owned().to_owned()
177189
);
@@ -337,7 +349,7 @@ fn process_file(
337349
if !Path::new(thumbnail_output_path).exists() {
338350
std::fs::create_dir_all(thumbnail_output_path)?;
339351
}
340-
debug!("Copying thumbnails to {}", thumbnail_output_path);
352+
info!("Copying thumbnails to {}", thumbnail_output_path);
341353
for (i, frame) in selected_frames.iter().enumerate() {
342354
let thumbnail_file_extension = Path::new(&frame.filename).extension().unwrap();
343355
let thumbnail_filename = format!(

src/models.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ impl MediaInfo {
112112
.output()?;
113113

114114
if let Ok(stdout) = str::from_utf8(&output.stdout) {
115-
let f: Ffprobe = serde_json::from_str(stdout).map_err(|e| CustomError::StreamError(e))?;
115+
let f: Ffprobe =
116+
serde_json::from_str(stdout).map_err(|e| CustomError::StreamError(e))?;
116117
Ok(f)
117118
} else {
118119
Err(CustomError::Io(io::Error::new(

0 commit comments

Comments
 (0)