Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove glob crate #1

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 1 addition & 105 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffzap"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
description = "⚡ A multithreaded CLI for digital media processing using ffmpeg. If ffmpeg can do it, ffzap can do it - as many files in parallel as your CPU can handle."
license-file = "license.md"
Expand All @@ -11,5 +11,3 @@ repository = "https://github.com/CodeF0x/ffzap"

[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
glob = "0.3.1"
rand = "0.9.0-alpha.2"
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@ as a string and without the file name.
With a single file it doesn't really make sense to use ffzap, so consider this more advanced example:

```bash
ffzap -i "vids/**/*.{mp4,mkv}" -f "-c:v libx264 -b:v 1000k" -o transcoded/{{name}}.mp4 -t 4
ffzap -i vids/**/*.{mp4,mkv} -f "-c:v libx264 -b:v 1000k" -o transcoded/{{name}}.mp4 -t 4
```

This command takes all videos in `vids` and its subfolders ending in `.mp4` and `.mkv`, processes them using the
options provided by `-f` and saves them to a (new) directory called `transcoded`, keeping the original filename and
changing the file extension to `.mp4` while processing 4 files in parallel.

Notice that, when using glob patterns, `-i` (short for `--input-directory`) needs to be a string so that ffzap executes
the glob pattern and **not** your shell. Otherwise, the command will fail.

For more `-o` (short for `--output-directory`) options, run `ffzap --help`. For more ffmpeg options,
For more info on the `-o` syntax, run `ffzap --help`. For more ffmpeg options,
visit [ffmpeg's documentation](https://ffmpeg.org/ffmpeg.html).

### Requirements
Expand Down
39 changes: 21 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use clap::Parser;
use glob::glob;
use std::ffi::OsStr;
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
Expand All @@ -18,9 +17,9 @@ struct CmdArgs {
#[arg(short, long, allow_hyphen_values = true)]
ffmpeg_options: String,

/// the directory with all files you want to process. supports unix globs
#[arg(short, long)]
input_directory: String,
/// the files you want to process.
#[arg(short, long, num_args = 1.., value_delimiter = ' ')]
input_directory: Vec<String>,

/// Specify the output file pattern. Use placeholders to customize file paths:
///
Expand All @@ -41,19 +40,14 @@ struct CmdArgs {
fn main() {
let cmd_args = CmdArgs::parse();

let paths = Arc::new(Mutex::new(match glob(&cmd_args.input_directory) {
Ok(paths) => paths.filter_map(Result::ok).collect::<Vec<PathBuf>>(),
Err(err) => {
eprintln!("{}", err.msg);
std::process::exit(1);
}
}));
let paths = Arc::new(Mutex::new(cmd_args.input_directory));

let mut thread_handles = vec![];

for thread in 0..cmd_args.thread_count {
let paths: Arc<Mutex<Vec<PathBuf>>> = Arc::clone(&paths);
let args = cmd_args.clone();
let paths: Arc<Mutex<Vec<String>>> = Arc::clone(&paths);
let ffmpeg_options = cmd_args.ffmpeg_options.clone();
let output = cmd_args.output.clone();

let handle = thread::spawn(move || loop {
let path_to_process = {
Expand All @@ -64,12 +58,21 @@ fn main() {

match path_to_process {
Some(path) => {
let path = Path::new(&path);

if !path.is_file() {
eprintln!(
"[THREAD {thread}] -- {} doesn't appear to be a file, ignoring. Continuing with next task if there's more to do...",
path.to_str().unwrap()
);
continue;
}

println!("[THREAD {thread}] -- Processing {}", path.display());
let split_options = &mut args.ffmpeg_options.split(' ').collect::<Vec<&str>>();
let split_options = &mut ffmpeg_options.split(' ').collect::<Vec<&str>>();

let mut final_file_name = args
.output
.replace("{{ext}}", path.extension().unwrap().to_str().unwrap());
let mut final_file_name =
output.replace("{{ext}}", path.extension().unwrap().to_str().unwrap());
final_file_name = final_file_name
.replace("{{name}}", &path.file_stem().unwrap().to_str().unwrap());
final_file_name = final_file_name.replace(
Expand Down