0
Get subtitles for transcript
One script reply has been approved by the moderators Verified

Export your transcript in SRT or VTT format to use with a video player for subtitles and closed captions.

Created by hugo697 32 days ago Viewed 2795 times
0
Submitted by hugo697 Bun
Verified 32 days ago
1
//native
2
type Assemblyai = {
3
  apiKey: string;
4
};
5
/**
6
 * Get subtitles for transcript
7
 * Export your transcript in SRT or VTT format to use with a video player for subtitles and closed captions.
8
 */
9
export async function main(
10
  auth: Assemblyai,
11
  transcript_id: string,
12
  subtitle_format: "srt" | "vtt",
13
  chars_per_caption: string | undefined,
14
) {
15
  const url = new URL(
16
    `https://api.assemblyai.com/v2/transcript/${transcript_id}/${subtitle_format}`,
17
  );
18
  for (const [k, v] of [["chars_per_caption", chars_per_caption]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: "Bearer " + auth.apiKey,
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.text();
35
}
36