Docs/Skills and code/Top-level code

test-transcript-fetch.js

Code47 lines2.0 KBTop-level code

What the file says about itself

Transcript fetch test (no official YouTube API). Run: node test-transcript-fetch.js - Source video is hardcoded: HinExp's most-viewed video in data/indian-business-ecosystem.db (looked up once, 1,576,818 views).

#!/usr/bin/env node
/**
 * Transcript fetch test (no official YouTube API).
 *
 * Run: node test-transcript-fetch.js
 *
 * - Source video is hardcoded: HinExp's most-viewed video in
 *   data/indian-business-ecosystem.db (looked up once, 1,576,818 views).
 * - Fetches the transcript in the video's ORIGINAL language (see src/transcript-fetch.js).
 * - Writes transcript to transcripts/<videoId>.txt and .json.
 */

import fs from "node:fs";
import path from "node:path";
import { fetchOriginalTranscript, formatTranscriptText } from "./src/transcript-fetch.js";

const SOURCE = {
  videoId: "z4cw6TJ4zmM",
  title: "The Economics of Owning a Bus Business | Business Case Study",
  channel: "HinExp (@HinExpofficial)",
  viewCount: 1576818
};

const OUT_DIR = path.resolve(process.cwd(), "transcripts");

console.log(`Source: ${SOURCE.title}\n  ${SOURCE.channel} | ${SOURCE.viewCount.toLocaleString()} views | ${SOURCE.videoId}`);

const result = await fetchOriginalTranscript(SOURCE.videoId).catch(error => {
  console.error("\nTranscript fetch failed:", error.message);
  process.exit(1);
});

const last = result.segments.at(-1);
const flat = result.segments.map(s => s.text).join(" ").replace(/\s+/g, " ").trim();

fs.mkdirSync(OUT_DIR, { recursive: true });
const base = path.join(OUT_DIR, SOURCE.videoId);
fs.writeFileSync(`${base}.txt`, formatTranscriptText(result.segments) + "\n", "utf8");
fs.writeFileSync(`${base}.json`, JSON.stringify({ source: SOURCE, ...result }, null, 2), "utf8");

console.log(`\nOK via ${result.library}`);
console.log(`  language: ${result.languageName} (${result.lang}), detected by: ${result.detection}`);
console.log(`  tracks on video: ${result.availableTracks.join(", ")}`);
console.log(`  segments: ${result.segments.length}, words: ${flat.split(" ").length}, covers ~${Math.round(last.start + last.duration)}s`);
console.log(`  saved: ${base}.txt\n         ${base}.json`);
console.log(`\nPreview: ${flat.slice(0, 200)}...`);

Shown as it is in the repository at build time (2026-11-03); the home directory appears as <home>. If this page and the file differ, the file is right. Back to the catalogue.