Content Detection

Content Detection using Thumbnails API #

Stream offers Thumbnails of videos that have been uploaded.

These thumbnails can be run through AI inference models in a Worker.

Pick a Video #

Video ID
Timestamp
Fomat: 1s, 5s, 3m

Currently no error handling if request is past video duration!

Samples #

Make a selection above.



Worker Code #

This page sends a video ID and timestamp to a Pages Function, which pulls the same thumbnail displayed in the sample and runs it through Workers AI:

// Grab the thumbnail from Stream
const url = `https://cloudflarestream.com/${id}/thumbnails/thumbnail.jpg?height=720&time=${time}`;
const image = await fetch(url);

if (!image.ok) {
  return new Response(`Stream error: ${image.statusText}`, { status: 500 });
}

let content;

// Send the image to the requested model, then return the response:
switch (mode) {
  case "detect":
    content = await env.AI.run(
      "@cf/microsoft/resnet-50",
      {
        image: [... new Uint8Array(await image.arrayBuffer()) ],
      }
    );
    break;

  case "describe":
    content = await env.AI.run(
      "@cf/unum/uform-gen2-qwen-500m",
      {
        image: [... new Uint8Array(await image.arrayBuffer()) ],
        prompt: "Describe the setting and content of this image. If there are any people, describe what they are wearing.",
        max_tokens: 256,
      }
    );

    break;
}

return new Response(JSON.stringify(content, null, 2));