Flap Video Generator (preview)

Preview. This document has not been reviewed yet. Content may be incomplete or inaccurate.

1. Overview

Flap Video Generator lets any smart contract (a Flap vault, a game, a story contract) generate AI video clips on-chain. You call generateVideo() on FlapAIProvider with a text prompt and a flat BNB fee. Flap's backend generates the clip, pins it to IPFS, and writes the result back on-chain.

Each requester (msg.sender) owns an append-only video session. Each fulfilled clip is appended to it, and the clips are encoded so that playing them back-to-back gives one continuous stream. A contract can grow a story clip by clip, and anyone can watch the whole session as a single video.

Clips can be conditioned on reference images (IPFS CIDs). That gives you two controls:

  • Continuity: the next clip starts exactly where the previous one ended.
  • Character consistency: the same character keeps the same look across clips.

Reference type 5 gives you both in one clip (see Β§3). Section 4 shows a real deployed consumer.

Live demo: video.munch.cash plays real on-chain sessions on BSC Testnet and BNB Mainnet (switch at the top of the page). Connect a wallet to extend the story with your own prompt. To watch the session of any requester, open https://video.munch.cash/?network=<mainnet|testnet>&consumer=<address>.

flowchart TD
    subgraph on-chain["πŸ”— On-Chain"]
        Consumer(["🏦 Your Contract<br/>(vault / story consumer)"])
        Provider(["βš™οΈ FlapAIProvider"])
        Session(["🎞️ Video Session<br/>(per requester, append-only)"])
    end

    subgraph off-chain["☁️ Off-Chain"]
        Backend(["πŸ€– Flap Video Backend"])
        Model(["🎬 Video Model"])
        IPFS(["πŸ“¦ IPFS<br/>(clips + last frames)"])
    end

    Consumer -->|"β‘  generateVideo(prompt, referenceType, referenceCids) + fee"| Provider
    Provider -->|"β‘‘ emit FlapAIProviderVideoRequested"| Backend
    Backend -->|"β‘’ prompt + reference images"| Model
    Model -->|"β‘£ video"| Backend
    Backend -->|"β‘€ upload clip + last frame"| IPFS
    Backend -->|"β‘₯ submit clip CIDs"| Provider
    Provider -->|"⑦ append clip"| Session
    Provider -->|"β‘§ onVideoRequestSettled(result)"| Consumer

Key properties

PropertyValue
FeeFlat fee per clip, read it from videoGenerateFee(); msg.value must match exactly
ConcurrencyOne pending request per requester. A second call reverts until the first one is fulfilled or failed
OutputMPEG-TS clip CID + last-frame JPEG CID + duration, stored on-chain in the requester's session
Clip format (current)H.264 MPEG-TS, 1280Γ—720 (16:9), 24 fps, ~8 s, with audio, about 2 MB
Typical latencyAbout 45–135 s from request to fulfillment
CallbackOptional best-effort IFlapVideoReceiver.onVideoRequestSettled, which can never block fulfillment
FailureTerminal FAILED with a machine-readable reason. No refund

2. Deployed Addresses

The video generator lives on the same FlapAIProvider contract as the Flap AI Oracle.

NetworkChain IDFlapAIProviderFee per clipCallback gas limit
BSC Testnet970xFfddcE44e8cFf7703Fd85118524bfC8B2f70b7440.01 BNB2,000,000
BNB Mainnet560xaEe3a7Ca6fe6b53f6c32a3e8407eC5A9dF8B7E390.01 BNB2,000,000

Always call videoGenerateFee() at request time. Never hardcode the fee. It is configurable and may change. A wrong msg.value (too much or too little) reverts with FlapAIProviderWrongVideoFee.

Testnet note: video generation is expensive, so the testnet backend runs on a limited budget and may stop fulfilling if it runs out. If you'd like to test on testnet, feel free to reach out to us.

Demo consumers

Both are ReferenceStoryVideoConsumer (Β§4) and can be watched and extended in the demo app.

NetworkConsumerCharacterStoryWatch
BSC Testnet0xB7f4398BEECE5Ec0da06497e647107D697c55c66Ice boy (2D cartoon)The Ice Boy Who Melted Upwardvideo.munch.cash/?network=testnet
BNB Mainnet0x8b527D3f104A1945BD68B391fBd6b4a00B7EA3a2Arthur (photorealistic)Arthur's Boat, 8 clips, 64 svideo.munch.cash/?network=mainnet

3. Reference Types

generateVideo takes a uint8 referenceType that tells the backend how to use the reference image CIDs (IPFS CIDs, each ≀ 128 bytes) you pass in referenceCids:

