ws-apifix: videos save as animated images (F.save(video, gif | apng | webp), F.convert(avi, "gif"): frames / fps / maxFrames (300) / maxPixels (2^25)); VideoFrame.index restarts at the seek position; F.detect names WebM files "webm"

api: encoderFor finds image formats for a VideoDecoder, videoToImage() collects the frames; F.detect asks a
format's optional variant(r) for a more precise name (video/mkv: Mkv.variant, from Parse.docType).
codecs/video/Video: index after seek = round(pts * frameRate) + 1.
This commit is contained in:
4DBug committed 2026-10-31 14:00:36 -05:00
1 parent 10bd6b021f
commit d73b93c740
8 files changed
+207 -6

No files matched your search

+28 -5
View File
@@ -51,7 +51,7 @@ Times: `VideoFrame.pts` / `duration`, `seek` and every `duration` field are **se
| `pts` | seconds from the start of the stream |
| `duration` | seconds |
| `keyframe` | true for frames that decode on their own |
| `index` | 1-based, counted in output order since the decoder was opened |
| `index` | 1-based position in output order; after `seek` it restarts at the frame's own position (`round(pts * frameRate) + 1`) |
A `yuv420p` image holds three planes in one buffer: Y (`width * height` bytes, rows top down), then U and then V,
each `ceil(width / 2) * ceil(height / 2)` bytes. `image.meta.yuv` gives the matrix (`"bt601"` / `"bt709"`) and the
@@ -192,7 +192,7 @@ local mkv = assert(F.save(assert(F.video.open(mp4Bytes)), nil, { format = "mkv"
local back = assert(F.video.open(mkv))
assert(back.codec == "h264" and back.audio ~= nil)
local webm = assert(F.save(assert(F.video.open(webmBytes)), nil, { format = "webm" }))
assert(F.detect(webm) == "mkv") -- WebM is the mkv format
assert(F.detect(webm) == "webm" and F.format("webm").name == "mkv") -- WebM: the mkv format
local avi = assert(F.convert(mp4Bytes, "avi", { quality = 80 })) -- decoded, re-encoded as MJPEG
assert(F.video.open(avi).codec == "mjpeg")
local y4m = assert(F.save(assert(F.video.fromFrames({ F.Types.Image.new(16, 16) }, 0.04)), nil, { format = "y4m" }))
@@ -204,6 +204,30 @@ assert(F.detect(gifAvi) == "avi")
A VideoDecoder is consumed as it is saved: open the source again to save it twice. Uneven frame delays (a GIF's)
become a constant frame rate with repeated frames.
### As an animated image
Saving a video to an image format (`gif`, `apng`, `webp`) collects its frames, from the decoder's current position,
into an animated Image and writes that. Each frame keeps its own duration as its delay.
| Option | Default | Meaning |
|---|---|---|
| `frames` | all (up to the cap) | how many frames to keep |
| `fps` | the source's timing | the output frame rate: frames are picked at `1 / fps` steps; a source frame that covers several steps is held longer instead of repeated |
| `maxFrames` | 300 | the cap on frames written |
| `maxPixels` | 2^25 | a cap on `frames * width * height` (memory: 4 bytes per pixel and frame) |
Past a cap the rest of the video is left unread (no error).
```luau
local gif = assert(F.convert(aviBytes, "gif", { fps = 5 })) -- every other frame of a 10 fps clip, 0.2 s each
local anim = assert(F.image.load(gif))
assert(F.detect(gif) == "gif" and #anim.frames > 1 and math.abs(anim.frames[1].delay - 0.2) < 1e-9)
local v = assert(F.video.open(aviBytes))
v:seek(0.5) -- start half a second in
local apng = assert(F.save(v, nil, { format = "apng", frames = 3 }))
assert(#F.image.load(apng).frames == 3)
```
## The codec registry
`F.video.codecs` maps codec ids to entries `{ id, name, fourcc?, matroska?, new?, encode? }`. Containers use it to
@@ -295,9 +319,8 @@ See [FORMATS.md](../FORMATS.md). Subtitle tracks of Matroska files are read as c
- The only video encoder is Motion-JPEG. H.264, VP8 and the others are copied (remuxed) or decoded, never
encoded; `F.save(v, nil, { format = "webm" })` needs a WebM-compatible source.
- Saving consumes the decoder, and so does iterating `v.demuxer:packets()`: they share the read position.
- `frame.index` keeps counting after `seek`; use `frame.pts` to know where you are.
- A video does not save to an image format: `F.save(v, nil, { format = "gif" })` and `F.convert(aviBytes, "gif")`
fail with `UNKNOWN_FORMAT`. Collect `v:toRGBA(frame)` images and save `F.ops.image.fromFrames(images, delay)`.
- Saving a video as GIF / APNG / WebP holds every collected frame in memory as rgba8: keep `maxFrames` /
`maxPixels` low for big videos, or pick fewer frames with `fps`.
- Packet times are ticks, frame times are seconds. Divide by `track.timescale`.
- `nextFrame` returns yuv420p by default. Convert with `v:toRGBA(frame)` or open with `{ rgba = true }` before handing
pixels to `F.host.toEditableImage` / `toImageData`, which need rgba8.