MCPcopy Create free account
hub / github.com/SamuelZ12/longcut / handler

Function handler

app/api/transcript/route.ts:30–232  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

28}
29
30async function handler(request: NextRequest) {
31 try {
32 const { url, lang, expectedDuration } = await request.json();
33
34 if (!url) {
35 return respondWithNoCredits({ error: 'YouTube URL is required' }, 400);
36 }
37
38 const videoId = extractVideoId(url);
39
40 if (!videoId) {
41 return respondWithNoCredits({ error: 'Invalid YouTube URL' }, 400);
42 }
43
44 if (shouldUseMockData()) {
45 console.log(
46 '[TRANSCRIPT] Using mock data (NEXT_PUBLIC_USE_MOCK_DATA=true)'
47 );
48 const mockData = getMockTranscript();
49
50 const rawSegments = mockData.content.map((item: any) => ({
51 text: item.text,
52 start: item.offset / 1000, // Convert milliseconds to seconds
53 duration: item.duration / 1000 // Convert milliseconds to seconds
54 }));
55
56 // Merge segments into complete sentences for better translation
57 const mergedSentences = mergeTranscriptSegmentsIntoSentences(rawSegments);
58 const transformedTranscript = mergedSentences.map((sentence) => ({
59 text: sentence.text,
60 start: sentence.segments[0].start, // Use first segment's start time
61 duration: sentence.segments.reduce((sum, seg) => sum + seg.duration, 0) // Sum all durations
62 }));
63
64 const transcriptDuration = rawSegments.length > 0
65 ? rawSegments[rawSegments.length - 1].start + rawSegments[rawSegments.length - 1].duration
66 : 0;
67
68 return NextResponse.json({
69 videoId,
70 transcript: transformedTranscript,
71 language: mockData.lang || 'en',
72 availableLanguages: mockData.availableLangs || ['en'],
73 transcriptDuration: Math.round(transcriptDuration),
74 segmentCount: transformedTranscript.length,
75 rawSegmentCount: rawSegments.length,
76 isPartial: false,
77 coverageRatio: undefined,
78 });
79 }
80
81 // ── Strategy: Try YouTube direct first (free), fall back to Supadata (paid) ──
82 // YouTube's InnerTube API is free but gets blocked from datacenter IPs.
83 // Supadata is a paid API that handles YouTube's bot detection for us.
84 // By trying free first, we only pay for Supadata when YouTube blocks us.
85
86 let rawSegments: { text: string; start: number; duration: number }[] | null = null;
87 let language: string | undefined;

Callers

nothing calls this directly

Calls 8

extractVideoIdFunction · 0.90
shouldUseMockDataFunction · 0.90
getMockTranscriptFunction · 0.90
fetchYouTubeTranscriptFunction · 0.90
logMethod · 0.80
respondWithNoCreditsFunction · 0.70

Tested by

no test coverage detected