Caption Summary

Caption Summaries #

Pick a Video #

Video

Samples #

Make a selection above.

Using bart-large-cnn #

Using llama-3.3-70b-instruct-fp8-fast #

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:

// For this demo, `videos` is a list of VTT file URLs, grab value by index
const url = videos[parseInt(id)] ?? null;

// Load that VTT file from Stream:
const captions = await fetch(url).then(res => res.text(), res => `Could not fetch captions: ${res.status} ${res.statusText}`);

// Pull and concatenate the text portion of all the cues
const [meta, ...stack] = captions.split('\n\n');
const plaintext = stack.reduce((acc, cue) => {
  const [id, time, ...text] = cue.split('\n');
  return acc.concat(' ', text.join(' '));
}, '');

let summary;

// Send the text to the model and return the response.
switch (model) {
  case "bart":
    // Not really a summary, more like a truncated slight shortening
    summary = await env.AI.run('@cf/facebook/bart-large-cnn', {
      input_text: plaintext,
      max_length: 512,
    });
    break;
  case "llama":
    // Much more through / conversational summary
    const messages = [
      {role: 'system', content: 'Summarize this video transcript'},
      {role: 'user', content: plaintext},
    ];

    summary = await env.AI.run("@cf/meta/llama-3.3-70b-instruct-fp8-fast",
      {
        messages,
        stream: false,
        max_tokens: 256,
      }
    );
    break;
}

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

The Hard Part #

Unless signed URLs are enabled, Thumbnails are available publicly so Workers can pull them easily into models. However, VTT files for caption tracks require a JWT in a query arg to load, so a URL like this doesn’t work:

https://customer-CODE.cloudflarestream.com/VIDEO_ID/text/LANG_LOCALE.vtt

The HLS manifest (and the Pages Function powering this demo) includes this, which is signed by Stream itself so it can’t be generated by a customer.

https://customer-igynxd2rwhmuoxw8.cloudflarestream.com/541de0f2ce24dd64f849bfa961ba62b2/text/en.vtt?p=eyJ0eXBlIjoiZmlsZSIsInZpZGVvSUQiOiI1NDFkZTBmMmNlMjRkZDY0Zjg0OWJmYTk2MWJhNjJiMiIsIm93bmVySUQiOjM0MjA5Mjc1LCJjcmVhdG9ySUQiOiJzdHJlYW0iLCJ0cmFjayI6IjUwMzUxNzYwMjI0YTE3NjI1NjRmMTgzYTE2ODYyZDJjIiwicmVuZGl0aW9uIjoiOTExMTM0OTQ2IiwibXV4aW5nIjoiOTY2MzQ0ODgxIn0&s=wrUxwoXDjcKRwr5kNsK2wopPBxPCnQ1ZBMOUw5XCgwl8NjTDgg4Ow5xmZMO6w5I',