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

Re-use best sample when encoding images #100

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
20 changes: 19 additions & 1 deletion src/command/auto_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use clap::Parser;
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use std::{sync::Arc, time::Duration};
use std::{fs, sync::Arc, time::Duration};

/// Automatically determine the best crf to deliver the min-vmaf and use it to encode a video or image.
///
Expand Down Expand Up @@ -38,6 +38,9 @@ pub async fn auto_encode(Args { mut search, encode }: Args) -> anyhow::Result<()
let defaulting_output = encode.output.is_none();
let input_probe = Arc::new(ffprobe::probe(&search.args.input));

// Keep image samples so we can use the best sample as the final image
search.keep = input_probe.is_probably_an_image();

let output = encode.output.unwrap_or_else(|| {
default_output_name(
&search.args.input,
Expand Down Expand Up @@ -97,6 +100,21 @@ pub async fn auto_encode(Args { mut search, encode }: Args) -> anyhow::Result<()
style(best.enc.vmaf).green(),
style(format!("{:.0}%", best.enc.encode_percent)).green(),
));

// We don't need to do a final encode for images since we already did it when searching for the best CRF
if input_probe.is_probably_an_image() {
let output_path_with_best_crf = &output.with_extension(format!("crf{}.avif", best.crf()));

// Check if we actually have a real sample or if our best CRF was just read from the cache
if output_path_with_best_crf.exists() {
fs::rename(output_path_with_best_crf, output)?;
temporary::clean_all().await;
return Ok(());
}

// If the best CRF was read from the cache then we don't have an image to re-use, so we just continue as normal
}

temporary::clean_all().await;

let bar = ProgressBar::new(12).with_style(
Expand Down
7 changes: 6 additions & 1 deletion src/command/crf_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub struct Args {

#[arg(skip)]
pub quiet: bool,

/// Keep temporary files after exiting.
#[arg(long)]
pub keep: bool,
}

pub async fn crf_search(mut args: Args) -> anyhow::Result<()> {
Expand Down Expand Up @@ -123,6 +127,7 @@ pub async fn run(
quiet,
cache,
vmaf,
keep
}: &Args,
input_probe: Arc<Ffprobe>,
bar: ProgressBar,
Expand All @@ -142,7 +147,7 @@ pub async fn run(
args: args.clone(),
crf: 0.0,
sample: sample.clone(),
keep: false,
keep: *keep,
cache: *cache,
stdout_format: sample_encode::StdoutFormat::Json,
vmaf: vmaf.clone(),
Expand Down