# Footage


`footage` draws one frame of a video file into a rectangle. Which frame is up to you: you pass the time in the file, computed from the scene's time like everything else, so a clip can play, play slowly, freeze, or be cut into pieces by ordinary arithmetic. Once drawn it is a picture like any other: groups move, fade, grade and filter it.

> **Note:** Footage is decoded by ffmpeg (`ffmpeg` and `ffprobe` must be on the `PATH`). The browser build has no ffmpeg, and `footage` draws nothing there, so this page has no live demos.

## Drawing a clip

```odin
footage :: proc(path: string, src: f32, r: Rect, fit := Image_Fit.Cover, alpha: f32 = 1, focus := Vec2{0.5, 0.5}, zoom: f32 = 1)
```

| Argument | Meaning |
| --- | --- |
| `path` | The video file, relative to the project directory. Anything ffmpeg reads. |
| `src` | Seconds into the file. Before 0 the first frame is held, after the end the last one. |
| `r` | Where to draw it, in stage px. `fx.stage()` is the whole frame. |
| `fit` | `.Cover` (the default) fills `r` and crops what does not fit, `.Contain` shows the whole frame inside `r`, `.Fill` stretches it to `r`. |
| `alpha` | Opacity, 0..1. |
| `focus` | With `.Cover`: which part stays in view when the frame is cropped, 0..1 on each axis. `{0.5, 0.5}` keeps the centre, `{0, 0}` the top left corner. |
| `zoom` | With `.Cover`: crops further in around `focus`. 1 is no extra crop; values below 1 count as 1. |

```odin
clip :: proc(s: fx.Shot) {
	// From second 12.5 of the file, at normal speed, filling the frame.
	fx.footage("assets/karp.mp4", 12.5 + s.lt, fx.stage())
}

portrait :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	// In a card, keeping the face (right of centre, upper part) in view, pushing in slowly.
	fx.footage("assets/karp.mp4", 40 + s.lt, {1100, 140, 600, 800}, focus = {0.6, 0.4}, zoom = 1 + 0.15 * s.p)
}
```

`fit`, `focus` and `zoom` work exactly as they do for [`fx.image`](/docs/draw/#images), which `footage` calls with the decoded frame.

`footage` draws pictures only. For the clip's sound, add a [`Sound`](/docs/sound/) with the same file and the same offset: `{name = "karp", file = "assets/karp.mp4", from = 12.5, at = 20, dur = 4}`.

## Time is an argument

Because `src` is just a number, the cut is in the expression:

```odin
fx.footage(clip, 12.5 + s.lt, r)          // plays
fx.footage(clip, 12.5 + s.lt * 0.5, r)    // half speed
fx.footage(clip, 14.2, r)                 // a freeze frame
fx.footage(clip, 12.5 + fx.ramp(s.lt, RAMP[:]), r) // a speed ramp
```

[`fx.ramp`](/docs/edit/#speed-ramps) turns a list of speeds over time into the source time for exactly this.

The file's own frame rate decides which frame a time means: `src` is rounded down to a frame of the file. A 30 fps clip in a 60 fps video shows each of its frames twice, and the second time costs nothing, because the frame is already there.

## How frames are decoded

Each render thread decodes for itself. The first time a thread draws a file, it asks `ffprobe` for the file's size, frame rate and length (once per process), and starts an `ffmpeg` process that seeks to the frame asked for and writes raw frames into a pipe. The pipe keeps playing forward from there:

- Asking for **the same frame** again returns the image the thread already has.
- Asking for **the next frame**, or one up to two seconds ahead, reads forward through the pipe. The next frame costs one decode; skipping ahead decodes and throws away the frames in between.
- Asking for **an earlier frame, or one more than two seconds ahead**, closes the pipe and starts a new ffmpeg at the new spot. That is a cut, and it costs a process start and a seek: about 0.1 s.

Renders draw frames in order, each worker its own stretch of the video, so a clip that plays forward costs one decode per frame, and every cut to another place costs one restart per worker that draws it.

Frames come out of ffmpeg at the file's frame rate (variable frame rates are made constant), scaled with a bicubic filter to at most `MAX_DECODE_W` = 1920 px wide (smaller files are not enlarged) and as BGRA. A 4K source is therefore decoded at 4K but handed over at 1920 px: sources larger than the video cost decoding time and add nothing.

```odin
MAX_DECODE_W :: 1920
MAX_PIPES    :: 2
```

A thread keeps at most `MAX_PIPES` = 2 pipes open. When a third file is drawn, the pipe the thread used least recently is closed first. Its last frame stays available, but the next different frame from that file needs a restart. This is what keeps a render with many workers and many clips within memory, and it has two consequences worth knowing:

- Two clips on screen at once (a split screen, a cross-fade between two files) play as cheaply as one. A frame that draws **three or more different files** restarts a pipe for each of them on every frame.
- There is one decoder per file per thread. Drawing **the same file at two different times** in one frame jumps back and forth: at least one restart per frame.

Playing a clip **backwards** is the same problem: every frame is a step back, so every frame is a restart.

## Preparing footage

A restart seeks to the last keyframe before the frame it wants and decodes forward from there. Camera files and downloads often have a keyframe only every few seconds, so each cut decodes up to that much for nothing. Footage recut with a keyframe every 12 frames restarts fastest:

```bash
ffmpeg -i in.mp4 -an -c:v libx264 -g 12 out.mp4
```

Do this for anything you cut quickly or scrub through in the editor, where every jump of the playhead is a restart (about 0.1 s each).

## Memory

Every open pipe is an ffmpeg process holding a few decoded frames, and every decoder keeps one frame of its own (8 MB at 1920×1080). A render worker has its encoder, its frames and up to two footage decoders: about 1 GB in all at 1080p. `effects render` therefore starts one worker per core but no more than the available memory allows (0.7 workers per free GB); `--workers n` overrides it.

For scale: an edit of 48.6 s at 30 fps with footage in every frame renders in 62 s on a 16-thread laptop and peaks at about 15 GB.

## Asking about a file

```odin
Media :: struct {
	w, h:     int,
	fps:      f32,
	rate:     string, // the frame rate as ffprobe gives it (30000/1001)
	duration: f32,
	frames:   int,
}

media :: proc(path: string) -> (Media, bool)
```

`media` returns a video file's size in pixels, frame rate, length in seconds and number of frames, read with `ffprobe` once per process and remembered. The second result is false when the file cannot be read (a message is printed once). Use it to lay out a clip at its own aspect ratio, or to stop at its end:

```odin
m, ok := fx.media("assets/karp.mp4")
if ok {
	h := 900 * f32(m.h) / f32(m.w)
	fx.footage("assets/karp.mp4", min(s.lt, m.duration), {510, 540 - h / 2, 900, h})
}
```

## The frame itself

```odin
frame         :: proc(path: string, src: f32) -> ^sk.Image
release_media :: proc()
```

`frame` is what `footage` draws: the decoded frame as an image, or `nil` if the file cannot be read. Pass it to `fx.image`, or to anything else that takes an image. It is valid until the next call for the same file on the same thread, so use it right away and do not keep it. It does not exist in the browser build.

`release_media` closes the calling thread's pipes and frees its frames. You do not call it: the renderers do, through `Video.release`, when a worker is done and when the editor reloads the code.

## Reference

| Name | What it is |
| --- | --- |
| `footage(path, src, r, fit, alpha, focus, zoom)` | Draws the frame at `src` seconds of a video file into `r`. |
| `frame(path, src)` | That frame as an image, valid until the next call for the file on this thread. |
| `media(path)` | A file's `Media` and whether it could be read. |
| `Media` | `w`, `h`, `fps`, `rate`, `duration`, `frames`. |
| `MAX_DECODE_W` | 1920: frames wider than this are scaled down to it. |
| `MAX_PIPES` | 2: decoders a thread keeps running at once. |
| `release_media()` | Closes the calling thread's decoders; called by the renderers. |
