Use CasesClip a Live Stream

Clip a Live Stream

Send Klap a link to a stream that is live and it clips the stream as it is broadcast: first what was just streamed, then again about every 30 minutes, until the stream ends.

If you already generate shorts from videos, nothing changes in how you create the task, list the clips or export them. The differences are that the task stays processing for as long as the stream is followed, and that clips arrive in batches.

API Flow

1. Submit the Stream

Use the same endpoint as for a video. Klap detects that the link is live.

Endpoint Used: POST /tasks/video-to-shorts

const task = await postRequest("/tasks/video-to-shorts", {
  source_video_url: "https://www.twitch.tv/your_channel",
  max_clip_count: 5, // per pass
  max_live_duration: 7200, // follow at most 2 hours
});
 
console.log(task.status); // "processing"
console.log(task.live.state); // "live"

Accepted links are a YouTube video that is live, a Twitch channel and a Kick channel. See Live Streams for the details and the errors.

2. Get the Clips as They Arrive

With a webhook (recommended). Set your endpoint once, then handle two events:

app.post("/klap/webhook", express.raw({ type: "application/json" }), async (req, res) => {
  if (!isFromKlap(SECRET, req.get("Klap-Signature"), req.body.toString())) return res.sendStatus(400);
  res.sendStatus(200);
 
  const event = JSON.parse(req.body.toString());
  const { task, pass } = event.data;
 
  if (event.type === "task.pass_completed") {
    // New clips from one more pass of the stream
    for (const clipId of pass.clip_ids) await exportClip(task.output_id, clipId);
  }
  if (event.type === "task.ready" || event.type === "task.failed") {
    console.log(`Stream finished: ${task.live?.state}, ${task.live?.pass_count} passes`);
  }
});

By polling. Poll the task as usual. Each time live.pass_count goes up or the task ends, list the folder and handle the clips you have not seen yet:

const seen = new Set();
 
while (true) {
  const current = await getRequest(`/tasks/${task.id}`);
  const clips = await getRequest(`/projects/${current.output_id}`);
 
  for (const clip of clips) {
    if (seen.has(clip.id)) continue;
    seen.add(clip.id);
    await exportClip(current.output_id, clip.id);
  }
 
  if (current.status !== "processing") break; // "ready" or "error"
  await new Promise((resolve) => setTimeout(resolve, 60000));
}

The first clips are usually ready 10 to 20 minutes after the request.

3. Export the Clips

Exactly as for a video: POST /projects/{folder_id}/{project_id}/exports, then poll the export.

4. Stop Early (Optional)

Endpoint Used: POST /tasks/{task_id}/stop

await postRequest(`/tasks/${task.id}/stop`, {});

No new pass starts. The pass in progress finishes, then the task becomes ready.

When the Task Ends

The task becomesWhen
readyThe stream ended, you stopped it, or a limit was reached, and at least one pass made clips
errorThe stream is over and no pass made a clip

live.state and live.stop_reason say why it ended.

Cost

Each pass is billed as one input video plus the shorts it generates, so a stream costs more the longer it is followed. Use max_live_duration and max_clip_count to bound it. See Pricing.