CodeConstantreferenceCidsCountWhat the model getsEnabled
0VIDEO_REF_NONE[]0Text onlyβœ…
1VIDEO_REF_PREV_LAST_FRAME[] (the provider fills it in on-chain)0Your previous clip's last frame as the exact opening frame. Needs at least one previous clipβœ…
2VIDEO_REF_FIRST_FRAME[start]1Your image as the exact opening frameβœ…
3VIDEO_REF_FIRST_AND_LAST_FRAME[start, end]2Exact opening and closing frames❌ (not enabled)
4VIDEO_REF_CHARACTER[ref1, …]1–3Soft references only (character, subject, style). No frame is pinnedβœ…
5VIDEO_REF_START_FRAME_WITH_REFS[start, ref1, …, ref6]2–7An exact opening frame plus 1–6 referencesβœ…
6β€”β€”β€”Removed. Reverts with FlapAIProviderUnsupportedReferenceType(6)❌

Always check isVideoReferenceTypeEnabled(type) before using a type: a disabled or unknown type reverts with FlapAIProviderUnsupportedReferenceType.

The legacy one-argument generateVideo(prompt) is still supported. It uses type 0 for the first clip and type 1 for every later clip, and is not affected by which types are enabled.

Rules per type

  • Type 1: the provider resolves the CID from your session on-chain. The event and getVideoReferenceCids show the resolved CID. It reverts with FlapAIProviderNoPreviousClip on your first request, so a session must start with another type.
  • Type 4: the references guide the look but do not pin the opening frame, so consecutive clips can jump-cut. Prompts usually say which image is which ("Reference image 1 is the main character…"). To continue roughly from your last clip, add your own getLastVideoFrameCid(you) as one of the references.
  • Type 5 (recommended for stories):
    • referenceCids[0] is always the start frame: the video opens exactly on it. Pass getLastVideoFrameCid(you) for a seamless join with your previous clip, or "" for no start frame (the model picks the opening shot).
    • referenceCids[1..] are the references. Name them in the prompt as Image 1, Image 2, …, in order. The start frame is not numbered.
    • "" is allowed only at index 0. An empty CID anywhere else reverts with FlapAIProviderInvalidReferenceCid(i).
    • An empty start frame still counts towards the total, so [""] alone reverts with FlapAIProviderBadReferenceCidCount (count 1).
    • The provider stores exactly what you pass; nothing is looked up.
// Type 5: seamless continuation + a consistent character
string[] memory cids = new string[](2);
cids[0] = provider.getLastVideoFrameCid(address(this)); // "" on the first clip -> no start frame
cids[1] = CHARACTER_CID;                                  // "Image 1"
provider.generateVideo{value: provider.videoGenerateFee()}(
    "Image 1 is the main character. She opens the door and steps into the rain.", 5, cids);

Which type to use

You want…Use
Plain text-to-video0
An exact join with your previous clip, nothing else1
A specific opening image2
Character consistency, without an exact join4 (or 5 with "" as the start frame)
An exact join and a consistent character5
flowchart LR
    subgraph T1["Type 1: PREV_LAST_FRAME"]
        A1["Previous clip's<br/>last frame"] -->|"pinned as frame 0"| B1["New clip"]
    end
    subgraph T4["Type 4: CHARACTER"]
        A4["Character image(s)"] -->|"soft guide"| B4["New clip"]
    end
    subgraph T5["Type 5: START_FRAME_WITH_REFS"]
        A5["Start frame<br/>(e.g. previous last frame)"] -->|"pinned as frame 0"| B5["New clip"]
        C5["Image 1 … Image 6<br/>(character, objects, style)"] -->|"soft guide"| B5
    end

Type 5 gives both controls in one clip: the opening frame is pinned and the references keep the character consistent. Before type 5, the only way to get both was to alternate type 4 (re-anchor the character, with a visible cut) and type 1 (seamless join, with the character slowly drifting). That pattern still works and is what the example consumer in Β§4 uses.

Prompt tips:

  • Say which reference image is which at the start of the prompt (type 4: "Reference image 1 is…"; type 5: "Image 1 is…", not counting the start frame).
  • Restate the character's key traits (colours, outfit, style) in the prompt. The model then follows them more closely.

Asset requirements

The backend enforces these, not the contract:

  • Each CID must be an image (JPEG, PNG or WebP) that can be fetched through Flap's IPFS gateway. A token-metadata JSON CID is not an image: the request fails with OTHER.
  • Images smaller than 256 px on a side are upscaled automatically.
  • 16:9 start frames work best, because the output is 16:9.
  • Photorealistic likenesses of real people and copyrighted characters may be refused by the model (CONTENT_POLICY).

A FAILED request is final, and its fee is not refunded. Check your CIDs and prompt before you send the request.

Limits

ConstantValue
MAX_VIDEO_REFERENCE_CIDS7 (cap for any type)
MAX_VIDEO_START_FRAME_REFERENCES6 (references after the start frame, type 5)
MAX_VIDEO_CHARACTER_REFERENCES3 (type 4)
MAX_VIDEO_REFERENCE_CID_LENGTH128 bytes

A wrong number of CIDs for the type reverts with FlapAIProviderBadReferenceCidCount(referenceType, count).


4. Example: ReferenceStoryVideoConsumer

