Skip to content

Instantly share code, notes, and snippets.

@kornysietsma
Last active September 5, 2022 12:05
Show Gist options
  • Save kornysietsma/8e31431f63ed2393d5d08233a654db85 to your computer and use it in GitHub Desktop.
Save kornysietsma/8e31431f63ed2393d5d08233a654db85 to your computer and use it in GitHub Desktop.
#![forbid(unsafe_code)]
#![warn(clippy::all)]
#![warn(rust_2018_idioms)]
pub mod mymodule;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use anyhow::anyhow;
use anyhow::Result;
use env_logger::Env;
use clap::Parser;
use log::debug;
use log::error;
use log::info;
use log::warn;
fn downscale(input: OsString, output: OsString) -> Result<()> {
info!("downscaling {:?} to {:?}", input, output);
let mut cmd = Command::new("ffmpeg");
cmd.arg("-i")
.arg(input)
.args([
"-c:v",
"libx265",
"-crf",
"28",
"-preset",
"fast",
"-c:a",
"copy",
"-vf",
"scale=-2:'min(720,ih)'",
"-loglevel",
"warning",
"-nostats",
"-hide_banner",
"-x265-params",
"log-level=error",
])
.arg(output);
let status = cmd.status().expect("ffmpeg failed to run!");
match status.code() {
Some(0) => {
info!("Succeeded");
Ok(())
}
Some(code) => {
error!("Exited with status code: {}", code);
Err(anyhow!("Exited with status code: {}", code))
}
None => {
error!("Process terminated by signal");
Err(anyhow!("Terminated."))
}
}
}
fn downscale_recursive(root_source: &Path, root_dest: &Path, suffix: Vec<OsString>) -> Result<()> {
let mut source = PathBuf::from(root_source);
let mut dest = PathBuf::from(root_dest);
for dir in &suffix {
source.push(&dir);
dest.push(&dir);
}
assert!(&source.is_dir(), "Source does not exist, somehow");
for entry in fs::read_dir(&source)? {
let entry = entry?;
let file_type = entry.file_type()?;
if file_type.is_dir() {
let mut new_suffix: Vec<OsString> = suffix.clone();
new_suffix.push(entry.file_name());
downscale_recursive(root_source, root_dest, new_suffix)?;
} else if file_type.is_file() {
let source_file = entry.path();
if let Some(ext) = source_file.extension() {
if ext == "mp4" || ext == "mkv" {
let mut dest_file = dest.clone();
if !dest.is_dir() {
fs::create_dir_all(&dest)?
}
dest_file.push(Path::new(&entry.file_name()));
if dest_file.exists() {
debug!("not overwriting {:?}", &dest_file);
} else {
downscale(source_file.into_os_string(), dest_file.into_os_string())?;
}
} else {
debug!("ignoring file {:?}", &source_file);
}
} else {
debug!("ignoring file {:?}", &source_file);
}
} else {
debug!("ignoring file type {:?}", file_type);
}
}
Ok(())
}
fn run(source: &Path, dest: &Path) -> Result<()> {
downscale_recursive(source, dest, Vec::new())
}
fn validate_path_exists(path: &str) -> Result<(), String> {
if !Path::new(path).is_dir() {
Err(format!("Path {} does not exist", path))
} else {
Ok(())
}
}
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct Opts {
#[clap(value_parser, short, long, validator=validate_path_exists)]
source: PathBuf,
#[clap(value_parser, short, long)]
destination: PathBuf,
}
fn main() -> Result<()> {
// default log level to info
// override with RUST_LOG env var
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let opts = Opts::try_parse()?;
run(&opts.source, &opts.destination)
}
@kornysietsma
Copy link
Author

Note this doesn't check for lots of error conditions like source and dest clashing, or dest unwriteable. ffmpeg should crash but who knows?

@kornysietsma
Copy link
Author

Also - this downscales videos to 720p, with a fairly agressive algorithm (libx265) - it's mostly for putting kid videos on a tablet, where I don't care about pixels and do care about space. Don't use it for movies unless you are OK with 720p.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment