use std::fs;
use std::path::Path;
use std::process::Command;
use std::time::Duration;
use avt::Vt;
use crate::terminal::attributed::palette_color;
use crate::terminal::attributed::rgb_color;
use crate::terminal::attributed::screen_svg;
use crate::terminal::attributed::AttributedCell;
use crate::terminal::attributed::AttributedScreen;
use crate::terminal::attributed::SvgMetrics;
use crate::terminal::attributed::DEFAULT_CELL_H;
use crate::terminal::attributed::DEFAULT_CELL_W;
use crate::terminal::attributed::DEFAULT_COLUMNS;
use crate::terminal::attributed::DEFAULT_FONT_PX;
use crate::terminal::attributed::DEFAULT_PADDING;
use crate::terminal::attributed::DEFAULT_ROWS;
use crate::terminal::proc::combined_output;
use crate::terminal::proc::run_with_timeout;
const RSVG_CONVERT: &str = "/usr/bin/rsvg-convert";
const FFMPEG: &str = "/usr/local/bin/ffmpeg";
const RSVG_TIMEOUT: Duration = Duration::from_secs(30);
const FFMPEG_TIMEOUT: Duration = Duration::from_secs(120);
pub type ToolRunner = Box<dyn Fn(&str, &[String], Duration) -> Result<(), String> + Send + Sync>;
pub fn default_runner() -> ToolRunner {
Box::new(|executable, args, timeout| {
let mut cmd = Command::new(executable);
cmd.args(args);
let output = run_with_timeout(cmd, timeout).map_err(|e| e.to_string())?;
if !output.status.success() {
return Err(format!(
"{} exited {}: {}",
executable,
output.status.code().unwrap_or(-1),
combined_output(&output),
));
}
Ok(())
})
}
pub struct CastVideoConverter {
pub fps: u32,
pub idle_time_limit: f64,
pub columns: usize,
pub rows: usize,
pub cell_w: f64,
pub cell_h: f64,
pub font_px: u32,
pub padding: u32,
pub width: Option<usize>,
pub height: Option<usize>,
runner: ToolRunner,
}
impl Default for CastVideoConverter {
fn default() -> Self {
CastVideoConverter {
fps: 24,
idle_time_limit: 1.0,
columns: DEFAULT_COLUMNS,
rows: DEFAULT_ROWS,
cell_w: DEFAULT_CELL_W,
cell_h: DEFAULT_CELL_H,
font_px: DEFAULT_FONT_PX,
padding: DEFAULT_PADDING,
width: None,
height: None,
runner: default_runner(),
}
}
}
impl CastVideoConverter {
pub fn new() -> Self {
Self::default()
}
pub fn with_runner(runner: ToolRunner) -> Self {
CastVideoConverter {
runner,
..Self::default()
}
}
pub fn metrics(&self) -> SvgMetrics {
let mut metrics = SvgMetrics {
columns: self.columns,
rows: self.rows,
cell_w: self.cell_w,
cell_h: self.cell_h,
font_px: self.font_px,
padding: self.padding,
width: 0,
height: 0,
};
metrics.width = self.width.unwrap_or_else(|| metrics.derived_width());
metrics.height = self.height.unwrap_or_else(|| metrics.derived_height());
metrics
}
pub fn frame_width(&self) -> usize {
self.metrics().width
}
pub fn frame_height(&self) -> usize {
self.metrics().height
}
pub fn output_path_for(&self, cast_path: &str) -> String {
let p = Path::new(cast_path);
p.with_extension("mp4").to_string_lossy().to_string()
}
pub fn convert(&self, cast_path: &str, video_path: Option<&str>) -> Result<String, String> {
let output_path = video_path
.map(|s| s.to_string())
.unwrap_or_else(|| self.output_path_for(cast_path));
if let Some(parent) = Path::new(&output_path).parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
}
let frames = self.frames_from_cast(cast_path)?;
let tmpdir = tempfile::Builder::new()
.prefix("termproof_cast_frames_")
.tempdir()
.map_err(|e| e.to_string())?;
let metrics = self.metrics();
for (index, frame) in frames.iter().enumerate() {
let svg_path = tmpdir.path().join(format!("frame-{:05}.svg", index));
let png_path = tmpdir.path().join(format!("frame-{:05}.png", index));
fs::write(&svg_path, screen_svg(frame, &metrics)).map_err(|e| e.to_string())?;
(self.runner)(
RSVG_CONVERT,
&[
"--output".to_string(),
png_path.to_string_lossy().to_string(),
svg_path.to_string_lossy().to_string(),
],
RSVG_TIMEOUT,
)?;
}
let frame_glob = tmpdir.path().join("frame-%05d.png");
(self.runner)(
FFMPEG,
&[
"-y".to_string(),
"-framerate".to_string(),
self.fps.to_string(),
"-i".to_string(),
frame_glob.to_string_lossy().to_string(),
"-c:v".to_string(),
"libx264".to_string(),
"-pix_fmt".to_string(),
"yuv444p".to_string(),
"-crf".to_string(),
"15".to_string(),
"-preset".to_string(),
"slower".to_string(),
"-tune".to_string(),
"stillimage".to_string(),
"-movflags".to_string(),
"+faststart".to_string(),
output_path.clone(),
],
FFMPEG_TIMEOUT,
)?;
Ok(output_path)
}
fn frames_from_cast(&self, cast_path: &str) -> Result<Vec<AttributedScreen>, String> {
let contents = fs::read_to_string(cast_path).map_err(|e| e.to_string())?;
let mut lines = contents.lines();
let header_line = lines.next().ok_or("empty cast file")?;
let header: serde_json::Value =
serde_json::from_str(header_line).map_err(|e| e.to_string())?;
let width = header
.get("width")
.and_then(|v| v.as_u64())
.map(|v| v as usize)
.unwrap_or(self.columns);
let height = header
.get("height")
.and_then(|v| v.as_u64())
.map(|v| v as usize)
.unwrap_or(self.rows);
let mut vt = Vt::new(width.max(1), height.max(1));
let mut frames: Vec<AttributedScreen> = Vec::new();
let mut next_frame_at = 0.0_f64;
let mut playback_time = 0.0_f64;
let mut previous_cast_time = 0.0_f64;
let frame_interval = 1.0 / self.fps as f64;
for line in lines {
if line.trim().is_empty() {
continue;
}
let event: serde_json::Value = serde_json::from_str(line).map_err(|e| e.to_string())?;
let arr = match event.as_array() {
Some(a) if a.len() >= 3 => a,
_ => continue,
};
let event_type = arr[1].as_str().unwrap_or("");
if event_type != "o" {
continue;
}
let timestamp = arr[0].as_f64().unwrap_or(0.0);
let data = arr[2].as_str().unwrap_or("");
let elapsed = (timestamp - previous_cast_time).max(0.0);
playback_time += elapsed.min(self.idle_time_limit);
previous_cast_time = timestamp;
vt.feed_str(data);
while next_frame_at <= playback_time {
frames.push(attributed_screen_from_avt(&vt));
next_frame_at += frame_interval;
}
}
let final_snapshot = attributed_screen_from_avt(&vt);
if frames.is_empty() || frames.last() != Some(&final_snapshot) {
frames.push(final_snapshot);
}
Ok(frames)
}
}
fn attributed_screen_from_avt(vt: &Vt) -> AttributedScreen {
let rows = vt
.view()
.iter()
.map(|line| line.cells().iter().map(cell_from_avt).collect())
.collect();
let cursor = vt.cursor();
AttributedScreen {
rows,
cursor_row: cursor.row,
cursor_column: cursor.col,
cursor_hidden: !cursor.visible,
}
}
fn cell_from_avt(cell: &avt::Cell) -> AttributedCell {
let pen = cell.pen();
let width = cell.width().min(2) as u8;
AttributedCell {
text: if width == 0 {
String::new()
} else {
cell.char().to_string()
},
fg: avt_color(pen.foreground()),
bg: avt_color(pen.background()),
bold: pen.is_bold(),
dim: pen.is_faint(),
italic: pen.is_italic(),
underline: pen.is_underline(),
strikethrough: pen.is_strikethrough(),
reverse: pen.is_inverse(),
width,
}
}
fn avt_color(color: Option<avt::Color>) -> String {
match color {
None => "default".to_string(),
Some(avt::Color::Indexed(index)) => palette_color(index as u16),
Some(avt::Color::RGB(rgb)) => rgb_color(rgb.r, rgb.g, rgb.b),
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::Mutex;
use super::*;
fn cast_with(events: &str) -> (tempfile::TempDir, String) {
let dir = tempfile::tempdir().unwrap();
let cast = dir.path().join("r.cast");
fs::write(
&cast,
format!("{{\"version\":2,\"width\":10,\"height\":3}}\n{}", events),
)
.unwrap();
let path = cast.to_string_lossy().to_string();
(dir, path)
}
fn frames(events: &str) -> Vec<AttributedScreen> {
let (_dir, path) = cast_with(events);
CastVideoConverter::new().frames_from_cast(&path).unwrap()
}
#[test]
fn output_path_swaps_extension() {
let conv = CastVideoConverter::new();
assert_eq!(conv.output_path_for("/tmp/a/b.cast"), "/tmp/a/b.mp4");
}
#[test]
fn frames_from_simple_cast() {
let f = frames("[0.0, \"o\", \"hi\"]\n[0.5, \"o\", \"!\"]\n");
assert!(!f.is_empty());
assert_eq!(f.last().unwrap().text_lines(true)[0], "hi!");
}
#[test]
fn non_output_events_are_ignored() {
let f = frames("[0.0, \"i\", \"typed\"]\n[0.1, \"o\", \"ok\"]\n");
assert_eq!(f.last().unwrap().text_lines(true)[0], "ok");
}
#[test]
fn frame_count_follows_the_frame_rate() {
let f = frames("[0.0, \"o\", \"a\"]\n[1.0, \"o\", \"b\"]\n");
assert_eq!(f.len(), 25);
}
#[test]
fn a_long_pause_is_collapsed_to_the_idle_limit() {
let f = frames("[0.0, \"o\", \"a\"]\n[60.0, \"o\", \"b\"]\n");
assert_eq!(f.len(), 25);
}
#[test]
fn cast_frames_use_avts_width_table() {
let f = frames("[0.0, \"o\", \"\\ud83e\\ude89x\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(
row[0].width, 1,
"avt no longer measures U+1FA89 with the unicode-width 0.1 table; \
re-check whether avt and vt100 can now share one table"
);
assert_eq!(row[1].text, "x", "x should sit in the adjacent column");
let mut parser = vt100::Parser::new(3, 10, 0);
parser.process("\u{1FA89}x".as_bytes());
let live = crate::terminal::attributed::from_vt100(parser.screen());
assert_eq!(live.rows[0][0].width, 2);
assert_eq!(live.rows[0][2].text, "x");
}
#[test]
fn alternate_screen_repaints_are_captured() {
let f = frames(
"[0.0, \"o\", \"scrollback\"]\n\
[0.1, \"o\", \"\\u001b[?1049h\\u001b[H\\u001b[2Jalt\"]\n",
);
assert_eq!(f.last().unwrap().text_lines(true)[0], "alt");
}
#[test]
fn scroll_region_is_honored() {
let f = frames(
"[0.0, \"o\", \"a\\r\\nb\\r\\nkeep\"]\n\
[0.1, \"o\", \"\\u001b[1;2r\\u001b[2;1H\\n\\rc\"]\n",
);
let text = f.last().unwrap().text_lines(true);
assert_eq!(text[0], "b");
assert_eq!(text[1], "c");
assert_eq!(text[2], "keep");
}
#[test]
fn colors_survive_into_the_frame() {
let f = frames("[0.0, \"o\", \"\\u001b[31mred\\u001b[0m\"]\n");
let cell = &f.last().unwrap().rows[0][0];
assert_eq!(cell.fg, "red");
assert_eq!(f.last().unwrap().rows[0][3].fg, "default");
}
#[test]
fn indexed_and_rgb_colors_map_to_hex() {
let f = frames("[0.0, \"o\", \"\\u001b[38;5;196ma\\u001b[38;2;1;2;3mb\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(row[0].fg, "ff0000");
assert_eq!(row[1].fg, "010203");
}
#[test]
fn text_attributes_survive_into_the_frame() {
let f = frames("[0.0, \"o\", \"\\u001b[1;3;4;9;7ma\"]\n");
let cell = &f.last().unwrap().rows[0][0];
assert!(cell.bold && cell.italic && cell.underline);
assert!(cell.strikethrough && cell.reverse);
}
#[test]
fn wide_glyph_keeps_its_filler_column() {
let f = frames("[0.0, \"o\", \"\\u4f60x\"]\n");
let row = &f.last().unwrap().rows[0];
assert_eq!(row[0].width, 2);
assert_eq!(row[1].width, 0);
assert_eq!(row[2].text, "x");
}
#[test]
fn frames_are_rendered_and_stitched() {
let calls = Arc::new(Mutex::new(Vec::new()));
let sink = calls.clone();
let conv = CastVideoConverter::with_runner(Box::new(move |exe, args, _| {
sink.lock()
.expect("poisoned")
.push((exe.to_string(), args.to_vec()));
Ok(())
}));
let (dir, path) = cast_with("[0.0, \"o\", \"hi\"]\n");
let out = dir.path().join("out.mp4").to_string_lossy().to_string();
assert_eq!(conv.convert(&path, Some(&out)).unwrap(), out);
let calls = calls.lock().unwrap();
let (ffmpeg_exe, ffmpeg_args) = calls.last().unwrap();
assert_eq!(ffmpeg_exe, FFMPEG);
assert!(ffmpeg_args.contains(&"yuv444p".to_string()));
assert!(ffmpeg_args.contains(&"stillimage".to_string()));
assert!(ffmpeg_args.contains(&"24".to_string()));
assert!(calls[..calls.len() - 1]
.iter()
.all(|(e, _)| e == RSVG_CONVERT));
}
#[test]
fn a_frame_is_the_still_the_screenshot_renderer_would_draw() {
let seen = Arc::new(Mutex::new(Vec::new()));
let sink = seen.clone();
let conv = CastVideoConverter::with_runner(Box::new(move |exe, args, _| {
if exe == RSVG_CONVERT {
sink.lock()
.expect("poisoned")
.push(fs::read_to_string(&args[2]).expect("frame written"));
}
Ok(())
}));
let (dir, path) = cast_with("[0.0, \"o\", \"hi\"]\n");
let out = dir.path().join("out.mp4").to_string_lossy().to_string();
conv.convert(&path, Some(&out)).unwrap();
let expected = screen_svg(&frames("[0.0, \"o\", \"hi\"]\n")[0], &conv.metrics());
let seen = seen.lock().expect("poisoned");
assert_eq!(seen.first().map(String::as_str), Some(expected.as_str()));
}
#[test]
fn frame_size_is_derived_but_overridable() {
let conv = CastVideoConverter::new();
assert_eq!(conv.frame_width(), 120 * 10 + 20);
assert_eq!(conv.frame_height(), (40.0 * 22.0) as usize + 20);
let conv = CastVideoConverter {
width: Some(640),
height: Some(480),
..Default::default()
};
assert_eq!(conv.frame_width(), 640);
assert_eq!(conv.frame_height(), 480);
}
}