ReferenceStoryVideoConsumer is a small contract that builds a continuous story around one fixed character. It predates type 5 and uses the alternating type 4 / type 1 pattern; a new consumer can use type 5 [getLastVideoFrameCid(this), CHARACTER_CID] on every clip instead (see the type 5 example). Anyone can pay the fee to add the next clip and choose, per clip, between:

  • Type 1: continue seamlessly from the previous clip.
  • Type 4: re-anchor on the character, with the previous last frame as a scene hint.

The contract checks the reference CIDs and adds the "which image is which" prefix to the prompt itself, so the prompt always matches the images.

NetworkBSC Testnet
Consumer0xB7f4398BEECE5Ec0da06497e647107D697c55c66 (source verified)
Provider0xFfddcE44e8cFf7703Fd85118524bfC8B2f70b744
Character imagebafkreigo6g3mkveu5w3l7ud56qr4oq3sa62hawcdmybdhbi5agurqwm5ye (an "ice boy", flat 2D cartoon style)

Request flow

sequenceDiagram
    participant U as Caller (anyone)
    participant C as ReferenceStoryVideoConsumer
    participant P as FlapAIProvider
    participant B as Flap Video Backend

    U->>C: generateVideo(prompt, type, cids) + fee
    C->>P: getLastVideoFrameCid(this)
    alt type 1 (PREV_LAST_FRAME)
        C->>C: require a previous clip, cids == []
    else type 4 (CHARACTER)
        C->>C: require cids == [CHARACTER_CID] (first clip)<br/>or [CHARACTER_CID, lastFrame]
        C->>C: prefix prompt with "image 1 = character, image 2 = previous scene"
    end
    C->>P: generateVideo{value: fee}(fullPrompt, type, cids)
    P-->>B: FlapAIProviderVideoRequested
    B->>P: submit clip (videoCid, lastFrameCid, duration)
    P->>C: onVideoRequestSettled(result)
    C->>C: store latest clip / failure

The contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {IFlapAIProvider, IFlapVideoReceiver} from "src/plugins/AIProvider/IFlapAIProvider.sol";

/// @dev Reference-type surface of FlapAIProvider used by this consumer.
interface IFlapAIProviderReferenceTypes {
    function generateVideo(string calldata prompt, uint8 referenceType, string[] calldata referenceCids)
        external
        payable
        returns (uint256 requestId);
    function getLastVideoFrameCid(address user) external view returns (string memory);
}

