חבילת NPM לתמלולי YouTube: SDK ל-JavaScript ול-TypeScript
חלצו תמלולי YouTube באופן תכנותי באפליקציות ה-Node.js שלכם. מתקינים מ-NPM, מתחברים עם מפתח ה-API ומתחילים לתמלל סרטונים תוך דקות.
התקנה
npm install youtube-transcript-api pnpm add youtube-transcript-api yarn add youtube-transcript-api
התחלה מהירה
import { YouTubeTranscript } from "youtube-transcript-api";
const yt = new YouTubeTranscript({ apiKey: "your_api_key" });
const result = await yt.getTranscript("dQw4w9WgXcQ");
console.log(result.data?.transcript.text);תכונות
כיסוי מלא של V2 API
תמלול, עיבוד קבוצתי, משימות, polling: כל ה-endpoints עטופים.
TypeScript קודם כול
הגדרות טיפוסים מלאות עם תמיכה ב-IntelliSense.
אפס תלויות
משתמש ב-fetch המובנה. Node 18 ומעלה בלבד, בלי משקל עודף.
שגיאות עם טיפוסים
מחלקות שגיאה מפורטות לכל קוד שגיאה של ה-API.
Polling מובנה
פונקציית העזר waitForJob לתמלול ASR אסינכרוני.
ESM ו-CommonJS
עובד בשתי מערכות המודולים מהקופסה.
תמלול עם אפשרויות
const result = await yt.transcribe({
video: "dQw4w9WgXcQ",
language: "fr",
source: "manual",
format: {
timestamp: true,
paragraphs: true,
words: false,
},
});
console.log(result.status); // "completed"
console.log(result.data?.transcript.text); // Full transcript text
console.log(result.data?.transcript.language); // "fr"
console.log(result.data?.transcript.segments); // Timestamped segments
console.log(result.credits_used); // Credits consumedעיבוד קבוצתי (עד 100 סרטונים)
const result = await yt.batch({
video_ids: ["dQw4w9WgXcQ", "jNQXAC9IVRw", "9bZkp7q19f0"],
format: { timestamp: true },
});
console.log(result.summary);
// { total: 3, succeeded: 3, failed: 0, processing: 0 }
for (const item of result.results) {
console.log(`${item.data?.video_id}: ${item.data?.transcript.text.slice(0, 100)}...`);
}תמלול שמע ב-ASR
בסרטונים בלי כתוביות, השתמשו ב-ASR כדי לתמלל ישירות מהשמע. תומך בקריאות webhook או ב-polling מובנה.
const result = await yt.transcribe({
video: "VIDEO_ID",
source: "asr",
allow_asr: true,
webhook_url: "https://yoursite.com/webhook",
});const result = await yt.transcribe({
video: "VIDEO_ID",
source: "asr",
allow_asr: true,
});
if (result.job_id) {
const final = await yt.waitForJob(result.job_id, {
interval: 5000,
maxAttempts: 60,
});
console.log(final.data?.transcript.text);
}const result = await yt.transcribe({
video: "VIDEO_WITHOUT_CAPTIONS",
source: "asr",
// allow_asr not set: V2 requires explicit confirmation
});
if (result.status === "requires_asr_confirmation") {
console.log(result.estimated_credits); // e.g. 5
console.log(result.duration_minutes); // e.g. 7.5
// User confirms → retry with allow_asr
const confirmed = await yt.transcribe({
video: "VIDEO_WITHOUT_CAPTIONS",
source: "asr",
allow_asr: true,
});
}טיפול בשגיאות
import {
YouTubeTranscript,
InvalidRequestError,
AuthenticationError,
InsufficientCreditsError,
NoCaptionsError,
RateLimitError,
} from "youtube-transcript-api";
try {
await yt.getTranscript("invalid");
} catch (error) {
if (error instanceof AuthenticationError) {
console.log("Bad API key");
} else if (error instanceof InsufficientCreditsError) {
console.log("Top up at https://youtubetranscript.dev/pricing");
} else if (error instanceof NoCaptionsError) {
console.log("No captions: try source: 'asr' with allow_asr: true");
} else if (error instanceof RateLimitError) {
console.log(`Rate limited. Retry after ${error.retryAfter}s`);
}
}הגדרות
const yt = new YouTubeTranscript({
apiKey: "your_api_key",
baseUrl: "https://...", // Override API base URL
timeout: 60_000, // Request timeout in ms (default: 30s)
});נדרש Node.js 18 ומעלה (משתמש ב-fetch המובנה). את מפתח ה-API אפשר לקבל מתוך youtubetranscript.dev/dashboard/account.
SDKs וכלים נוספים
מתי להשתמש במה
חבילת NPM
הכי מתאימה לאפליקציות Node.js, לרינדור בצד השרת, לכלי CLI ולסקריפטים אוטומטיים שבהם צריך גישה תכנותית.
REST API
הכי מתאים לסביבות שאינן JavaScript, לבקשות חד-פעמיות, וכשצריך גישת HTTP שלא תלויה בשפה.
שרת MCP
הכי מתאים לעוזרי קוד מבוססי AI כמו Claude, Cursor ו-VS Code Copilot, כשרוצים אינטראקציה בשפה טבעית.
שאלות נפוצות
האם השימוש בחבילת ה-NPM חינמי?+
ההתקנה של החבילה עצמה חינמית. כדי לשלוח בקשות צריך מפתח API של YouTubeTranscript.dev. מפתחות API זמינים בכל המסלולים בתשלום.
האם היא עובדת בדפדפן?+
החבילה מיועדת לשימוש בצד השרת ב-Node.js. לחילוץ מתוך הדפדפן, השתמשו ישירות ב-REST API שלנו עם fetch או axios.
אילו גרסאות Node.js נתמכות?+
Node.js 18 ומעלה. החבילה משתמשת ב-fetch המובנה ובתכונות מודרניות של JavaScript.
אפשר להשתמש בה עם Deno או Bun?+
כן, החבילה תואמת לסביבות הריצה Deno ו-Bun שתומכות בחבילות NPM.