type ProgressCallback = Box<dyn FnMut(&VideoExportProgress) + Send>;
type EventCallback = Box<dyn FnMut(&VideoExportEvent) + Send>;
use std::io::Write;
use std::process::{Command, Stdio};
use crate::captions::{CaptionFont, CaptionFormat, CaptionPainter, CaptionStyle, CaptionTrack};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CaptionMode {
#[default]
Burn,
Sidecar,
Embed,
BurnAndSidecar,
}
impl CaptionMode {
pub fn burns(self) -> bool {
matches!(self, CaptionMode::Burn | CaptionMode::BurnAndSidecar)
}
pub fn writes_sidecar(self) -> bool {
matches!(self, CaptionMode::Sidecar | CaptionMode::BurnAndSidecar)
}
pub fn embeds(self) -> bool {
matches!(self, CaptionMode::Embed)
}
}
#[derive(Clone, Debug)]
pub struct CaptionExport {
pub track: CaptionTrack,
pub mode: CaptionMode,
pub style: Option<CaptionStyle>,
pub font: Option<Vec<u8>>,
pub format: CaptionFormat,
pub sidecar_path: Option<String>,
}
impl CaptionExport {
pub fn new(track: CaptionTrack) -> Self {
Self {
track,
mode: CaptionMode::default(),
style: None,
font: None,
format: CaptionFormat::Srt,
sidecar_path: None,
}
}
pub fn sidecar_for(&self, output: &str) -> String {
self.sidecar_path
.clone()
.unwrap_or_else(|| replace_extension(output, self.format.extension()))
}
}
fn replace_extension(path: &str, extension: &str) -> String {
let start = path.rfind(['/', '\\']).map(|i| i + 1).unwrap_or(0);
match path[start..].rfind('.') {
Some(dot) if dot > 0 => format!("{}.{extension}", &path[..start + dot]),
_ => format!("{path}.{extension}"),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoCodec {
H264,
Hevc,
HevcVideoToolbox,
Vp9,
Gif,
Apng,
}
impl VideoCodec {
fn is_crf_based(self) -> bool {
matches!(self, VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::Vp9)
}
pub fn label(self) -> &'static str {
match self {
VideoCodec::H264 => "h264",
VideoCodec::Hevc => "hevc",
VideoCodec::HevcVideoToolbox => "hevc-vt",
VideoCodec::Vp9 => "webm",
VideoCodec::Gif => "gif",
VideoCodec::Apng => "apng",
}
}
pub fn subtitle_encoder(self) -> Option<&'static str> {
match self {
VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::HevcVideoToolbox => Some("mov_text"),
VideoCodec::Vp9 => Some("webvtt"),
VideoCodec::Gif | VideoCodec::Apng => None,
}
}
}
#[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,
pub captions: Option<CaptionExport>,
}
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,
captions: None,
}
}
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
}
pub fn captions(mut self, track: CaptionTrack) -> Self {
self.captions = Some(CaptionExport::new(track));
self
}
pub fn caption_mode(mut self, mode: CaptionMode) -> Self {
if let Some(c) = self.captions.as_mut() {
c.mode = mode;
}
self
}
pub fn caption_style(mut self, style: CaptionStyle) -> Self {
if let Some(c) = self.captions.as_mut() {
c.style = Some(style);
}
self
}
pub fn caption_font(mut self, ttf: impl Into<Vec<u8>>) -> Self {
if let Some(c) = self.captions.as_mut() {
c.font = Some(ttf.into());
}
self
}
pub fn caption_format(mut self, format: CaptionFormat) -> Self {
if let Some(c) = self.captions.as_mut() {
c.format = format;
}
self
}
pub fn caption_sidecar_path(mut self, path: impl Into<String>) -> Self {
if let Some(c) = self.captions.as_mut() {
c.sidecar_path = Some(path.into());
}
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoExportPhase {
Capture,
Encode,
Done,
}
impl VideoExportPhase {
pub fn as_str(self) -> &'static str {
match self {
Self::Capture => "capture",
Self::Encode => "encode",
Self::Done => "done",
}
}
}
#[derive(Clone, Debug)]
pub struct VideoExportProgress {
pub phase: VideoExportPhase,
pub frame: u32,
pub frames: u32,
pub ratio: f32,
pub codec: VideoCodec,
pub message: String,
}
#[derive(Clone, Debug)]
pub enum VideoExportEvent {
Start {
phase: VideoExportPhase,
frames: u32,
codec: VideoCodec,
},
Progress(VideoExportProgress),
Capture(VideoExportProgress),
Encode(VideoExportProgress),
Complete {
output: String,
frames: u32,
codec: VideoCodec,
},
Error {
message: String,
},
}
pub fn format_video_progress(info: &VideoExportProgress) -> String {
let pct = (info.ratio.clamp(0.0, 1.0) * 100.0).round() as i32;
match info.phase {
VideoExportPhase::Capture => {
format!("Rendering {}/{} ({pct}%)", info.frame, info.frames)
}
VideoExportPhase::Encode => {
format!(
"Encoding {}… ({pct}%)",
info.codec.label().to_ascii_uppercase()
)
}
VideoExportPhase::Done => format!("Done ({} frames)", info.frames),
}
}
fn make_progress(
phase: VideoExportPhase,
frame: u32,
frames: u32,
ratio: f32,
codec: VideoCodec,
) -> VideoExportProgress {
let mut info = VideoExportProgress {
phase,
frame,
frames,
ratio: ratio.clamp(0.0, 1.0),
codec,
message: String::new(),
};
info.message = format_video_progress(&info);
info
}
#[derive(Default)]
struct VideoExportTracer {
on_progress: Option<ProgressCallback>,
on_event: Option<EventCallback>,
}
impl VideoExportTracer {
fn emit_event(&mut self, event: VideoExportEvent) {
if let VideoExportEvent::Progress(ref p)
| VideoExportEvent::Capture(ref p)
| VideoExportEvent::Encode(ref p) = event
{
if let Some(cb) = self.on_progress.as_mut() {
cb(p);
}
}
if let Some(cb) = self.on_event.as_mut() {
cb(&event);
}
}
fn emit_progress(&mut self, info: VideoExportProgress) {
match info.phase {
VideoExportPhase::Capture => {
self.emit_event(VideoExportEvent::Progress(info.clone()));
self.emit_event(VideoExportEvent::Capture(info));
}
VideoExportPhase::Encode => {
self.emit_event(VideoExportEvent::Progress(info.clone()));
self.emit_event(VideoExportEvent::Encode(info));
}
VideoExportPhase::Done => {
self.emit_event(VideoExportEvent::Progress(info));
}
}
}
}
pub struct VideoExporter {
options: VideoOptions,
width: u32,
height: u32,
frames: usize,
tracer: VideoExportTracer,
}
impl VideoExporter {
pub fn new(output: impl Into<String>) -> Self {
Self {
options: VideoOptions::new(output),
width: 0,
height: 0,
frames: 0,
tracer: VideoExportTracer::default(),
}
}
pub fn size(mut self, width: u32, height: u32) -> Self {
self.width = width;
self.height = height;
self
}
pub fn frames(mut self, frames: usize) -> Self {
self.frames = frames;
self
}
pub fn fps(mut self, fps: u32) -> Self {
self.options = self.options.fps(fps);
self
}
pub fn codec(mut self, codec: VideoCodec) -> Self {
self.options = self.options.codec(codec);
self
}
pub fn bitrate(mut self, bitrate: impl Into<String>) -> Self {
self.options = self.options.bitrate(bitrate);
self
}
pub fn crf(mut self, crf: u32) -> Self {
self.options = self.options.crf(crf);
self
}
pub fn transparent(mut self, transparent: bool) -> Self {
self.options = self.options.transparent(transparent);
self
}
pub fn gif_colors(mut self, n: u16) -> Self {
self.options = self.options.gif_colors(n);
self
}
pub fn extra_args(mut self, args: Vec<String>) -> Self {
self.options = self.options.extra_args(args);
self
}
pub fn captions(mut self, track: CaptionTrack) -> Self {
self.options = self.options.captions(track);
self
}
pub fn caption_mode(mut self, mode: CaptionMode) -> Self {
self.options = self.options.caption_mode(mode);
self
}
pub fn caption_style(mut self, style: CaptionStyle) -> Self {
self.options = self.options.caption_style(style);
self
}
pub fn caption_font(mut self, ttf: impl Into<Vec<u8>>) -> Self {
self.options = self.options.caption_font(ttf);
self
}
pub fn caption_format(mut self, format: CaptionFormat) -> Self {
self.options = self.options.caption_format(format);
self
}
pub fn caption_sidecar_path(mut self, path: impl Into<String>) -> Self {
self.options = self.options.caption_sidecar_path(path);
self
}
pub fn options(mut self, options: VideoOptions) -> Self {
self.options = options;
self
}
pub fn on_progress(mut self, cb: impl FnMut(&VideoExportProgress) + Send + 'static) -> Self {
self.tracer.on_progress = Some(Box::new(cb));
self
}
pub fn on_event(mut self, cb: impl FnMut(&VideoExportEvent) + Send + 'static) -> Self {
self.tracer.on_event = Some(Box::new(cb));
self
}
pub fn export<F>(mut self, frame: F) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
if self.width == 0 || self.height == 0 {
return Err(VideoError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"VideoExporter: size(width, height) required",
)));
}
export_video_traced(
self.width,
self.height,
self.frames,
&self.options,
frame,
&mut self.tracer,
)
}
}
#[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,
Captions(String),
}
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"),
VideoError::Captions(m) => write!(f, "captions could not be applied: {m}"),
}
}
}
impl std::error::Error for VideoError {}
pub fn export_video<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
frame: F,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
let mut tracer = VideoExportTracer::default();
export_video_traced(width, height, frames, options, frame, &mut tracer)
}
pub fn export_video_with_progress<F, P>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
frame: F,
on_progress: P,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
P: FnMut(&VideoExportProgress) + Send + 'static,
{
VideoExporter::new(options.output.clone())
.size(width, height)
.frames(frames)
.options(options.clone())
.on_progress(on_progress)
.export(frame)
}
fn export_video_traced<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
frame: F,
tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
if frames == 0 {
let err = VideoError::NoFrames;
tracer.emit_event(VideoExportEvent::Error {
message: err.to_string(),
});
return Err(err);
}
let expected = (width as usize) * (height as usize) * 4;
let frames_u = frames as u32;
let codec = options.codec;
let mut burner = match CaptionBurner::prepare(options, width, height) {
Ok(b) => b,
Err(e) => {
tracer.emit_event(VideoExportEvent::Error {
message: e.to_string(),
});
return Err(e);
}
};
let mut source = frame;
let mut frame = |i: usize| {
let mut buf = source(i);
if let Some(b) = burner.as_mut() {
b.apply(&mut buf, i);
}
buf
};
let result = (|| {
let embedded = EmbeddedSubtitles::prepare(options)?;
#[cfg(feature = "native-codec")]
if matches!(options.codec, VideoCodec::Gif | VideoCodec::Apng) {
export_native_animation(width, height, frames, options, &mut frame, tracer)?;
return write_sidecar(options);
}
#[cfg(feature = "native-codec")]
if matches!(options.codec, VideoCodec::H264) {
export_native_h264(width, height, frames, options, &mut frame, tracer)?;
return write_sidecar(options);
}
tracer.emit_event(VideoExportEvent::Start {
phase: VideoExportPhase::Capture,
frames: frames_u,
codec,
});
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", "-"]);
if let Some(sub) = &embedded {
sub.append_input(&mut cmd);
}
append_codec_args(&mut cmd, options);
if let Some(sub) = &embedded {
sub.append_output_args(&mut cmd);
}
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 done = (i + 1) as u32;
tracer.emit_progress(make_progress(
VideoExportPhase::Capture,
done,
frames_u,
done as f32 / (frames_u as f32 + 1.0),
codec,
));
}
}
let status = child.wait().map_err(VideoError::Spawn)?;
if !status.success() {
return Err(VideoError::Ffmpeg(status.code()));
}
write_sidecar(options)?;
tracer.emit_progress(make_progress(
VideoExportPhase::Done,
frames_u,
frames_u,
1.0,
codec,
));
Ok(())
})();
match &result {
Ok(()) => tracer.emit_event(VideoExportEvent::Complete {
output: options.output.clone(),
frames: frames_u,
codec,
}),
Err(e) => tracer.emit_event(VideoExportEvent::Error {
message: e.to_string(),
}),
}
result
}
#[cfg(feature = "native-codec")]
fn export_native_h264<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
frame: &mut F,
tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
use crate::codec::h264::encode_mp4_with_captions;
use crate::codec::hevc::Yuv420Frame;
if !width.is_multiple_of(2) || !height.is_multiple_of(2) {
return Err(VideoError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"native H.264 export requires even width and height (4:2:0)",
)));
}
if options.transparent {
return Err(VideoError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"native H.264 export does not support transparency yet",
)));
}
let expected = (width as usize) * (height as usize) * 4;
let frames_u = frames as u32;
let codec = options.codec;
let captions = options.captions.as_ref().and_then(|c| {
if c.mode.embeds() && !c.track.is_empty() {
Some(&c.track)
} else {
None
}
});
tracer.emit_event(VideoExportEvent::Start {
phase: VideoExportPhase::Capture,
frames: frames_u,
codec,
});
let mut yuv_frames = Vec::with_capacity(frames);
for i in 0..frames {
let buf = frame(i);
if buf.len() != expected {
return Err(VideoError::FrameSize {
frame: i,
expected,
got: buf.len(),
});
}
yuv_frames.push(Yuv420Frame::from_rgba(width, height, &buf));
let done = (i + 1) as u32;
tracer.emit_progress(make_progress(
VideoExportPhase::Capture,
done,
frames_u,
done as f32 / (frames_u as f32 + 1.0),
codec,
));
}
tracer.emit_event(VideoExportEvent::Start {
phase: VideoExportPhase::Encode,
frames: frames_u,
codec,
});
tracer.emit_progress(make_progress(
VideoExportPhase::Encode,
0,
frames_u,
frames_u as f32 / (frames_u as f32 + 1.0),
codec,
));
let bytes = encode_mp4_with_captions(width, height, options.fps, &yuv_frames, captions);
std::fs::write(&options.output, bytes).map_err(VideoError::Io)?;
tracer.emit_progress(make_progress(
VideoExportPhase::Done,
frames_u,
frames_u,
1.0,
codec,
));
Ok(())
}
#[cfg(feature = "native-codec")]
fn export_native_animation<F>(
width: u32,
height: u32,
frames: usize,
options: &VideoOptions,
frame: &mut F,
tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
F: FnMut(usize) -> Vec<u8>,
{
use crate::codec::animation::{
encode_animation_rgba_with_progress, AnimationEncodeError, AnimationEncodeOptions,
AnimationExportPhase, BrowserCodec,
};
let codec = match options.codec {
VideoCodec::Gif => BrowserCodec::Gif,
VideoCodec::Apng => BrowserCodec::Apng,
_ => unreachable!("native animation export only handles GIF and APNG"),
};
let opts = AnimationEncodeOptions {
width,
height,
fps: options.fps,
codec,
transparent: options.transparent,
gif_colors: options.gif_colors,
};
let expected = (width as usize) * (height as usize) * 4;
let frames_u = frames as u32;
let vcodec = options.codec;
tracer.emit_event(VideoExportEvent::Start {
phase: VideoExportPhase::Capture,
frames: frames_u,
codec: vcodec,
});
let mut captured = Vec::with_capacity(frames);
for i in 0..frames {
let buf = frame(i);
if buf.len() != expected {
return Err(VideoError::FrameSize {
frame: i,
expected,
got: buf.len(),
});
}
captured.push(buf);
let done = (i + 1) as u32;
tracer.emit_progress(make_progress(
VideoExportPhase::Capture,
done,
frames_u,
done as f32 / (frames_u as f32 + 1.0),
vcodec,
));
}
tracer.emit_event(VideoExportEvent::Start {
phase: VideoExportPhase::Encode,
frames: frames_u,
codec: vcodec,
});
let bytes = encode_animation_rgba_with_progress(&opts, captured, |p| {
let phase = match p.phase {
AnimationExportPhase::Encode => VideoExportPhase::Encode,
AnimationExportPhase::Done => VideoExportPhase::Done,
AnimationExportPhase::Capture => VideoExportPhase::Capture,
};
tracer.emit_progress(make_progress(phase, p.frame, p.frames, p.ratio, vcodec));
})
.map_err(|error| match error {
AnimationEncodeError::Empty => VideoError::NoFrames,
AnimationEncodeError::FrameSize {
frame,
expected,
got,
} => VideoError::FrameSize {
frame,
expected,
got,
},
AnimationEncodeError::Dimension => VideoError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"invalid native animation dimensions or options",
)),
AnimationEncodeError::Io(message) => VideoError::Io(std::io::Error::other(message)),
})?;
std::fs::write(&options.output, bytes).map_err(VideoError::Io)?;
tracer.emit_progress(make_progress(
VideoExportPhase::Done,
frames_u,
frames_u,
1.0,
vcodec,
));
Ok(())
}
struct CaptionBurner<'a> {
painter: CaptionPainter,
track: &'a CaptionTrack,
fps: u32,
width: u32,
height: u32,
}
impl<'a> CaptionBurner<'a> {
fn prepare(
options: &'a VideoOptions,
width: u32,
height: u32,
) -> Result<Option<Self>, VideoError> {
let Some(captions) = options.captions.as_ref() else {
return Ok(None);
};
if captions.mode.embeds() && options.codec.subtitle_encoder().is_none() {
return Err(VideoError::Captions(format!(
"{} has no subtitle track — use CaptionMode::Burn or ::Sidecar",
options.codec.label().to_ascii_uppercase()
)));
}
if !captions.mode.burns() || captions.track.is_empty() {
return Ok(None);
}
let font = match &captions.font {
Some(bytes) => CaptionFont::from_ttf_bytes(bytes).map_err(|e| {
VideoError::Captions(format!("caption font could not be parsed: {e:?}"))
})?,
None => CaptionFont::builtin(),
};
let style = captions
.style
.clone()
.unwrap_or_else(|| CaptionStyle::default().for_height(height));
Ok(Some(Self {
painter: CaptionPainter::with_font(font).style(style),
track: &captions.track,
fps: options.fps.max(1),
width,
height,
}))
}
fn apply(&mut self, frame: &mut [u8], index: usize) {
let time = index as f64 / self.fps as f64;
self.painter
.burn_in(frame, self.width, self.height, self.track, time);
}
}
fn write_sidecar(options: &VideoOptions) -> Result<(), VideoError> {
let Some(captions) = options.captions.as_ref() else {
return Ok(());
};
if !captions.mode.writes_sidecar() || captions.track.is_empty() {
return Ok(());
}
let path = captions.sidecar_for(&options.output);
std::fs::write(path, captions.track.to_format(captions.format)).map_err(VideoError::Io)
}
struct EmbeddedSubtitles {
path: std::path::PathBuf,
encoder: &'static str,
language: String,
label: String,
}
impl EmbeddedSubtitles {
fn prepare(options: &VideoOptions) -> Result<Option<Self>, VideoError> {
let Some(captions) = options.captions.as_ref() else {
return Ok(None);
};
if !captions.mode.embeds() || captions.track.is_empty() {
return Ok(None);
}
let encoder = options.codec.subtitle_encoder().ok_or_else(|| {
VideoError::Captions(format!(
"{} has no subtitle track — use CaptionMode::Burn or ::Sidecar",
options.codec.label().to_ascii_uppercase()
))
})?;
let format = match encoder {
"webvtt" => CaptionFormat::Vtt,
_ => CaptionFormat::Srt,
};
let path = std::env::temp_dir().join(format!(
"threers-captions-{}.{}",
std::process::id(),
format.extension()
));
std::fs::write(&path, captions.track.to_format(format)).map_err(VideoError::Io)?;
Ok(Some(Self {
path,
encoder,
language: String::from_utf8_lossy(&crate::captions::iso639_2(&captions.track.language))
.into_owned(),
label: captions.track.label.clone(),
}))
}
fn append_input(&self, cmd: &mut Command) {
cmd.arg("-i").arg(&self.path);
}
fn append_output_args(&self, cmd: &mut Command) {
cmd.args(["-map", "0:v:0", "-map", "1:s:0"]);
cmd.args(["-c:s", self.encoder]);
cmd.args(["-metadata:s:s:0", &format!("language={}", self.language)]);
if !self.label.is_empty() {
cmd.args(["-metadata:s:s:0", &format!("title={}", self.label)]);
}
}
}
impl Drop for EmbeddedSubtitles {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
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",
]);
}
VideoCodec::Apng => {
cmd.args(["-plays", "0", "-f", "apng"]);
}
}
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()]);
}
}
}
}
#[cfg(test)]
mod caption_tests {
use super::*;
#[test]
fn sidecar_paths_swap_the_extension() {
assert_eq!(replace_extension("out.mp4", "srt"), "out.srt");
assert_eq!(replace_extension("a/b/clip.webm", "vtt"), "a/b/clip.vtt");
assert_eq!(replace_extension("out/movie", "srt"), "out/movie.srt");
assert_eq!(replace_extension("v1.2/clip", "srt"), "v1.2/clip.srt");
assert_eq!(replace_extension(".hidden", "srt"), ".hidden.srt");
assert_eq!(
replace_extension("C:\\vids\\a.mov", "vtt"),
"C:\\vids\\a.vtt"
);
}
#[test]
fn sidecar_for_honors_format_and_explicit_path() {
let mut export = CaptionExport::new(CaptionTrack::new());
assert_eq!(export.sidecar_for("out.mp4"), "out.srt");
export.format = CaptionFormat::Vtt;
assert_eq!(export.sidecar_for("out.mp4"), "out.vtt");
export.sidecar_path = Some("elsewhere/subs.vtt".into());
assert_eq!(export.sidecar_for("out.mp4"), "elsewhere/subs.vtt");
}
#[test]
fn caption_modes_describe_what_they_do() {
assert!(CaptionMode::Burn.burns());
assert!(!CaptionMode::Burn.writes_sidecar());
assert!(!CaptionMode::Burn.embeds());
assert!(CaptionMode::Sidecar.writes_sidecar());
assert!(!CaptionMode::Sidecar.burns());
assert!(CaptionMode::Embed.embeds());
assert!(CaptionMode::BurnAndSidecar.burns());
assert!(CaptionMode::BurnAndSidecar.writes_sidecar());
assert_eq!(CaptionMode::default(), CaptionMode::Burn);
}
#[test]
fn caption_builders_are_no_ops_without_a_track() {
let opts = VideoOptions::new("out.mp4")
.caption_mode(CaptionMode::Embed)
.caption_format(CaptionFormat::Vtt);
assert!(opts.captions.is_none());
}
#[test]
fn unset_caption_style_scales_itself_to_the_export_height() {
let opts = VideoOptions::new("out.mp4").captions(CaptionTrack::new().cue(0.0, 1.0, "x"));
let burner = CaptionBurner::prepare(&opts, 1280, 540).unwrap().unwrap();
assert!((burner.painter.style.font_size - 17.0).abs() < 1e-4);
let opts = opts.caption_style(CaptionStyle::default().font_size(40.0));
let burner = CaptionBurner::prepare(&opts, 1280, 540).unwrap().unwrap();
assert_eq!(burner.painter.style.font_size, 40.0);
}
}
#[cfg(all(test, feature = "native-codec"))]
mod tests {
use super::*;
fn solid(w: u32, h: u32, r: u8, g: u8, b: u8) -> Vec<u8> {
let mut out = vec![0u8; (w * h * 4) as usize];
for px in out.chunks_exact_mut(4) {
px.copy_from_slice(&[r, g, b, 255]);
}
out
}
#[test]
fn video_exporter_emits_progress_events() {
let dir = std::env::temp_dir().join("threers-video-events");
let _ = std::fs::create_dir_all(&dir);
let out = dir.join("cube.gif");
let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
let ev2 = events.clone();
let prog = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
let p2 = prog.clone();
VideoExporter::new(out.to_string_lossy().into_owned())
.size(8, 8)
.frames(4)
.fps(10)
.codec(VideoCodec::Gif)
.gif_colors(32)
.on_progress(move |p| p2.lock().unwrap().push(p.message.clone()))
.on_event(move |e| {
let label = match e {
VideoExportEvent::Start { phase, .. } => format!("start:{}", phase.as_str()),
VideoExportEvent::Progress(p) => format!("progress:{}", p.phase.as_str()),
VideoExportEvent::Capture(_) => "capture".into(),
VideoExportEvent::Encode(_) => "encode".into(),
VideoExportEvent::Complete { .. } => "complete".into(),
VideoExportEvent::Error { message } => format!("error:{message}"),
};
ev2.lock().unwrap().push(label);
})
.export(|i| {
let c = ((i * 40) % 255) as u8;
solid(8, 8, c, 80, 200)
})
.expect("export");
let events = events.lock().unwrap().clone();
assert!(events.iter().any(|e| e.starts_with("start:")), "{events:?}");
assert!(events.iter().any(|e| e == "capture"), "{events:?}");
assert!(events.iter().any(|e| e == "encode"), "{events:?}");
assert!(events.iter().any(|e| e == "complete"), "{events:?}");
let prog = prog.lock().unwrap().clone();
assert!(!prog.is_empty(), "expected progress messages");
assert!(out.is_file());
let bytes = std::fs::read(&out).unwrap();
assert_eq!(&bytes[..3], b"GIF");
}
}