contract ReferenceStoryVideoConsumer is IFlapVideoReceiver {
    uint8 public constant VIDEO_REF_PREV_LAST_FRAME = 1;
    uint8 public constant VIDEO_REF_CHARACTER = 4;

    string internal constant CHARACTER_HINT =
        "Reference image 1 is the main character: keep the exact same character design, colors, outfit and art style. ";
    string internal constant SCENE_HINT =
        "Reference image 2 is the final frame of the previous clip: continue from that scene, same setting, lighting and camera framing, as a natural continuation. ";

    IFlapAIProvider public immutable provider;
    string public CHARACTER_CID;

    uint256 public lastRequestId;
    string public latestVideoCid;
    string public latestFrameCid;
    uint32 public latestDurationMs;
    bool public lastRequestFailed;
    IFlapAIProvider.VideoFailureReason public lastFailureReason;
    string public lastFailureDetail;

    event VideoGenerationRequested(
        uint256 indexed requestId, address indexed caller, string prompt, uint8 referenceType, string[] referenceCids
    );
    event VideoGenerationFulfilled(uint256 indexed requestId, string videoCid, string lastFrameCid, uint32 durationMs);
    event VideoGenerationFailed(uint256 indexed requestId, IFlapAIProvider.VideoFailureReason reason, string failureDetail);

    error WrongFee(uint256 sent, uint256 required);
    error EmptyPrompt();
    error OnlyProvider();
    error RequestAlreadyPending(uint256 requestId);
    error ReferenceTypeNotAllowed(uint8 referenceType);
    error NoPreviousClip();
    error BadCharacterReferences();

    constructor(address provider_, string memory characterCid_) {
        require(provider_ != address(0), "zero provider");
        require(bytes(characterCid_).length != 0, "empty character cid");
        provider = IFlapAIProvider(provider_);
        CHARACTER_CID = characterCid_;
    }

    /// @param referenceType VIDEO_REF_PREV_LAST_FRAME (1) or VIDEO_REF_CHARACTER (4).
    /// @param referenceCids Type 1: empty. Type 4: [CHARACTER_CID] for the first clip,
    ///                      [CHARACTER_CID, current last frame] afterwards.
    function generateVideo(string calldata prompt, uint8 referenceType, string[] calldata referenceCids)
        external
        payable
        returns (uint256 requestId)
    {
        uint256 fee = provider.videoGenerateFee();
        if (msg.value != fee) revert WrongFee(msg.value, fee);
        if (bytes(prompt).length == 0) revert EmptyPrompt();
        if (lastRequestId != 0) {
            IFlapAIProvider.VideoRequestView memory prior = provider.getVideoRequest(lastRequestId);
            if (prior.status == IFlapAIProvider.VideoRequestStatus.PENDING) revert RequestAlreadyPending(lastRequestId);
        }

        IFlapAIProviderReferenceTypes p = IFlapAIProviderReferenceTypes(address(provider));
        // No race: the provider's one-pending rule means the last frame can't change
        // before this request is recorded.
        string memory lastFrame = p.getLastVideoFrameCid(address(this));
        bool hasLastFrame = bytes(lastFrame).length != 0;

        string memory fullPrompt;
        if (referenceType == VIDEO_REF_PREV_LAST_FRAME) {
            // Continuity: the provider resolves the previous last frame on-chain.
            if (!hasLastFrame) revert NoPreviousClip();
            if (referenceCids.length != 0) revert BadCharacterReferences();
            fullPrompt = prompt;
        } else if (referenceType == VIDEO_REF_CHARACTER) {
            // Character consistency: [character] or [character, previous last frame].
            uint256 expected = hasLastFrame ? 2 : 1;
            if (referenceCids.length != expected) revert BadCharacterReferences();
            if (keccak256(bytes(referenceCids[0])) != keccak256(bytes(CHARACTER_CID))) revert BadCharacterReferences();
            if (hasLastFrame && keccak256(bytes(referenceCids[1])) != keccak256(bytes(lastFrame))) {
                revert BadCharacterReferences();
            }
            fullPrompt = hasLastFrame
                ? string.concat(CHARACTER_HINT, SCENE_HINT, prompt)
                : string.concat(CHARACTER_HINT, prompt);
        } else {
            revert ReferenceTypeNotAllowed(referenceType);
        }

        requestId = p.generateVideo{value: msg.value}(fullPrompt, referenceType, referenceCids);
        lastRequestId = requestId;
        emit VideoGenerationRequested(requestId, msg.sender, prompt, referenceType, referenceCids);
    }

    function onVideoRequestSettled(VideoRequestResult calldata result) external override {
        if (msg.sender != address(provider)) revert OnlyProvider();
        if (result.status == IFlapAIProvider.VideoRequestStatus.FULFILLED) {
            latestVideoCid = result.videoCid;
            latestFrameCid = result.lastFrameCid;
            latestDurationMs = result.durationMs;
            lastRequestFailed = false;
            emit VideoGenerationFulfilled(result.requestId, result.videoCid, result.lastFrameCid, result.durationMs);
        } else if (result.status == IFlapAIProvider.VideoRequestStatus.FAILED) {
            lastRequestFailed = true;
            lastFailureReason = result.failureReason;
            lastFailureDetail = result.failureDetail;
            emit VideoGenerationFailed(result.requestId, result.failureReason, result.failureDetail);
        }
    }

    // ---- views ----

    function getLastFrame() external view returns (string memory) {
        return IFlapAIProviderReferenceTypes(address(provider)).getLastVideoFrameCid(address(this));
    }

    function getFullSession() external view returns (IFlapAIProvider.VideoClip[] memory) {
        uint256 len = provider.getVideoSessionLength(address(this));
        return provider.getVideoSessionSlice(address(this), 0, len);
    }
}

Design notes:

  1. The session belongs to the consumer contract, because it is the msg.sender of the provider call. Many callers can extend one shared story.
  2. The character CID is fixed in the constructor. Type-4 calls must pass it as reference image 1, so no caller can swap the character.
  3. The last frame is read from the provider (getLastVideoFrameCid(address(this))), not from the consumer's own callback storage. The value is still correct if a callback was skipped or ran out of gas.
  4. The prompt prefix matches the CID order the contract enforces. "Image 1 is the character, image 2 is the previous scene" is always true.
  5. The callback only records state. It is best-effort and gas-capped, so heavy logic doesn't belong there.

Example: an 8-clip story

This 8-clip story, "The Ice Boy Who Melted Upward", was generated on the testnet consumer above. It alternates type 4 and type 1:

#TypereferenceCidsPurposePrompt (abridged)
14[character]Establish the characterThe ice boy wakes up alone inside a giant snow globe on a kitchen table…
21[]Seamless continuationThe glass softens like jelly; he pushes through and drops onto the table…
34[character, lastFrame]New scene, re-anchor characterHe follows floating ice cubes out of the window into a sky where it snows upward…
41[]Seamless continuationA whale made of folded newspaper swims past; he rides its fin higher…
54[character, lastFrame]New scene, re-anchor characterThe whale delivers him to a floating island where tiny suns sunbathe…
61[]Seamless continuationHe melts, but his water flows upward into a cloud shaped like his face…
74[character, lastFrame]Re-anchor characterThe cloud rains back down and rebuilds him, covered in crystal flowers…
81[]Seamless endingHe shakes a new snow globe; the camera reveals the whole world is inside one…

Calling it with cast:

CONSUMER=0xB7f4398BEECE5Ec0da06497e647107D697c55c66
CHAR=bafkreigo6g3mkveu5w3l7ud56qr4oq3sa62hawcdmybdhbi5agurqwm5ye
RPC=https://bsc-testnet-rpc.publicnode.com
FEE=$(cast call 0xFfddcE44e8cFf7703Fd85118524bfC8B2f70b744 "videoGenerateFee()(uint256)" --rpc-url $RPC | awk '{print $1}')

