Skip to content

Commit

Permalink
Add defaults for mpeg2video encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Dec 22, 2024
1 parent 8e0d142 commit c50584b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased (0.8.1)
* Support negative `--preset` args.
* Add `--vmaf-fps`: Frame rate override used to analyse both reference & distorted videos.
* mpeg2video: map `--crf` to ffmpeg `-q` and set default crf range to 2-30.

# v0.8.0
* crf-search: Tweak 2nd iteration logic that slices the crf range at the 25% or 75% crf point.
Expand Down
8 changes: 8 additions & 0 deletions src/command/args/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,19 @@ impl Encoder {
}
}

pub fn default_min_crf(&self) -> f32 {
match self.as_str() {
"mpeg2video" => 2.0,
_ => 10.0,
}
}

pub fn default_max_crf(&self) -> f32 {
match self.as_str() {
"libx264" | "libx265" => 46.0,
"librav1e" => 255.0,
"av1_vaapi" => 255.0,
"mpeg2video" => 30.0,
// Works well for svt-av1
_ => 55.0,
}
Expand Down
13 changes: 8 additions & 5 deletions src/command/crf_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::{
};

const BAR_LEN: u64 = 1024 * 1024 * 1024;
const DEFAULT_MIN_CRF: f32 = 10.0;

/// Interpolated binary search using sample-encode to find the best crf
/// value delivering min-vmaf & max-encoded-percent.
Expand Down Expand Up @@ -54,12 +53,14 @@ pub struct Args {
pub max_encoded_percent: f32,

/// Minimum (highest quality) crf value to try.
#[arg(long, default_value_t = DEFAULT_MIN_CRF)]
pub min_crf: f32,
///
/// [default: 10, 2 for mpeg2video]
#[arg(long)]
pub min_crf: Option<f32>,

/// Maximum (lowest quality) crf value to try.
///
/// [default: 55, 46 for x264,x265, 255 for rav1e,av1_vaapi]
/// [default: 55, 46 for x264,x265, 255 for rav1e,av1_vaapi, 30 for mpeg2video]
#[arg(long)]
pub max_crf: Option<f32>,

Expand Down Expand Up @@ -198,6 +199,8 @@ pub fn run(
async_stream::try_stream! {
let default_max_crf = args.encoder.default_max_crf();
let max_crf = max_crf.unwrap_or(default_max_crf);
let default_min_crf = args.encoder.default_min_crf();
let min_crf = min_crf.unwrap_or(default_min_crf);
Error::ensure_other(min_crf < max_crf, "Invalid --min-crf & --max-crf")?;

// Whether to make the 2nd iteration on the ~20%/~80% crf point instead of the min/max to
Expand All @@ -208,7 +211,7 @@ pub fn run(
// the min/max crf on the 3rd iter.
//
// If a custom crf range is being used under half the default, this 2nd cut is not needed.
let cut_on_iter2 = (max_crf - min_crf) > (default_max_crf - DEFAULT_MIN_CRF) * 0.5;
let cut_on_iter2 = (max_crf - min_crf) > (default_max_crf - default_min_crf) * 0.5;

let crf_increment = crf_increment
.unwrap_or_else(|| args.encoder.default_crf_increment())
Expand Down
3 changes: 2 additions & 1 deletion src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ impl VCodecSpecific for Arc<str> {
}

fn crf_arg(&self) -> &str {
// // use crf-like args to support encoders that don't have crf
// use crf-like args to support encoders that don't have crf
match &**self {
// https://ffmpeg.org//ffmpeg-codecs.html#librav1e
"librav1e" => "-qp",
"mpeg2video" => "-q",
// https://ffmpeg.org//ffmpeg-codecs.html#VAAPI-encoders
e if e.ends_with("_vaapi") => "-q",
e if e.ends_with("_nvenc") => "-cq",
Expand Down

0 comments on commit c50584b

Please sign in to comment.