use std::io::Write;
use std::process::{Command, Stdio};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoCodec {
H264,
Hevc,
HevcVideoToolbox,
Vp9,
Gif,
}
impl VideoCodec {
fn is_crf_based(self) -> bool {
matches!(self, VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::Vp9)
}
}
#[derive(Clone, Debug)]
pub enum VideoQuality {
Default,
Bitrate(String),
Crf(u32),
}
#[derive(Clone, Debug)]
pub struct VideoOptions {
pub output: String,
pub fps: u32,
pub codec: VideoCodec,
pub quality: VideoQuality,
pub transparent: bool,
pub extra_args: Vec<String>,
pub gif_colors: u16,
}
impl VideoOptions {
pub fn new(output: impl Into<String>) -> Self {
Self {
output: output.into(),
fps: 30,
codec: VideoCodec::H264,
quality: VideoQuality::Default,
transparent: false,
extra_args: Vec::new(),
gif_colors: 256,
}
}
pub fn fps(mut self, fps: u32) -> Self {
self.fps = fps.max(1);
self
}
pub fn codec(mut self, codec: VideoCodec) -> Self {
self.codec = codec;
self
}
pub fn bitrate(mut self, bitrate: impl Into<String>) -> Self {
self.quality = VideoQuality::Bitrate(bitrate.into());
self
}
pub fn crf(mut self, crf: u32) -> Self {
self.quality = VideoQuality::Crf(crf);
self
}
pub fn transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
pub fn extra_args(mut self, args: Vec<String>) -> Self {
self.extra_args = args;
self
}
pub fn gif_colors(mut self, n: u16) -> Self {
self.gif_colors = n.clamp(2, 256);
self
}
}
#[derive(Debug)]
pub enum VideoError {
Spawn(std::io::Error),
Write(std::io::Error),
Io(std::io::Error),
FrameSize {
frame: usize,
expected: usize,
got: usize,
},
Ffmpeg(Option<i32>),
NoFrames,
}
impl std::fmt::Display for VideoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VideoError::Spawn(e) => {
write!(f, "failed to launch ffmpeg (installed and on PATH?): {e}")
}
VideoError::Write(e) => write!(f, "failed to write a frame to ffmpeg: {e}"),
VideoError::Io(e) => write!(f, "failed to write output: {e}"),
VideoError::FrameSize {
frame,
expected,
got,
} => write!(
f,
"frame {frame} returned {got} bytes, expected {expected} (width*height*4)"
),
VideoError::Ffmpeg(code) => write!(f, "ffmpeg exited with status {code:?}"),
VideoError::NoFrames => write!(f, "frame count was zero"),
}
}
}
impl std::error::Error for VideoError {}
pub fn export_video<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
mut frame: F,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
if frames == 0 {
return Err(VideoError::NoFrames);
}
let expected = (width as usize) * (height as usize) * 4;
#[cfg(feature = "native-codec")]
if options.codec == VideoCodec::Gif {
return export_gif_native(width, height, frames, options, expected, &mut frame);
}
let mut cmd = Command::new("ffmpeg");
cmd.arg("-y")
.args(["-f", "rawvideo"])
.args(["-pixel_format", "rgba"])
.args(["-video_size", &format!("{width}x{height}")])
.args(["-framerate", &options.fps.to_string()])
.args(["-i", "-"]);
append_codec_args(&mut cmd, options);
for a in &options.extra_args {
cmd.arg(a);
}
cmd.arg(&options.output);
cmd.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::inherit());
let mut child = cmd.spawn().map_err(VideoError::Spawn)?;
{
let mut stdin = child.stdin.take().expect("ffmpeg stdin");
for i in 0..frames {
let buf = frame(i);
if buf.len() != expected {
let _ = child.kill();
return Err(VideoError::FrameSize {
frame: i,
expected,
got: buf.len(),
});
}
stdin.write_all(&buf).map_err(VideoError::Write)?;
}
}
let status = child.wait().map_err(VideoError::Spawn)?;
if !status.success() {
return Err(VideoError::Ffmpeg(status.code()));
}
Ok(())
}
#[cfg(feature = "native-codec")]
fn export_gif_native<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
expected: usize,
frame: &mut F,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
use crate::codec::gif::{GifOptions, GifWriter, PaletteMode};
use std::fs::File;
let delay_den = options.fps.max(1) as u16;
let opts = GifOptions::default()
.colors(options.gif_colors)
.transparency(options.transparent)
.dither(true)
.diff_rects(true)
.palette_mode(PaletteMode::Local);
let file = File::create(&options.output).map_err(VideoError::Io)?;
let mut writer = GifWriter::new(file, width, height, 0, opts).map_err(VideoError::Io)?;
for i in 0..frames {
let buf = frame(i);
if buf.len() != expected {
return Err(VideoError::FrameSize {
frame: i,
expected,
got: buf.len(),
});
}
writer
.write_frame(&buf, 1, delay_den)
.map_err(VideoError::Io)?;
}
writer.finish().map_err(VideoError::Io)?;
Ok(())
}
fn append_codec_args(cmd: &mut Command, opts: &VideoOptions) {
match opts.codec {
VideoCodec::H264 => {
cmd.args(["-c:v", "libx264", "-pix_fmt", "yuv420p"]);
}
VideoCodec::Hevc => {
cmd.args(["-c:v", "libx265", "-pix_fmt", "yuv420p", "-tag:v", "hvc1"]);
}
VideoCodec::HevcVideoToolbox => {
cmd.args([
"-c:v",
"hevc_videotoolbox",
"-tag:v",
"hvc1",
"-pix_fmt",
"yuv420p",
]);
}
VideoCodec::Vp9 => {
let pix = if opts.transparent {
"yuva420p"
} else {
"yuv420p"
};
cmd.args(["-c:v", "libvpx-vp9", "-pix_fmt", pix]);
}
VideoCodec::Gif => {
cmd.args([
"-vf",
"split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse",
]);
}
}
match &opts.quality {
VideoQuality::Default => {}
VideoQuality::Bitrate(b) => {
cmd.args(["-b:v", b]);
}
VideoQuality::Crf(crf) => {
if opts.codec.is_crf_based() {
cmd.args(["-crf", &crf.to_string()]);
}
}
}
}