# Type 4: re-anchor the character, previous last frame as scene reference
LAST=$(cast call $CONSUMER "getLastFrame()(string)" --rpc-url $RPC | tr -d '"')
cast send $CONSUMER "generateVideo(string,uint8,string[])" \
  "The ice boy follows a trail of floating ice cubes into a sky where it snows upward." \
  4 "[$CHAR,$LAST]" --value $FEE --rpc-url $RPC --private-key <KEY>

# Type 1: seamless continuation (wait for the previous request to settle first)
cast send $CONSUMER "generateVideo(string,uint8,string[])" \
  "A whale made of folded newspaper swims past; the ice boy grabs its fin and rides it higher." \
  1 "[]" --value $FEE --rpc-url $RPC --private-key <KEY>

5. Playing a Session

A session is an ordered list of VideoClips. Every clip is a separate, immutable MPEG-TS file on IPFS (videoCid). Together they are designed to play as one continuous video: load the clip list from the chain, give it to an MPEG-TS player as consecutive segments, and the viewer sees one timeline across all clips.

Why MPEG-TS

Every VideoClip carries startPtsMs, its start time on the session's timeline: 0 for the first clip, then the previous clip's startPtsMs + durationMs (the cumulative duration of all earlier clips). The backend encodes each clip with its timestamps already shifted by that offset, so clip N's first frame is stamped exactly where clip N-1 ended.

This only works because of how MPEG-TS is built:

  • No global header. An MPEG-TS file is a flat sequence of self-describing packets with absolute timestamps. Two TS files placed back-to-back (as player segments, or even byte-concatenated) are still a valid stream.
  • Timestamps live in the stream. Each clip can be encoded once with its final position on the session timeline. Neither the player nor any server has to rewrite anything.
  • Clean splices. Clips are encoded with no B-frames (so presentation and decode timestamps are always equal) and start with a keyframe. Decode time never jumps backwards at a clip boundary, and every clip can be decoded on its own from its first packet.

What this means for you:

MPEG-TS (what the session uses)Plain MP4 per clip
Append a new clipUpload one more file; earlier clips never changeEach file has its own header (moov) and a timeline starting at 0
Play clips back-to-backOne continuous timeline, no reset between clipsPlayer resets or seeks at every clip, or you re-mux everything
InfrastructureStatic files on IPFS + the clip list on-chainNeeds re-encoding or a server-generated HLS/DASH manifest
Content addressingEach clip CID is final the moment it's on-chainA merged file would get a new CID every time it grows

In short: the session is append-only and content-addressed, and MPEG-TS is the format where "append one more immutable file" and "play everything as one video" are compatible. The on-chain clip list is the playlist.

durationMs and startPtsMs are in milliseconds on-chain. mpegts.js segment durations are in seconds, so always pass durationMs / 1000.

Reading the session

From a contract:

uint256 len = provider.getVideoSessionLength(consumer);
IFlapAIProvider.VideoClip[] memory clips = provider.getVideoSessionSlice(consumer, 0, len);
// clips[i].videoCid      β†’ MPEG-TS segment
// clips[i].durationMs    β†’ segment duration (ms)
// clips[i].startPtsMs    β†’ segment start on the session timeline (ms)
// clips[i].lastFrameCid  β†’ thumbnail / reference for the next clip

From a frontend, with viem. user is the address that called generateVideo (usually your consumer contract):

import { createPublicClient, http, type Address } from "viem";
import { bsc } from "viem/chains"; // use `bscTestnet` for testnet

// FlapAIProvider: BNB Mainnet. Testnet: 0xFfddcE44e8cFf7703Fd85118524bfC8B2f70b744
const FLAP_AI_PROVIDER: Address = "0xaEe3a7Ca6fe6b53f6c32a3e8407eC5A9dF8B7E39";

const abi = [
  {
    type: "function",
    name: "getVideoSessionLength",
    stateMutability: "view",
    inputs: [{ name: "user", type: "address" }],
    outputs: [{ name: "", type: "uint256" }],
  },
  {
    type: "function",
    name: "getVideoSessionSlice",
    stateMutability: "view",
    inputs: [
      { name: "user", type: "address" },
      { name: "start", type: "uint256" },
      { name: "count", type: "uint256" },
    ],
    outputs: [
      {
        name: "clips",
        type: "tuple[]",
        components: [
          { name: "requestId", type: "uint256" },
          { name: "videoCid", type: "string" },
          { name: "lastFrameCid", type: "string" },
          { name: "durationMs", type: "uint32" },
          { name: "startPtsMs", type: "uint64" },
          { name: "createdAt", type: "uint64" },
          { name: "referenceType", type: "uint8" },
        ],
      },
    ],
  },
] as const;

const client = createPublicClient({ chain: bsc, transport: http() });

