MCPcopy Create free account
hub / github.com/bjsi/incremental-everything / fetchTranscript

Function fetchTranscript

src/lib/youtube_transcript.ts:32–65  ·  view source on GitHub ↗
(
    videoIdOrUrl: string,
    lang?: string
)

Source from the content-addressed store, hash-verified

30export interface Transcript {
31 language: string;
32 /**
33 * 'manual': captions the uploader made; 'auto': YouTube's speech
34 * recognition; 'whisper': transcribed from the audio by the helper.
35 */
36 kind: 'manual' | 'auto' | 'whisper';
37 segments: TranscriptSegment[];
38}
39
40/**
41 * 'captions': the video's captions only; 'audio': Whisper on the audio, even
42 * when there are captions; 'auto': the captions, else Whisper when the helper
43 * has it set up.
44 */
45export type TranscriptSource = 'captions' | 'audio' | 'auto';
46
47/**
48 * A YouTube video's transcript. Captions come whole; Whisper transcribes only
49 * `range` when given (unless the whole video's is already cached), so cut the
50 * result with segmentsInRange either way. Throws with a message for the user.
51 */
52export async function fetchTranscript(
53 plugin: RNPlugin,
54 videoIdOrUrl: string,
55 { source = 'auto', range }: { source?: TranscriptSource; range?: { start: number; end: number } } = {}
56): Promise<Transcript> {
57 // A local video (local-video:// URL) has no captions: the helper always
58 // transcribes its audio.
59 const file = parseLocalVideoUrl(videoIdOrUrl);
60 const videoId = file ? file.path : extractVideoId(videoIdOrUrl);
61 if (!videoId) throw new Error(`Not a YouTube video: ${videoIdOrUrl}`);
62 const transcript = await callVideoHelper<Transcript & { cached: boolean }>(plugin, '/video-transcript', {
63 ...(file ? { file } : { videoId }),
64 source,
65 ...(range ? { start: range.start, end: range.end } : {}),
66 });
67 console.log(
68 `[YT-Transcript] ${videoId}: ${transcript.segments.length} lines, ${transcript.language} (${transcript.kind})` +

Callers 1

getTranscriptForRangeFunction · 0.85

Calls 1

extractVideoIdFunction · 0.85

Tested by

no test coverage detected