Expand description
fframes renders videos from Rust code. A video is a type that implements Video. It declares
the size, frame rate and duration once and returns an SVG tree (Svgr) for every frame from
the svgr! macro. The renderer rasterises the frames on all cores, mixes the audio and
encodes the result with FFmpeg.
§Pipeline
Video::duration,Video::define_scenesandVideo::audioare resolved into a frame timeline once per render. Scenes (Scene,Scenes,Overlap) are placed back to back, audio tracks (AudioMap,AudioTrack) are placed at their sample position.- For every frame the renderer builds a
Frame(index, time, scene offset) and aFFramesContext(media lookups, scene info, video size) and callsVideo::render_frame. Frames are rendered concurrently, so the method must be pure: read precomputed data fromself, no I/O, no panics. - The returned
Svgrbecomes ausvgr::Tree. With thecompile-time-svgtreefeature the macro emits the tree at compile time and every subtree without{}interpolation carries a static hash, which lets the backends cache its rasterisation across frames. Without the feature the markup is a string parsed per frame. - A rendering backend (
FFramesRenderBackend) rasterises the tree. The built-incpu::CpuRenderingBackendrenders one video segment per thread with tiny-skia. The Skia backend in thefframes_skia_renderercrate walks the tree on the GPU and also executesShaderlayers. - Segments are encoded through
FFmpeg(EncoderOptions), concatenated, and muxed with the audio mix (AudioMixOptions: summing, ducking, fades, master limiter).
§Minimal video
use fframes::{AudioMap, Duration, FFramesContext, Frame, RenderOptions, Svgr, Video, animation::Easing};
// Every file in the folder becomes a field; fonts are registered by family name.
fframes::include_media_dir!(pub struct Media, "media");
struct Hello<'a> {
media: &'a Media,
title: &'a str,
}
impl Video for Hello<'_> {
const FPS: usize = 30;
const WIDTH: usize = 1920;
const HEIGHT: usize = 1080;
fn duration(&self) -> Duration<'_> {
Duration::Seconds(3.0)
}
fn audio(&self) -> AudioMap<'_> {
AudioMap::none()
}
fn render_frame<'a>(&'a self, frame: Frame, _ctx: &FFramesContext<'a, '_>) -> Svgr<'a> {
let opacity = frame.animate(&fframes::timeline!(
at 0.0 => 0.5, animate 0.0_f32 => 1.0, Easing::EaseOut
));
fframes::svgr!(
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080"
width={Self::WIDTH} height={Self::HEIGHT}>
<text x="120" y="560" font-family="DM Sans" font-size="150" fill="#fff"
opacity={opacity}>
"Hello " {self.title}
</text>
</svg>
)
}
}
fn main() -> std::process::ExitCode {
let media = Media::prepare().expect("embedded media");
let video = Hello { media: &media, title: "world" };
// `render`, `frame`, `strip`, `inspect`, `audio` and more (feature `cli`).
fframes::cli::new(&video, RenderOptions { media: Some(&media), ..Default::default() }).run()
}Without the cli feature call render with the output path, a backend and RenderOptions,
or render single frames through Previewer.
§Feature flags
cpu_renderer(default): the tiny-skia basedcpu::CpuRenderingBackendand the encoding pipeline. Disable it for awasm32build.cli: theclimodule and itsclapdependency.compile-time-svgtree:svgr!builds the SVG tree at compile time and hashes static subtrees. Required by the Skia backend and byShader.exif: EXIF orientation of loaded images.3d:three_d, 3D scenes with the building blocks of three.js, rendered into an<image>of the frame. Runs on the CPU, also in the editor.wgpu:wgpu_layerputs textures rendered with wgpu into frames, and the Skia Vulkan backend draws them without a readback. With3d, the 3D renders on the GPU.- Codecs
h264,h265,aac,mp3lame,opus,vpx: compile the library into the staticFFmpegbuild. Some of them needlibav-agree-gpl,libav-agree-nonfreeorlibav-agree-version3, which state that you accept the correspondingFFmpeglicense terms. - Hardware acceleration
videotoolbox,audiotoolbox,vaapi,nvidia,qsv,vulkan,mediacodec: enable the platform encoders and decoders inFFmpeg.
§FFmpeg
Decoding, encoding and muxing use the FFmpeg libraries through ffmpeg-sys-fframes, re-exported
as ffmpeg_sys_fframes. On Linux and macOS FFmpeg is linked statically: a prebuilt build
for the target and codec features is downloaded during cargo build, and compiled from source
when there is none, which needs the toolchain listed in the repository README (nasm, clang).
The codec features link the system codec libraries (x264, x265, …), so their dev packages
must be installed either way. On Windows a prebuilt FFmpeg 9 is
linked through FFMPEG_DIR or vcpkg and the codec features are not available.
Re-exports§
pub use crate::usvgr::roxmltree;pub use fframes_media as media;pub use lazy_static;pub use lru;pub use media::bytemuck;pub use media::ffmpeg_sys_fframes;pub use serde;pub use ttf_parser;pub use usvgr;pub use media::exif;pub use rayon;
Modules§
- animation
- cli
- A ready-made command line for video binaries (feature
cli). One call gives every video the same tools for rendering and for checking the result without watching it: - concatenator
- continuity
- Finds pops: parts of a picture that change in a single frame between frames in which they stand still. An element that appears, vanishes or jumps in front of another element without moving there is easy to miss in a contact sheet but obvious when the video plays.
- cpu
- diagnostics
- Frame diagnostics: problems that do not fail a render but produce a wrong picture, such as a missing image, an unknown font family or text pushed off the canvas. The collector reports them without rasterizing anything, from three sources:
- element_
labels - Outlines and labels the elements of a frame that have an
id, to tell which shape is which in a rendered PNG (frame --ids). - error
- fframes_
logger - log
- sheet
- Compositing several frames into one image: labelled contact sheets and onion skins, which show a movement in a single picture.
- three_d
- 3D scenes for fframes, with an API after three.js. fframes re-exports this crate as
fframes::three_dwith its3dfeature:fframes = { features = ["3d", "wgpu"] }.
Macros§
- MKTAG
- ffmpeg_
action - ffmpeg_
loggable_ action - include_
media_ dir - Embed the contents of a directory in your crate.
- log
- option_
duration - option_
literal - svgr
- timeline
Structs§
- Abort
Signal - A signal that can be used to abort the rendering process from the other thread to stop the rendering process or to abort the rendering process inside the rendering without panicking.
- Active
Track - A track playing at some moment, see
AudioMixer::active_tracks_at. - Animate
Runtime Input - Audio
Map - Audio map represents when and how long each audio file should be played within a video or a scene.
In case of scene audio map is always relative to the scene timestamp (which is resolved based on
the
Video::define_scenes()). - Audio
MixOptions - Master bus settings.
- Audio
Mixer - Mixes the resolved audio map block by block. Blocks are expected in order (the limiter is stateful); jumping elsewhere resets the limiter.
- Audio
Report - Everything
fframes audio analyzereports. - Audio
Timeline Frames - Audio
Timeline Samples - Audio
Track - One audio file placed on the timeline, with its mix settings.
- Audio
Track Report - AvBuffer
- A counted reference to a libav buffer (
AVBufferRef). Hardware device and frames contexts are passed around this way. - Break
Lines Opts - Color
- The basic rgba color type. It is limited to the range of 0-255 because the most of video codecs only support 8-bit color depth.
- Combined
Media Provider - Combines several allocated media providers into one. Allows to combine static and dynamic media providers.
- CompactF
Frames Logger - Compressor
- Feed-forward compressor with a peak detector, linked on both channels.
- CpuFrame
Renderer - The built-in tiny-skia rasterizer as a
FrameRenderer. - Ducking
- Lowers a track while voice tracks (see
AudioTrack::voice) play. Driven by the timeline, not the signal: the level drops before the voice starts and comes back after it ends. - Dynamic
Media Provider - Media loaded at runtime. Start with
DynamicMediaProvider::default()and add files with thewith_*methods: - Encoder
- Encoder
Frame - Encoder
Input - What the video encoders of a render are opened with, the outcome of
FFramesRenderBackend::negotiate_encoder_input. EveryVideoFramesubmitted to the render has to match it. - Encoder
Options - Estimate
Text Width Options - FFrames
Context - Font
Query - Define a query that will resolve a font from the provided media source.
The values from the struct could be passed directly to the
<text>element attributes like: - Frame
- Contains all the temporal information about the current frame and the mutable links to the intermidient caches.
- Frame
Claim - A frame handed to a worker by the [
FrameScheduler]. - Frame
Convert Options - Frame
Layout - Where the planes of a software frame are inside its pixel buffer.
- Frame
Panic - Where and why
Video::render_framepanicked. - Frame
Pool - Hands out software
VideoFrames of one format and size. The pixel buffers go back to the pool once the encoder is done with a frame, so a render does not allocate per frame. - Frame
Report - What the diagnostics found in one frame.
- Limiter
Options - Lines
Logger - Backs
FFramesLoggerVariant::LinesandFFramesLoggerVariant::Json. - Loudness
Analysis - Per 100 ms energies of a K-weighted stereo signal, the base of every loudness value.
- Media
Directory - A struct representing owned media directory which can be used to process and fill the media provider. Basically represented as the vector of bytes where every media file is owned bytes vector.
- Named
Range - Preview
Request - What the CLI’s
previewcommand asks a real-time player to do. - Previewer
- A long lived session for rendering arbitrary frames of a video.
- Quiet
Logger - RawFont
Data - Render
Options - All the final render-specific options applies to the final video rendering pipeline including media resolution, logging, rendering backend, and encoding.
- Reported
Diagnostic - Resize
Video Frame - Resolved
Audio Map - The resolved
audio_mapcontain each audio file position and duration in specified units. - Resolved
Audio Track - A track with its position on the timeline resolved.
- Resolved
Rendering Timeline - Resolved
Scenes Timeline - Rgba
Encoder Frame Renderer - An
EncoderFrameRendererfor anyFrameRenderer: renders RGBA and converts it on the CPU. - Rgba
Frame - Straight (not premultiplied) RGBA8 pixels of one frame.
- Rgba
Frame Converter - Converts RGBA8 pixels into frames of a software pixel format on the CPU: the built-in SIMD converter for yuv420p, swscale for every other format.
- Rotate
- Scale
- Scene
Info - Represents current scene position and duration within a video.
- Scene
Report - Scene
With Audio - Scenes
- Scenes
With Audio - Section
Loudness - Shader
- A compiled-once GPU shader definition. Cheap to clone.
- Shader
Draw - Everything a renderer needs to execute one queued
Shader::draw. - Shader
Uniforms - Named uniform values for one
Shader::drawcall. - Silent
Logger - Svgr
- Svgr
Images - The svgr images of one render: the rasterizer of the backend and the images of static trees, which render once.
- Sync
Video Frame Input - Time
Base - Timeline
Index - Everything needed to resolve time specs of one video.
- Timeline
Report - The resolved structure of a video: size, duration, scenes and audio tracks.
- Timeline
Scene - A scene as seen by the addressing: its name and resolved frames (overlaps included).
- Track
Mix - How a track is mixed. Everything defaults to “play the file as is”.
- Transform
- SVG transform attribute that can be used directly
as
transform={...}argument using svgr! macro - Video
Decoders Worker - Video
Encoder Info - The video encoder of a render as a backend sees it while negotiating the frames it will
deliver. The codec is resolved the same way
Encoderdoes it, nothing is opened yet. - Video
Frame - A frame on its way to the video encoder: an owned libav
AVFrame. - Video
Size - Visualize
Frame Input - Wasm
Editor Video Frame Fallback - This is used by the editor as a fallback for the static image
- Wrapped
Text Line - Wrapped
Text Structure
Enums§
- AVPixel
Format - Audio
Data - Audio
Timestamp - Duration
- Effect
- One processing step of a track, see the
AudioTrackmethods with the same names. - Encoder
Output - What an
Encoderwrites. - FFrames
Logger Variant - Different options for logging rendering process.
- FFrames
Mode - FFrames
Renderer Error - Fade
Curve - Shape of a fade.
- Font
Stretch - Font
Style - Font
Variant - Overlap
- Render
Encoding Error - Thread or Chunk level error which can happen during parallelized rendering
- Sample
Size - Shader
Language - The language the shader source was written in.
- Shader
Uniform Value - A single value bound to a named
uniformof aShader. - Text
Align - Text
Overflow - How text that does not fit into its box is shortened, the equivalent of
the css
text-overflowproperty. - Time
Spec Error - Window
Function - YuvMatrix
- The matrix every RGB to
YCbCrconversion of the encoder input uses, limited range either way. The encoder tags the stream with it, so players decode the colors that were rendered.
Traits§
- Audio
Timeline Unit - Encoder
Frame Renderer - Rasterizes frames straight into what the video encoder takes. One per rendering thread; implementations keep their surfaces, caches and converters between frames.
- FFrames
Logger - FFrames
Render Backend - FFrames
Synced Video Frame - Font
Face - Font
Source - Frame
Renderer - Rasterizes converted frames. Implementations keep their surfaces and caches between calls, so rendering many frames through one renderer is cheap.
- Media
Provider - Scene
- Static
Media Provider - Svgr
Rasterizer - Renders converted svgr trees into images for
FFramesContext::render_svgr. A backend shares one rasterizer between all threads of a render. - Video
- The base fframes video trait. It represents how to render a video for a struct which becomes an input of the video.
Functions§
- FFMPEG_
AVERROR - analyze_
audio - Analyzes a stereo mix.
sectionsare named sample ranges (usually the scenes). - apply_
window_ function - av_
error_ to_ string - center_
spectrum_ low_ frequencies - Prettifies audio spectrum by moving low frequencies (first elements) in the middle and all the other elements to be proportionally positioned to the edges from the middle
- char_
to_ digit - encode_
wav - Encodes stereo samples as a WAV file: 16-bit PCM with TPDF dither (plays everywhere) or 32-bit float (keeps levels above full scale).
- estimate_
text_ width - Estimates the width of the text in pixels.
Returns
Noneif the font is not resolved. - fit_
transform - The transform that fits a tree into the output size. Videos that hardcode their
WIDTHas the<svg width>are scaled this way when rendering withscale_resolution. - get_
thread_ count - Convenient function that can be used by different backend implementation to
save the reused about of threads for the rendering process.
Allowing to override the default thread count using the
FFRAMES_NUN_THREADSenvironment variable. - get_
visualization - hamming_
window - Applies a Hamming window (https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows) to an array of samples.
- hann_
window - Applies a Hann window (https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows) to an array of samples.
- hardware_
device - Opens a libav hardware device (
av_hwdevice_ctx_create). Fails whenFFmpegwas built without it or the machine has no such device. - image_
pixels - The pixels of
image. An image on the GPU (fframes::wgpu_layer) is read back, which waits for the GPU.Nonewhen that fails. Other images are their own pixels. - is_
hardware_ pixel_ format truefor formats that reference a hardware surface (AV_PIX_FMT_VIDEOTOOLBOX,AV_PIX_FMT_VULKAN, …) instead of carrying pixels.- make_
logger - render
- Renders fframes video to the output destination path. Uses the provided video and media implementations as long as the reneder backend implementation.
- render_
frame - Renders a single frame into the output image buffer.
Prints all the rendering warns and errors for the frame along with the svg file itself.
Returns a byte representation specific to the render backend used.
For
CpuRenderBackendit is a RGBA image of the video size. - render_
frame_ guarded - Calls
Video::render_frame, turning a panic into an error that says which frame, second and scene it happened in. Rendering backends should call the video through this function. - rendered_
image - An image of premultiplied RGBA pixels,
width * 4bytes per row. Each image gets a new id, so a cache keyed by the id never returns an old image. - resolve_
shader_ draw - Returns the shader draw request if
imageis a placeholder created byShader::draw. Called by rendering backends for every raster image. - resolve_
timeline - seconds_
to_ frames - short_
scene_ name my_video::scenes::Intro<'_>->Intro