/** Reads every fulfilled clip of `user`'s session, in timeline order. */
export async function fetchSession(user: Address) {
  const length = await client.readContract({
    address: FLAP_AI_PROVIDER,
    abi,
    functionName: "getVideoSessionLength",
    args: [user],
  });
  if (length === 0n) return [];

  return client.readContract({
    address: FLAP_AI_PROVIDER,
    abi,
    functionName: "getVideoSessionSlice",
    args: [user, 0n, length],
  });
  // β†’ readonly { requestId: bigint; videoCid: string; lastFrameCid: string;
  //     durationMs: number; startPtsMs: bigint; createdAt: bigint; referenceType: number }[]
}

export type VideoClip = Awaited<ReturnType<typeof fetchSession>>[number];

For long sessions, page through with getVideoSessionSlice(user, start, count). It clamps count to the end of the session.

Playing with mpegts.js

mpegts.js is an open-source HTML5 MPEG-TS player. It transmuxes TS to fragmented MP4 in the browser and plays it through Media Source Extensions (MSE). Give it all clips as one multi-segment source: it loads and stitches the segments itself, and the native <video> seek bar covers the whole session.

npm install mpegts.js

Segment URLs point at any IPFS gateway: https://<your-gateway>/ipfs/<videoCid>. The gateway must send CORS headers (Access-Control-Allow-Origin), because mpegts.js downloads the segments with fetch. A dedicated gateway is recommended for anything beyond testing.

Minimal example (plain HTML, no build step)

A single HTML file that reads a session from BSC Testnet and plays it:

<!doctype html>
<html>
<body>
  <video id="video" controls playsinline style="width: 100%; max-width: 960px"></video>

  <!-- mpegts.js UMD build: exposes a global `mpegts` -->
  <script src="https://cdn.jsdelivr.net/npm/mpegts.js@1.8.2/dist/mpegts.js"></script>
  <script type="module">
    import { createPublicClient, http, parseAbi } from "https://esm.sh/viem@2";
    import { bscTestnet } from "https://esm.sh/viem@2/chains";

    const PROVIDER = "0xFfddcE44e8cFf7703Fd85118524bfC8B2f70b744"; // FlapAIProvider (BSC Testnet)
    const USER = "0x...";                     // the requester whose session you want to play
    const GATEWAY = "https://<your-gateway>"; // IPFS gateway with CORS enabled

    const abi = parseAbi([
      "function getVideoSessionLength(address user) view returns (uint256)",
      "function getVideoSessionSlice(address user, uint256 start, uint256 count) view returns ((uint256 requestId, string videoCid, string lastFrameCid, uint32 durationMs, uint64 startPtsMs, uint64 createdAt, uint8 referenceType)[])",
    ]);
    const client = createPublicClient({ chain: bscTestnet, transport: http() });

    const len = await client.readContract({
      address: PROVIDER, abi, functionName: "getVideoSessionLength", args: [USER],
    });
    const clips = await client.readContract({
      address: PROVIDER, abi, functionName: "getVideoSessionSlice", args: [USER, 0n, len],
    });

    const video = document.getElementById("video");
    if (clips.length > 0 && mpegts.isSupported()) {
      const player = mpegts.createPlayer({
        type: "mse",
        isLive: false,
        segments: clips.map((c) => ({
          url: `${GATEWAY}/ipfs/${c.videoCid}`,
          duration: c.durationMs / 1000, // seconds
        })),
      });
      player.attachMediaElement(video);
      player.load();
      player.play()?.catch(() => {}); // autoplay may need a user click first
    } else {
      // Fallback: no MSE (or empty session). Offer the individual clips instead.
      video.replaceWith(...clips.map((c, i) => {
        const a = document.createElement("a");
        a.href = `${GATEWAY}/ipfs/${c.videoCid}`;
        a.textContent = `Clip ${i + 1} (.ts) `;
        return a;
      }));
    }
  </script>
</body>
</html>

React / Next.js component

Sessions grow over time, so a real app re-reads the session (for example every few seconds) and appends new clips as they're fulfilled. mpegts.js can't add a segment to a running player, so the component below rebuilds the player whenever a new clip shows up. It then seeks back to the viewer's current position, so the rebuild doesn't look like a restart.

"use client";

import { useEffect, useRef } from "react";
import type Mpegts from "mpegts.js";

type Player = ReturnType<typeof Mpegts.createPlayer>;

export interface Clip {
  requestId: bigint;
  videoCid: string;
  durationMs: number;
}

const GATEWAY = "https://<your-gateway>"; // IPFS gateway with CORS enabled

