Clip videos given a start and end time

Clips a video based on the specified start and end times provided in seconds.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Clip videos given a start and end time
8
 * Clips a video based on the specified start and end times provided in seconds.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    allowedOrigins?: string[];
15
    clippedFromVideoUID: string;
16
    creator?: string;
17
    endTimeSeconds: number;
18
    maxDurationSeconds?: number;
19
    requireSignedURLs?: boolean;
20
    startTimeSeconds: number;
21
    thumbnailTimestampPct?: number;
22
    watermark?: { uid?: string; [k: string]: unknown };
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/stream/clip`
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "X-AUTH-EMAIL": auth.email,
34
      "X-AUTH-KEY": auth.key,
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46