fps selector on idle screen, lower gif fps minimum, 16:9 preset, aspect ratio lock on frame resize, gif budget mode (resolution/framerate/balanced priority), fix asset protocol scope
This commit is contained in:
+48
-12
@@ -8,7 +8,7 @@ use std::thread;
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
|
||||
use crate::clicks::Click;
|
||||
use crate::config::{Format, GifDither, Mp4Codec, WebmCodec};
|
||||
use crate::config::{Format, GifBudgetMode, GifDither, Mp4Codec, WebmCodec};
|
||||
use crate::CaptureRect;
|
||||
|
||||
// Used both for bitrate budget calculation and final mux. Must be consistent.
|
||||
@@ -39,6 +39,7 @@ pub struct TranscodeRequest {
|
||||
pub mp4_codec: Mp4Codec,
|
||||
pub scratch_dir: PathBuf,
|
||||
pub crop: Option<CaptureRect>,
|
||||
pub gif_budget_mode: GifBudgetMode,
|
||||
pub deleted_frame_indices: Vec<u32>,
|
||||
pub trim_start_secs: Option<f64>,
|
||||
pub trim_end_secs: Option<f64>,
|
||||
@@ -341,7 +342,7 @@ fn cleanup_pass_logs(stem: &Path) {
|
||||
|
||||
fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
let palette_path = req.scratch_dir.join("palette.png");
|
||||
let scale_width = estimate_gif_scale_width(req, req.clip_width, req.clip_height);
|
||||
let (scale_width, effective_fps) = gif_params(req, req.clip_width, req.clip_height);
|
||||
let crop_prefix = crop_filter(req.crop)
|
||||
.map(|c| format!("{c},"))
|
||||
.unwrap_or_default();
|
||||
@@ -356,7 +357,7 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
cprefix = crop_prefix,
|
||||
kprefix = click_prefix,
|
||||
sprefix = select_prefix,
|
||||
fps = req.fps,
|
||||
fps = effective_fps,
|
||||
w = scale_width,
|
||||
);
|
||||
|
||||
@@ -380,7 +381,7 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
cprefix = crop_prefix,
|
||||
kprefix = click_prefix,
|
||||
sprefix = select_prefix,
|
||||
fps = req.fps,
|
||||
fps = effective_fps,
|
||||
w = scale_width,
|
||||
d = dither,
|
||||
);
|
||||
@@ -404,7 +405,7 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
1,
|
||||
1,
|
||||
req.duration_secs,
|
||||
req.fps,
|
||||
effective_fps,
|
||||
req.progress_tx.as_ref(),
|
||||
)?;
|
||||
|
||||
@@ -412,14 +413,48 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn estimate_gif_scale_width(req: &TranscodeRequest, clip_width: u32, clip_height: u32) -> u32 {
|
||||
let bytes_per_pixel_per_frame = 0.5_f64;
|
||||
let frames = (req.fps as f64 * req.duration_secs).max(1.0);
|
||||
let budget = req.target_bytes as f64 / (frames * bytes_per_pixel_per_frame);
|
||||
pub fn gif_params(req: &TranscodeRequest, clip_width: u32, clip_height: u32) -> (u32, u32) {
|
||||
let aspect = if clip_height > 0 { clip_width as f64 / clip_height as f64 } else { 16.0 / 9.0 };
|
||||
let w = (budget * aspect).sqrt();
|
||||
let w = w.clamp(120.0, 1280.0).round() as u32;
|
||||
(w / 2) * 2
|
||||
let bytes_per_pixel_per_frame = 0.5_f64;
|
||||
let target_bytes = req.target_bytes as f64;
|
||||
let requested_fps = req.fps as f64;
|
||||
let duration = req.duration_secs;
|
||||
|
||||
match req.gif_budget_mode {
|
||||
GifBudgetMode::Resolution => {
|
||||
let max_w = (clip_width.min(1280) as f64).clamp(120.0, 1280.0);
|
||||
let w = (max_w / 2.0).round() as u32 * 2;
|
||||
let h = (w as f64 / aspect).round() as u32;
|
||||
let pixels_per_frame = w as f64 * h as f64;
|
||||
let bytes_per_frame = pixels_per_frame * bytes_per_pixel_per_frame;
|
||||
let max_frames = (target_bytes / bytes_per_frame).floor().max(1.0);
|
||||
let min_fps = (max_frames / duration).max(1.0).round() as u32;
|
||||
let fps = (req.fps).min(min_fps).max(1);
|
||||
(w, fps)
|
||||
}
|
||||
GifBudgetMode::Framerate => {
|
||||
let frames = (requested_fps * duration).max(1.0);
|
||||
let budget = target_bytes / (frames * bytes_per_pixel_per_frame);
|
||||
let w = (budget * aspect).sqrt();
|
||||
let w = (w.clamp(120.0, 1280.0).round() as u32 / 2) * 2;
|
||||
(w, req.fps)
|
||||
}
|
||||
GifBudgetMode::Balanced => {
|
||||
let frames_at_full = (requested_fps * duration).max(1.0);
|
||||
let budget_at_full = target_bytes / (frames_at_full * bytes_per_pixel_per_frame);
|
||||
let w_at_full = (budget_at_full * aspect).sqrt().clamp(120.0, 1280.0);
|
||||
|
||||
let scale = if w_at_full < clip_width as f64 {
|
||||
(w_at_full / clip_width as f64).sqrt()
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
|
||||
let w = ((clip_width as f64 * scale).round() as u32 / 2).max(60) * 2;
|
||||
let fps = ((requested_fps * scale).round() as u32).max(1).min(req.fps);
|
||||
(w, fps)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_preview(
|
||||
@@ -713,6 +748,7 @@ mod tests {
|
||||
mp4_codec: Mp4Codec::H264,
|
||||
scratch_dir: PathBuf::new(),
|
||||
crop: None,
|
||||
gif_budget_mode: GifBudgetMode::Balanced,
|
||||
deleted_frame_indices: vec![],
|
||||
trim_start_secs: None,
|
||||
trim_end_secs: None,
|
||||
|
||||
Reference in New Issue
Block a user