export function SessionPlayer({ clips }: { clips: readonly Clip[] }) {
  const videoRef = useRef<HTMLVideoElement>(null);
  const playerRef = useRef<Player | null>(null);
  const builtForRef = useRef<bigint | null>(null); // requestId of the newest clip loaded

  // (Re)build the player whenever a new clip is appended to the session.
  useEffect(() => {
    const video = videoRef.current;
    if (!video || clips.length === 0) return;

    const newest = clips[clips.length - 1].requestId;
    if (builtForRef.current === newest) return; // nothing new (e.g. a poll re-render)

    const isFirstBuild = builtForRef.current === null;
    const resumeAt = isFirstBuild ? 0 : video.currentTime;
    const shouldPlay = isFirstBuild || video.ended || !video.paused;
    let cancelled = false;

    (async () => {
      // Dynamic import: mpegts.js touches `window`, so never load it during SSR.
      const mpegts = (await import("mpegts.js")).default;
      if (cancelled || !mpegts.isSupported()) return; // no MSE: render a fallback instead

      // mpegts.js cannot append a segment to a running player, so replace it.
      playerRef.current?.destroy();
      const player = mpegts.createPlayer({
        type: "mse",
        isLive: false,
        segments: clips.map((c) => ({
          url: `${GATEWAY}/ipfs/${c.videoCid}`,
          duration: c.durationMs / 1000, // SECONDS, not milliseconds
        })),
      });
      playerRef.current = player;
      player.attachMediaElement(video);
      player.load();
      builtForRef.current = newest;

      // Put the viewer back where they were, so the rebuild is invisible.
      if (resumeAt > 0) video.currentTime = resumeAt;
      if (shouldPlay) {
        const p = player.play();
        if (p) p.catch(() => {}); // autoplay may be blocked until the user interacts
      }
    })();

    return () => {
      cancelled = true;
    };
  }, [clips]);

  // Tear down on unmount.
  useEffect(
    () => () => {
      playerRef.current?.destroy();
      playerRef.current = null;
    },
    []
  );

  return <video ref={videoRef} controls playsInline style={{ width: "100%" }} />;
}

Usage: keep the clips from fetchSession(user) in client state, re-fetch on an interval, and render <SessionPlayer clips={clips} />. Only replace the array when the session actually changed (compare length / last requestId), so that background polls don't trigger rebuilds.

Jump to a clip. Since startPtsMs is each clip's position on the timeline, seeking to clip i is video.currentTime = Number(clips[i].startPtsMs) / 1000. To highlight the clip that's playing, find the last clip whose startPtsMs <= video.currentTime * 1000.

Seeking inside a player built from every clip makes mpegts.js download all earlier clips first, which is slow for later clips. A faster option (used by the demo app) is to rebuild the player from clips.slice(i), so only clip i and later are downloaded. mpegts.js then starts its timeline at clip i, so add clips[i].startPtsMs back when mapping video.currentTime to the session timeline.

Caching clips

Clips are content-addressed, so a /ipfs/<cid> URL never changes and can be cached forever. The browser HTTP cache alone is not reliable for them, especially on mobile: mobile disk caches evict multi-megabyte clips quickly. The demo app adds a small service worker (clip-cache-sw.js) that keeps every played clip in Cache Storage, keyed by URL, and answers later requests (including Range requests) from there. Browsers may still clear site storage under pressure, and iOS Safari clears it after about 7 days without a visit.

