Skip to content

Commit

Permalink
Improve eta by providing more granular progress bar position updates (#…
Browse files Browse the repository at this point in the history
…175)

* Improve eta by providing more granular progress bar position updates

* refactor
  • Loading branch information
alexheretic authored Jan 12, 2024
1 parent ad43442 commit 6378a6d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 107 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
* Improve eta stability.

# v0.7.11
* Fix sample-encode caching to consider vmaf args.

Expand Down
117 changes: 23 additions & 94 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ pub use sample_encode::sample_encode;
pub use vmaf::vmaf;

const PROGRESS_CHARS: &str = "##-";

/// Helper trait for durations under 584942 years or so.
trait SmallDuration {
/// Returns the total number of whole microseconds.
fn as_micros_u64(&self) -> u64;
}

impl SmallDuration for std::time::Duration {
fn as_micros_u64(&self) -> u64 {
self.as_micros().try_into().unwrap_or(u64::MAX)
}
}
2 changes: 1 addition & 1 deletion src/command/crf_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
time::Duration,
};

const BAR_LEN: u64 = 1000;
const BAR_LEN: u64 = 1_000_000_000;

/// Interpolated binary search using sample-encode to find the best crf
/// value delivering min-vmaf & max-encoded-percent.
Expand Down
8 changes: 4 additions & 4 deletions src/command/encode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
command::{
args::{self, Encoder},
PROGRESS_CHARS,
SmallDuration, PROGRESS_CHARS,
},
console_ext::style,
ffmpeg,
Expand Down Expand Up @@ -78,8 +78,8 @@ pub async fn run(
let mut enc_args = args.to_encoder_args(crf, &probe)?;
enc_args.video_only = video_only;
let has_audio = probe.has_audio;
if let Ok(d) = probe.duration {
bar.set_length(d.as_secs().max(1));
if let Ok(d) = &probe.duration {
bar.set_length(d.as_micros_u64().max(1));
}

// only downmix if achannels > 3
Expand All @@ -99,7 +99,7 @@ pub async fn run(
bar.set_message(format!("{fps} fps, "));
}
if probe.duration.is_ok() {
bar.set_position(time.as_secs());
bar.set_position(time.as_micros_u64());
}
}
FfmpegOut::StreamSizes {
Expand Down
18 changes: 10 additions & 8 deletions src/command/sample_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod cache;
use crate::{
command::{
args::{self, PixelFormat},
PROGRESS_CHARS,
SmallDuration, PROGRESS_CHARS,
},
console_ext::style,
ffmpeg::{self, FfmpegEncodeArgs},
Expand Down Expand Up @@ -115,8 +115,8 @@ pub async fn run(
(samples, SAMPLE_SIZE, false)
}
};
let sample_duration_s = sample_duration.as_secs();
bar.set_length(sample_duration_s * samples * 2);
let sample_duration_us = sample_duration.as_micros_u64();
bar.set_length(sample_duration_us * samples * 2);

// Start creating copy samples async, this is IO bound & not cpu intensive
let (tx, mut sample_tasks) = tokio::sync::mpsc::unbounded_channel();
Expand Down Expand Up @@ -173,7 +173,7 @@ pub async fn run(
.await
{
(Some(result), _) => {
bar.set_position(sample_n * sample_duration_s * 2);
bar.set_position(sample_n * sample_duration_us * 2);
bar.println(
style!(
"- Sample {sample_n} ({:.0}%) vmaf {:.2} (cache)",
Expand All @@ -198,7 +198,9 @@ pub async fn run(
)?;
while let Some(progress) = output.next().await {
if let FfmpegOut::Progress { time, fps, .. } = progress? {
bar.set_position(time.as_secs() + sample_idx * sample_duration_s * 2);
bar.set_position(
time.as_micros_u64() + sample_idx * sample_duration_us * 2,
);
if fps > 0.0 {
bar.set_message(format!("enc {fps} fps,"));
}
Expand Down Expand Up @@ -228,10 +230,10 @@ pub async fn run(
}
VmafOut::Progress(FfmpegOut::Progress { time, fps, .. }) => {
bar.set_position(
sample_duration_s
sample_duration_us
// *24/fps adjusts for vmaf `-r 24`
+ (time.as_secs_f64() * (24.0 / input_fps)).round() as u64
+ sample_idx * sample_duration_s * 2,
+ (time.as_micros_u64() as f64 * (24.0 / input_fps)).round() as u64
+ sample_idx * sample_duration_us * 2,
);
if fps > 0.0 {
bar.set_message(format!("vmaf {fps} fps,"));
Expand Down

0 comments on commit 6378a6d

Please sign in to comment.