Python SDK לתמלולי YouTube: החבילה הרשמית ב-PyPI
חלצו תמלולי YouTube באפליקציות ה-Python שלכם. מתקינים מ-PyPI, מתחברים עם מפתח ה-API ומשלבים חילוץ תמלולים בצינורות הנתונים, בכלי המחקר ובסקריפטי האוטומציה שלכם.
התקנה
pip install youtubetranscriptdevapi
התחלה מהירה
from youtubetranscript import YouTubeTranscript
yt = YouTubeTranscript("your_api_key")
result = yt.transcribe("dQw4w9WgXcQ")
print(f"Segments: {len(result.segments)}")
print(f"Duration: {result.duration:.0f}s")
print(f"Words: {result.word_count}")
for seg in result.segments[:5]:
print(f"[{seg.start_formatted}] {seg.text}")תכונות
# Translate to any language
result = yt.transcribe("dQw4w9WgXcQ", language="es")
# Choose caption source
result = yt.transcribe("dQw4w9WgXcQ", source="manual")
# Format options
result = yt.transcribe("dQw4w9WgXcQ", format={"timestamp": True, "words": True})batch = yt.batch(["video1", "video2", "video3"])
for t in batch.completed:
print(f"{t.video_id}: {t.word_count} words")job = yt.transcribe_asr("video_without_captions")
result = yt.wait_for_job(job.job_id) # polls until complete
print(result.text)print(result.to_srt()) # SRT subtitles print(result.to_vtt()) # WebVTT subtitles print(result.to_plain_text()) # Plain text print(result.to_timestamped_text()) # Text with timestamps
# Search within transcript
matches = result.search("keyword")
# Account stats
stats = yt.stats()
print(f"Credits: {stats.credits_remaining}")
# History
history = yt.list_transcripts(search="python tutorial", limit=5)לקוח אסינכרוני
import asyncio
from youtubetranscript import AsyncYouTubeTranscript
async def main():
async with AsyncYouTubeTranscript("your_api_key") as yt:
# Single
result = await yt.transcribe("dQw4w9WgXcQ")
# Concurrent
results = await asyncio.gather(
yt.transcribe("video1"),
yt.transcribe("video2"),
yt.transcribe("video3"),
)
asyncio.run(main())טיפול בשגיאות
from youtubetranscript import YouTubeTranscript
from youtubetranscript.exceptions import (
NoCaptionsError,
AuthenticationError,
InsufficientCreditsError,
RateLimitError,
)
yt = YouTubeTranscript("your_api_key")
try:
result = yt.transcribe("some_video")
except NoCaptionsError:
job = yt.transcribe_asr("some_video")
result = yt.wait_for_job(job.job_id)
except AuthenticationError:
print("Check your API key")
except InsufficientCreditsError:
print("Top up at youtubetranscript.dev/pricing")
except RateLimitError as e:
print(f"Rate limited: retry after {e.retry_after}s")מדריך ה-API
אובייקט Transcript
אובייקט Segment
עלויות קרדיטים
מול YOUTUBE-TRANSCRIPT-API (קוד פתוח)
ייתכן שנתקלתם בחבילת הקוד הפתוח הפופולרית youtube-transcript-api. כך החבילה שלנו שונה ממנה:
SDKs וכלים נוספים
שאלות נפוצות
אילו גרסאות Python נתמכות?+
Python 3.8 ומעלה. אנחנו ממליצים על Python 3.10 ומעלה לחוויה הטובה ביותר עם type hints.
במה זה שונה מ-youtube-transcript-api?+
ה-SDK שלנו הוא החבילה הרשמית של YouTubeTranscript.dev. הוא כולל תמלול מבוסס AI לסרטונים בלי כתוביות, חילוץ בצד השרת שמונע חסימת IP, הגבלת קצב מובנית ותמיכה מקצועית. חלופת הקוד הפתוח רק מחלצת כתוביות קיימות ורצה בצד הלקוח.
אפשר להשתמש בו ב-Jupyter notebooks?+
כן. ה-API הסינכרוני עובד מצוין ב-Jupyter notebooks וב-Google Colab. לעיבוד מקבילי בסביבות notebook, השתמשו ב-API האסינכרוני.
האם הוא עובד עם Django ו-Flask?+
כן. ה-SDK לא תלוי בפריימוורק ועובד עם כל פריימוורק ווב של Python. עם פריימוורקים אסינכרוניים כמו FastAPI, השתמשו בממשק האסינכרוני.