Skip to main content

Crate svg_to_video

Crate svg_to_video 

Source
Expand description

§svg-to-video

Crates.io Docs.rs License

A simple CLI tool for converting animated SVGs to frame-perfect, mathematically (or visually) lossless video files.

By leveraging a deterministic headless browser environment and piping uncompressed PNG frames directly into ffmpeg, this tool ensures zero dropped frames and accurate timing, completely bypassing the inconsistencies of real-time screen recording.

§Prerequisites

This tool acts as an orchestrator and requires the following external dependencies to be installed and available on your system:

  1. Google Chrome or Chromium: Required for the headless_chrome rendering engine.
  2. FFmpeg: Must be installed and accessible via your system’s $PATH.

§Installation

Ensure you have the Rust toolchain installed. You can install the CLI directly via Cargo:

cargo install svg-to-video

§Usage

The CLI requires an input SVG file and an output video path. By default, it renders a 5-second video at 60 FPS in 4K resolution (3840x2160) using Apple ProRes 422 HQ.

§Basic Example

svg-to-video input.svg output.mov

§Advanced Configuration

You can fully customize the duration, framerate, resolution, and output codec:

svg-to-video animation.svg render.mkv \
  --duration 10.5 \
  --fps 30 \
  --width 1920 \
  --height 1080 \
  --codec ffv1

§CLI Arguments

ArgumentTypeDefaultDescription
inputPathRequiredPath to the input SVG file.
outputPathRequiredPath to the output video file.
-d, --durationf645.0Duration of the animation to capture, in seconds.
--fpsu6460Target framerate for the output video.
--widthu323840Output video width in pixels.
--heightu322160Output video height in pixels.
-c, --codecEnumproresThe video codec to use for encoding.

§Supported Codecs

All supported codecs are strictly configured via FFmpeg flags for lossless (or virtually lossless archival) quality.

  • prores (Default): Apple ProRes 422 HQ. Optimized for Non-Linear Editors (NLEs) like Premiere or DaVinci Resolve. Recommended container: .mov
  • ffv1: Mathematically lossless. The ultimate archival format. Recommended container: .mkv
  • h264: H.264 Lossless (-crf 0). Large file size, mathematically perfect, broad compatibility. Recommended container: .mp4 or .mkv
  • h265: HEVC / H.265 Lossless. Better lossless compression ratios than H.264. Recommended container: .mp4 or .mkv
  • vp9: Google’s open-source lossless format. Recommended container: .webm or .mkv
  • av1: Next-generation open-source codec, mathematically perfect (-crf 0). Recommended container: .webm or .mkv

§How It Works

  1. Wrapper Generation: The SVG is injected into a temporary, perfectly sized HTML wrapper with a forced sRGB color profile.
  2. Headless Execution: A headless Chrome instance loads the HTML.
  3. Deterministic Stepping: The application pauses all native CSS/JS animations (document.getAnimations()) and internal SVG SMIL animations.
  4. Frame Extraction: It calculates the exact timestamp for each frame based on the requested FPS, advances the virtual playback head, and captures an uncompressed PNG screenshot.
  5. Stream Encoding: The raw PNG binaries are piped directly into ffmpeg via standard input (stdin) to avoid massive disk I/O, compiling the final container on the fly.

§License

This project is licensed under the MIT License.

Enums§

Codec
Supported output codecs for the video encoding pipeline. All codecs are configured for mathematically lossless or visually lossless output.

Functions§

capture_frames
Executes the core capture loop: pauses animations, advances timing precisely via JS, triggers a screenshot, and pipes the resulting PNG binary directly into FFmpeg.
launch_browser
Initializes an instance of Headless Chrome, enforces a consistent color profile, and navigates a new tab to the provided local HTML wrapper file.
prepare_html_wrapper
Reads the SVG content and wraps it in a perfectly sized HTML document.
spawn_ffmpeg_pipeline
Spawns the underlying FFmpeg process configured to accept continuous PNG streams via stdin and writes the output directly to the specified destination.
spawn_progress_monitor
Spawns a background thread to render a CLI progress bar dynamically.