Browser support and fallbacks

  • mpegts.js needs Media Source Extensions. Always check mpegts.isSupported() before creating a player.
  • Desktop Chrome, Edge, Firefox and Safari are supported. On iPhone, MSE isn't available before iOS 17.1. Newer iOS versions go through Apple's Managed Media Source, which mpegts.js supports, but test on your target devices.
  • If MSE is unavailable, a simple fallback is to link or download the individual clips (https://<your-gateway>/ipfs/<videoCid>) or show the lastFrameCid images as a storyboard.
  • Downloading a whole session: because the clips are MPEG-TS with continuous timestamps, concatenating their bytes in order gives one valid .ts file (for example cat clip0.ts clip1.ts … > session.ts). It plays in desktop players such as VLC and mpv, or can be converted to MP4 without re-encoding, e.g. ffmpeg -i session.ts -c copy session.mp4.

6. Interface Reference

Requesting

/// Text-to-video for the first clip, then automatically PREV_LAST_FRAME (legacy).
function generateVideo(string calldata prompt) external payable returns (uint256 requestId);

/// Explicit reference type + caller-supplied reference CIDs (see Β§3).
function generateVideo(string calldata prompt, uint8 referenceType, string[] calldata referenceCids)
    external payable returns (uint256 requestId); // 0x1d095ca2

function videoGenerateFee() external view returns (uint256);
function isVideoReferenceTypeEnabled(uint8 referenceType) external view returns (bool);

Both versions need a non-empty prompt and msg.value == videoGenerateFee(), and allow one pending request per requester. The session belongs to msg.sender: when a contract calls the provider, the session is the contract's. Video request IDs are a separate counter from the AI Oracle's.

Reading

function getVideoRequest(uint256 requestId) external view returns (VideoRequestView memory);
function getVideoReferenceCids(uint256 requestId) external view returns (uint8 referenceType, string[] memory referenceCids);
function getVideoSessionLength(address user) external view returns (uint256);
function getVideoClip(address user, uint256 index) external view returns (VideoClip memory);
function getVideoSessionSlice(address user, uint256 start, uint256 count) external view returns (VideoClip[] memory);
function getLastVideoFrameCid(address user) external view returns (string memory);
function getLastVideoRequestId(address user) external view returns (uint256); // 0 = none
function videoCallbackGasLimit() external view returns (uint256);

getVideoReferenceCids returns the CIDs exactly as passed; for type 1, the resolved last frame. getVideoSessionSlice clamps to the end of the session and never reverts out of range.

Polling for the result:

id = getLastVideoRequestId(you)
repeat getVideoRequest(id).status until 2 (FULFILLED) or 3 (FAILED)
FULFILLED: getVideoClip(you, getVideoSessionLength(you) - 1)

Data types

enum VideoRequestStatus { NONE, PENDING, FULFILLED, FAILED }

enum VideoFailureReason { NONE, CONTENT_POLICY, PROVIDER_ERROR, PIPELINE_ERROR, TIMEOUT, OTHER }

struct VideoClip {
    uint256 requestId;
    string videoCid;      // MPEG-TS clip
    string lastFrameCid;  // JPEG of the clip's last frame
    uint32 durationMs;
    uint64 startPtsMs;    // start on the session's continuous timeline
    uint64 createdAt;
    uint8 referenceType;  // VIDEO_REF_* used for this clip
}

struct VideoRequestView {
    uint256 requestId;
    address requester;
    VideoRequestStatus status;
    uint8 referenceType;
    uint64 timestamp;
    uint128 feePaid;
    VideoFailureReason failureReason;
}

Callback (optional)

interface IFlapVideoReceiver {
    struct VideoRequestResult {
        uint256 requestId;
        IFlapAIProvider.VideoRequestStatus status; // FULFILLED or FAILED
        string videoCid;                            // FULFILLED only
        string lastFrameCid;                        // FULFILLED only
        uint32 durationMs;                          // FULFILLED only
        IFlapAIProvider.VideoFailureReason failureReason; // FAILED only
        string failureDetail;                       // FAILED only
    }

    function onVideoRequestSettled(VideoRequestResult calldata result) external;
}

The callback runs after the clip has been appended to the session. It gets at most videoCallbackGasLimit() gas. A revert, out-of-gas, or missing implementation is tolerated and recorded in FlapAIProviderVideoCallbackAttempted(requestId, requester, success). EOA requesters are never called back.

CONTENT_POLICY means the same prompt would be refused again, so change it before retrying. PROVIDER_ERROR / TIMEOUT mean a fresh request may succeed. A FAILED request never adds a clip, so the next type-1 request still continues from the last fulfilled clip.

Events

event FlapAIProviderVideoRequested(
    uint256 indexed requestId, address indexed requester, string prompt,
    uint8 referenceType, string[] referenceCids, uint256 feePaid
);
event FlapAIProviderVideoFulfilled(
    uint256 indexed requestId, address indexed requester, string videoCid,
    string lastFrameCid, uint32 durationMs, uint64 startPtsMs, uint256 sessionIndex
);
event FlapAIProviderVideoFailed(
    uint256 indexed requestId, address indexed requester, VideoFailureReason reason, string failureDetail
);
event FlapAIProviderVideoCallbackAttempted(uint256 indexed requestId, address indexed requester, bool success);
event FlapAIProviderVideoReferenceTypeUpdated(uint8 indexed referenceType, bool enabled);

In FlapAIProviderVideoRequested, referenceCids is already resolved: for type 1 it holds the previous clip's last-frame CID, and for type 5, [0] is the start frame (possibly "") followed by the references.

Errors

ErrorSelectorWhen
FlapAIProviderWrongVideoFee(sent, required)0x8931e73cmsg.value != videoGenerateFee()
FlapAIProviderEmptyVideoPrompt()0x9714cd62Empty prompt
FlapAIProviderVideoRequestAlreadyPending(requester, pendingRequestId)0xe7e4f422The requester already has a pending request
FlapAIProviderUnsupportedReferenceType(referenceType)0x866b1f09Type unknown or not enabled
FlapAIProviderBadReferenceCidCount(referenceType, count)0xa45ea42cWrong number of CIDs for the type
FlapAIProviderInvalidReferenceCid(index)0x167c425bEmpty CID where not allowed (anywhere except index 0 of type 5) or CID > 128 bytes
FlapAIProviderNoPreviousClip(requester)0x86a72ebbType 1 with an empty session

7. Checklist

  • Read videoGenerateFee() at call time and send exactly that amount
  • Handle the one-pending-request rule (check getVideoRequest(lastRequestId).status before submitting)
  • Check isVideoReferenceTypeEnabled(type) before using a type
  • Start every session with type 0, 2, 4 or 5 (with "" as the start frame). Type 1 needs a previous clip
  • Pin your character image to IPFS (an image, not a metadata JSON) and keep its CID fixed in your contract
  • For a seamless join and a consistent character, use type 5 [getLastVideoFrameCid(this), character]
  • Say which reference image is which in the prompt (type 5: Image 1, Image 2, … not counting the start frame), and restate the character's key traits
  • Read the last frame from getLastVideoFrameCid(address(this)), not from callback-only storage
  • Keep onVideoRequestSettled light and check msg.sender == provider