Skip to content

Instantly share code, notes, and snippets.

@theoparis
Created July 10, 2025 08:57
Show Gist options
  • Save theoparis/2ff96abd7436f8bd8b3a7bf3700f15fe to your computer and use it in GitHub Desktop.
Save theoparis/2ff96abd7436f8bd8b3a7bf3700f15fe to your computer and use it in GitHub Desktop.
Rust program that runs ffmpeg and generates a cursed video from the specified binary file
use std::fs::metadata;
use std::process::Command;
use std::f64;
fn main() {
let program_path = std::env::args().nth(1).unwrap_or_else(|| {
"/Applications/Ghostty.app/Contents/MacOS/ghostty".to_string()
});
let file_size = metadata(&program_path)
.expect("Failed to stat file")
.len();
const PIX_FMT: &str = "rgb555";
const BYTES_PER_PIXEL: u64 = 2;
const ASPECT_TARGET: f64 = 16.0 / 10.0;
const MAX_DIM: u32 = 512;
let mut best_w = 0;
let mut best_h = 0;
let mut best_err = f64::MAX;
for w in (8..=MAX_DIM).rev() {
for h in (8..=MAX_DIM).rev() {
let frame_size = w as u64 * h as u64 * BYTES_PER_PIXEL;
if frame_size == 0 || file_size % frame_size != 0 {
continue;
}
let aspect = w as f64 / h as f64;
let err = (aspect - ASPECT_TARGET).powi(2);
if err < best_err {
best_w = w;
best_h = h;
best_err = err;
}
}
}
if best_w == 0 {
eprintln!("No suitable resolution found for file size {file_size}");
std::process::exit(1);
}
println!(
"Best resolution: {}x{} (aspect ~ {:.3}, frames = {})",
best_w,
best_h,
best_w as f64 / best_h as f64,
file_size / (best_w as u64 * best_h as u64 * BYTES_PER_PIXEL)
);
let ffmpeg_status = Command::new("ffmpeg")
.args([
"-f", "u8",
"-ar", "44100",
"-ac", "2",
"-i", &program_path,
"-f", "rawvideo",
"-pixel_format", PIX_FMT,
"-video_size", &format!("{}x{}", best_w, best_h),
"-framerate", "25",
"-i", &program_path,
"-vf", "scale=1280:720:flags=neighbor",
"-c:v", "libsvtav1",
"-b:v", "50000k",
"-c:a", "libopus",
"-shortest",
"-y", "output.webm",
])
.status()
.expect("Failed to run ffmpeg");
if !ffmpeg_status.success() {
eprintln!("ffmpeg exited with error");
std::process::exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment