# Time

> How vidmake measures time: numbers are frames, strings like `"1.5s"`, `"500ms"`, `"45f"` and `"1:02.5"` are converted at the composition's fps, markers name the beats, and every animation time is local to its layer.

vidmake counts time in **frames**. Anywhere a time is expected, a number means frames and a string can
use seconds, milliseconds, frames or minutes. Animation times are **local**: they count from the
layer's own start, not from the start of the video.

## Time formats

| you write | means | at 30 fps |
|---|---|---|
| `45` | 45 frames | 1.5 s |
| `"45f"` | 45 frames | 1.5 s |
| `"1.5s"` | 1.5 seconds | 45 frames |
| `"500ms"` | 500 milliseconds | 15 frames |
| `"1:02.5"` | 1 minute 2.5 seconds (`m:ss`) | 1875 frames |
| `"1:00:00"` | 1 hour (`h:mm:ss`) | 108000 frames |
| `"2.5"` | 2.5 frames (a string without a unit is still frames) | 0.083 s |

Strings are converted with the composition's `fps`, so `"2s"` is 60 frames at 30 fps and 120 frames at
60 fps. Whitespace around the number is ignored. Anything else is an error that lists the valid forms:
``invalid time `2sec` (use frames, "2.5s", "500ms", "90f" or "m:ss")``.

Convert between the forms here:

*(An interactive time widget is shown here on the HTML page.)*

> [!TIP]
> Prefer seconds strings in hand-written projects. `"start": "2s"` stays two seconds if you change the
> composition from 30 to 60 fps; `"start": 60` becomes one second.

> [!WARNING]
> A bare number is always frames. `"duration": 3` is three frames (a tenth of a second at 30 fps), not
> three seconds. This is the most common timing mistake; `vidmake timeline` makes it obvious.

### Fractional frames

Times don't have to land on whole frames. `"0.25s"` at 30 fps is 7.5 frames, and animations are evaluated
at fractional times too (motion blur samples between frames, for instance). Two things are rounded: a
composition's total length (to a whole number of frames), and the frame you ask for on the command line
(`-t 0.25s` inspects frame 8).

## Where times are used

| place | fields |
|---|---|
| composition | `duration` |
| every layer | `start`, `duration`, `end` |
| [keyframes](https://vidmake.app/docs/animation.md) | the time of each key: `["1s", 500]`, `{ "t": "1s", "v": 500 }` |
| [tweens](https://vidmake.app/docs/animation.md) | `start` (alias `delay`), `end`, `duration` |
| [presets](https://vidmake.app/docs/presets.md) | `duration`, `delay` in `enter` / `exit` objects, and a motion's `duration` |
| [transitions](https://vidmake.app/docs/series.md) | `duration` |
| [stagger](https://vidmake.app/docs/text.md#stagger) (text, groups, repeat) | `over`, `max`, `each` |
| [repeat](https://vidmake.app/docs/repeat.md) | `delay` between copies |
| [video](https://vidmake.app/docs/video.md) and [audio](https://vidmake.app/docs/audio.md) | `trimStart`, `fadeIn`, `fadeOut`, and the times in a `volume` curve |
| [captions](https://vidmake.app/docs/captions.md) | inline `items` and `words` times (caption *files* use seconds) |
| [particles](https://vidmake.app/docs/particles.md) | `lifetime` |
| CLI | `-t`, `--frames`, `--at`, `--every`, `--from`, `--to` |

Two places use seconds instead of frames, because they are not time *fields*: the expression variable
`t` (and `gt`, `dur`), and the numbers inside `.srt`/`.vtt`/`.json` caption files.

## A layer's time window

Every layer is on screen during a window defined by three fields:

| field | default | meaning |
|---|---|---|
| `start` | `0` | when the layer appears, relative to its parent |
| `duration` | the rest of the parent | how long it stays |
| `end` | | alternative to `duration`: when it disappears, relative to the parent. `duration` wins if both are set |

The default `duration` depends on the layer: media layers (`video`, `audio`) last as long as their media
(after `trimStart`, divided by `playbackRate`), `use` layers as long as the composition they reference,
`series` groups as long as their scenes, `captions` until the last caption ends. Everything else lasts
until the end of its parent.

```json
{ "id": "title", "type": "text", "text": "Hi", "start": "1s", "duration": "2s" }
{ "id": "title", "type": "text", "text": "Hi", "start": "1s", "end": "3s" }
```

Both layers are visible from 1 s to 3 s. Outside its window a layer isn't drawn, doesn't play sound and
takes no space in a [layout](https://vidmake.app/docs/groups.md). Nothing extends past its parent: a child of a group that
ends at 4 s is cut at 4 s, whatever its own duration says.

## Local time

All animation inside a layer is measured from **the layer's own start**. In the demo below, the three
dots have the same keyframes, `"x": [[0, 320], ["1.5s", 1080]]`, and different `start` values. Each one
makes the same 1.5-second move, beginning whenever its layer begins:

**Demo: One animation, three start times: keyframe times count from each layer's own start** (rendered video: https://vidmake.app/media/time-local.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "local", "description": "One animation, three start times: keyframe times count from each layer's own start",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "track", "type": "rect", "repeat": { "count": 3, "layout": "column", "spacing": 150 }, "x": 700, "y": "50%",
        "width": 760, "height": 2, "fill": "#d8d1c1" },
      { "id": "label", "type": "text", "repeat": { "count": 3, "layout": "column", "spacing": 150 }, "anchor": "right", "x": 270,
        "y": "50%", "font": "JetBrains Mono", "fontSize": 20, "fill": "#8a857a", "text": "\"start\": \"${i}s\"" },
      { "id": "dot0", "type": "ellipse", "start": 0, "y": 210, "width": 36, "fill": "#161412",
        "x": [[0, 320], ["1.5s", 1080, "easeInOutCubic"]] },
      { "id": "dot1", "type": "ellipse", "start": "1s", "y": 360, "width": 36, "fill": "#161412",
        "x": [[0, 320], ["1.5s", 1080, "easeInOutCubic"]] },
      { "id": "dot2", "type": "ellipse", "start": "2s", "y": 510, "width": 36, "fill": "#161412",
        "x": [[0, 320], ["1.5s", 1080, "easeInOutCubic"]] },
      { "id": "clock", "type": "text", "repeat": { "count": 3, "layout": "column", "spacing": 150, "delay": "1s" }, "anchor": "left",
        "x": 1110, "y": "50%", "font": "JetBrains Mono", "fontSize": 22, "fill": "#161412", "text": "t = ${t:1}s" },
      { "id": "global", "type": "text", "text": "composition time ${gt:1}s", "font": "JetBrains Mono", "fontSize": 20,
        "letterSpacing": 2.4, "uppercase": true, "fill": "#c92a26", "y": "12%" },
      { "id": "code", "type": "text", "text": "each dot: \"x\": [[0, 320], [\"1.5s\", 1080]]", "font": "JetBrains Mono",
        "fontSize": 20, "fill": "#8a857a", "y": "89%" }
    ]
  }]
}
```

The timeline confirms the windows:

```console
$ vidmake timeline time-local.json
Composition `local` — 1280x720 @ 30fps, 120 frames (4.00s)

ID       TYPE         START      END      DUR  DETAILS
track.0  rect         0.00s    4.00s    4.00s  
track.1  rect         0.00s    4.00s    4.00s  
track.2  rect         0.00s    4.00s    4.00s  
label.0  text         0.00s    4.00s    4.00s  ""start": "0s""
label.1  text         0.00s    4.00s    4.00s  ""start": "1s""
label.2  text         0.00s    4.00s    4.00s  ""start": "2s""
dot0     ellipse      0.00s    4.00s    4.00s  
dot1     ellipse      1.00s    4.00s    3.00s  
dot2     ellipse      2.00s    4.00s    2.00s  
clock.0  text         0.00s    4.00s    4.00s  "t = 0.0s"
clock.1  text         1.00s    4.00s    3.00s  "t = 0.0s"
clock.2  text         2.00s    4.00s    2.00s  "t = 0.0s"
global   text         0.00s    4.00s    4.00s  "composition time 0.0s"
code     text         0.00s    4.00s    4.00s  "each dot: "x": [[0, 320], ["1.5s", 1080]"

         |0s             1s             2s             3s             |
track.0  |████████████████████████████████████████████████████████████|
track.1  |████████████████████████████████████████████████████████████|
track.2  |████████████████████████████████████████████████████████████|
label.0  |████████████████████████████████████████████████████████████|
label.1  |████████████████████████████████████████████████████████████|
label.2  |████████████████████████████████████████████████████████████|
dot0     |████████████████████████████████████████████████████████████|
dot1     |···············█████████████████████████████████████████████|
dot2     |······························██████████████████████████████|
clock.0  |████████████████████████████████████████████████████████████|
clock.1  |···············█████████████████████████████████████████████|
clock.2  |······························██████████████████████████████|
global   |████████████████████████████████████████████████████████████|
code     |████████████████████████████████████████████████████████████|
```

`trace` shows the middle dot in composition time: invisible until 1 s, then the same curve the first
dot ran at 0 s.

```console
$ vidmake trace time-local.json -l dot1 --every 0.25s --from 0.5s --to 2.75s
 FRAME    TIME        X        Y       W       H    ROT  SCALE  OPAC  NOTES
    15   0.50s  (not visible)
    23   0.77s  (not visible)
    30   1.00s    320.0    360.0    36.0    36.0    0.0   1.00  1.00  
    38   1.27s    337.1    360.0    36.0    36.0    0.0   1.00  1.00  
    45   1.50s    432.6    360.0    36.0    36.0    0.0   1.00  1.00  
    53   1.77s    724.8    360.0    36.0    36.0    0.0   1.00  1.00  
    60   2.00s    967.4    360.0    36.0    36.0    0.0   1.00  1.00  
    68   2.27s   1068.6    360.0    36.0    36.0    0.0   1.00  1.00  
    75   2.50s   1080.0    360.0    36.0    36.0    0.0   1.00  1.00  
    83   2.77s   1080.0    360.0    36.0    36.0    0.0   1.00  1.00
```

Local time applies to everything that animates:

- keyframe times and tween `start`/`end`/`duration`,
- `enter` presets (which play at the layer's start) and `exit` presets (which play at its end),
- text `stagger` and `reveal`, and the expression variables `t`, `f` and `p`.

### Groups nest time

A layer inside a [group](https://vidmake.app/docs/groups.md) is timed relative to the group. Its `start` counts from the
group's start, and its animations count from its own start, as always:

```json
{ "id": "scene2", "type": "group", "start": "5s", "duration": "4s", "children": [
  { "id": "headline", "type": "text", "text": "Part two", "enter": "fadeUp" },
  { "id": "detail", "type": "text", "text": "Details", "y": "65%", "start": "1s", "enter": "fadeUp" }
] }
```

`headline` appears at 5 s and `detail` at 6 s on the composition's clock. Move `scene2` to `"start": "8s"`
and both move with it, animations included. This is how you build a video out of scenes without
recalculating anything; a [series](https://vidmake.app/docs/series.md) goes one step further and computes the starts for you.

## Markers

Once a video has more than a few beats, the same times end up typed in many places: the card arrives at
2.5s, so the progress bar, the node color and the label all change at 2.5s too, each written in its own
layer's local time. Markers name a beat once, in composition time, and every layer points at the name:

```json
"markers": { "title": "0.3s", "underline": "@title+0.7s", "sub": "@underline+0.6s", "out": "3.4s" }
```

**Demo: Markers name the beats once; layers start at @title, draw on at @underline and read since('underline')** (rendered video: https://vidmake.app/media/time-markers.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "markers", "description": "Markers name the beats once; layers start at @title, draw on at @underline and read since('underline')",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "markers": { "title": "0.3s", "underline": "@title+0.7s", "sub": "@underline+0.6s", "out": "3.4s" },
    "layers": [
      { "id": "title", "type": "text", "text": "Name the beats", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 116,
        "letterSpacing": -5, "fill": "#161412", "y": 250, "start": "@title", "end": "@out", "enter": "fadeUp", "exit": "fade",
        "decoration": { "type": "underline", "color": "#c92a26", "thickness": 6, "offset": 10,
          "progress": { "from": 0, "to": 1, "start": "@underline", "duration": "0.5s", "ease": "easeOutCubic" } } },
      { "id": "sub", "type": "text", "text": "then point at them from any layer", "font": "Schibsted Grotesk", "fontSize": 40, "fill": "#4a4640",
        "y": 380, "start": "@sub", "end": "@out", "enter": "fadeUp", "exit": "fade" },
      { "id": "clock", "type": "text", "text": "since('underline') = ${since('underline'):2}s", "font": "JetBrains Mono", "fontSize": 22,
        "fill": "#161412", "y": 470 },
      { "id": "rail", "type": "rect", "anchor": "left", "x": 100, "y": 600, "width": 1080, "height": 2, "fill": "#d8d1c1" },
      { "id": "tick", "type": "rect", "width": 2, "height": 26, "y": 600, "fill": "#161412",
        "x": "=100 + 1080 * pick(i, [marker('title'), marker('underline'), marker('sub'), marker('out')]) / 4",
        "repeat": 4 },
      { "id": "tick-name", "type": "text", "text": "@${pick(i, ['title', 'underline', 'sub', 'out'])}", "font": "JetBrains Mono", "fontSize": 16,
        "fill": "#8a857a", "y": 640, "x": "=100 + 1080 * pick(i, [marker('title'), marker('underline'), marker('sub'), marker('out')]) / 4",
        "repeat": 4 },
      { "id": "playhead", "type": "rect", "width": 3, "height": 46, "y": 600, "fill": "#c92a26", "x": "=100 + 1080 * gt / 4" }
    ]
  }]
}
```

- **In time values.** `"@name"`, `"@name+0.25s"` and `"@name-10"` work wherever a time does: `start`, `end`,
  `duration` (a marker there means "until the marker"), keyframe times, tween `start`/`end` and preset `delay`.
  vidmake converts the marker into the right local time for you, so `"start": "@sub"` puts a layer at 1.6s
  even inside a group that starts at 1s, inside a [series](https://vidmake.app/docs/series.md), or on a [repeat](https://vidmake.app/docs/repeat.md) copy.
- **In expressions.** `since('name')` is the seconds since the marker (negative before it) and
  `marker('name')` is the marker's time in the layer's local seconds. `"opacity": "=map(since('strike'), 0, 0.3, 1, 0.3)"`
  dims a layer right after the strike, whatever the layer's own start.
- **Chained.** A marker can be written relative to an earlier one (`"@title+0.7s"`), so moving one beat moves
  everything after it.
- **Scoped.** Markers belong to their composition. A component used through [`use`](https://vidmake.app/docs/components.md) sees its own
  markers.

Retiming a video is now one edit: change `"underline": "@title+0.7s"` and the line, the subtitle that
follows it and the clock all move together. An unknown name is an error that lists the markers you have.

## Time in expressions

[Expressions](https://vidmake.app/docs/expressions.md) see time in both units, local and global:

| variable | meaning |
|---|---|
| `t` | seconds since the layer started |
| `f` | frames since the layer started |
| `p` | progress through the layer, 0 at its start to 1 at its end |
| `dur`, `durf` | the layer's duration in seconds and frames |
| `gt`, `gf` | seconds and frames since the composition started |
| `fps` | the composition's frame rate |
| `since('m')`, `marker('m')` | seconds since a [marker](#markers), and the marker's local time |

```json
"rotation": "=t * 90",
"opacity": "=map(f, 0, 15, 0, 1)",
"width": "=pw * p",
"text": "${gt:1}s"
```

`"=t * 90"` turns 90 degrees per second; `map(f, 0, 15, 0, 1)` fades in over the first 15 frames;
`"=pw * p"` grows a progress bar across the parent over the layer's life. The demo above prints `t` next
to each dot and `gt` at the top, so you can watch local and global time side by side.

## Time on the command line

Every CLI flag that takes a time accepts the same formats:

| flag | commands | example |
|---|---|---|
| `-t`, `--time` | `inspect`, `still` | `-t 2.5s`, `-t 1:02` |
| `-f`, `--frame` | `inspect`, `still` | `-f 75` |
| `--at` | `storyboard` | `--at 0,1.5s,2s,90` |
| `--frames` | `render` | `--frames 1s-3s` (end exclusive), `--frames 0-90` |
| `--every`, `--from`, `--to` | `trace` | `--every 0.25s --from 1s --to 3s` |

Markers work in all of them: `still -t @ship`, `storyboard --at @title,@underline+0.3s,@out`,
`render --frames @title..@out` (use `..` or `-@` between markers, since `@a-10` is an offset), and
`trace --from @underline`. `timeline` lists the markers and draws them under the Gantt chart.

> [!WARNING]
> `-t 2` means frame 2, not two seconds: the same "numbers are frames" rule applies. Write `-t 2s`.

A time outside the composition is an error: `frame 270 is outside the composition (0..120)`.

## Frame rate

`fps` is a composition field (default 30). A component used through [`use`](https://vidmake.app/docs/components.md) can have
its own frame rate; its timing is converted to the parent's, and validation warns that expressions using
frame numbers may differ. Using seconds strings and `t` rather than frame numbers and `f` keeps a
project independent of its frame rate.

## Next steps

- [Series and transitions](https://vidmake.app/docs/series.md) Let vidmake compute scene starts and overlaps.
- [Animation](https://vidmake.app/docs/animation.md) Keyframes, tweens and springs, all in local time.
- [Expressions](https://vidmake.app/docs/expressions.md) Everything you can compute from `t`, `f` and `p`.
- [Inspecting](https://vidmake.app/docs/inspecting.md) `timeline` and `trace` in depth.
