Elixir client for the YouTubeTranscript.net API: get YouTube transcripts with timestamps, download them as TXT, SRT or VTT, and transcribe whole playlists and channels. No runtime dependencies (:httpc and the built-in JSON), Elixir 1.18+.
Install
def deps do
[{:youtubetranscript_net, "~> 0.1"}]
endQuick start
Create an API key at https://youtubetranscript.net/account/api-keys and set it as YTT_API_KEY.
alias YouTubeTranscriptNet.Transcripts
client = YouTubeTranscriptNet.new!() # reads YTT_API_KEY
{:ok, result} = Transcripts.get(client, "https://www.youtube.com/watch?v=jNQXAC9IVRw")
for segment <- result.transcript.segments do
IO.puts("#{segment.start} #{segment.text}")
endget waits for the transcript: it asks for a fast answer, and when the video needs more time (speech-to-text, long videos) it polls the job until it is done.
Every call returns {:ok, value} or {:error, %YouTubeTranscriptNet.Error{}}.
Client options
YouTubeTranscriptNet.new(
api_key: "ytt_...", # default YTT_API_KEY
base_url: "https://api.youtubetranscript.net", # default YTT_BASE_URL, then this
timeout: 60_000, # ms per HTTP request
max_retries: 2 # automatic retries, see below
)new/1 returns {:error, %Error{code: "API_KEY_REQUIRED"}} when there is no key; new!/1 raises it. Pass transport: to use your own HTTP client (see YouTubeTranscriptNet.Transport).
Functions
video is a YouTube link (watch, youtu.be, Shorts) or an 11-character video id. Times are milliseconds.
YouTubeTranscriptNet.Transcripts
| Function | Returns |
|---|---|
get(client, video, language:, format:, include_timestamp:, send_metadata:, confirm_credits:, timeout: 300_000, poll_interval: 2_000) | %TranscriptResult{} once ready |
submit(client, video, ..., wait_ms:) | the first answer: %TranscriptResult{} or %Job{} |
job(client, job_id) | %TranscriptResult{} or %Job{} |
info(client, video) | video details and caption languages (free) |
list(client, q:, lang:, sort:, page:, page_size:) | %{"transcripts", "total", "page", "pageSize", "langs"} |
download(client, video_id, format: "srt", language:) | the file as a string (txt, srt, vtt, json, md) |
delete(client, video_id, language:) | %{"removed" => true} |
language is a priority list such as "en", "pt-BR,pt", "auto" (the video's own captions) or "asr" (speech-to-text when there are no captions). format: "text" returns plain text in result.text.
%TranscriptResult{} has status, job_id, language, format, cached, length_seconds, length_text, transcript (platform, external_id, lang, origin, title, channel, duration_sec, segments), text, metadata and raw (the full JSON).
YouTubeTranscriptNet.Batches
alias YouTubeTranscriptNet.Batches
{:ok, batch} = Batches.create(client, playlist: "https://www.youtube.com/playlist?list=...")
{:ok, _done} = Batches.wait(client, batch["id"])
{:ok, zip} = Batches.export(client, batch["id"], format: "zip", files: "srt")
File.write!("transcripts.zip", zip)| Function | Returns |
|---|---|
create(client, items: [...] | playlist: | channel:, language:, limit:, name:) | the batch |
get(client, batch_id, max_results:, page_token:) | %{"batch", "items", "nextPageToken"} |
wait(client, batch_id, timeout: 1_800_000, poll_interval: 3_000) | the last get once completed or failed |
export(client, batch_id, format: "zip", files:, timestamps:) | file bytes (zip, csv, txt, json, srt, vtt) |
retry(client, batch_id) | %{"batch", "retried", "credits"} |
delete(client, batch_id) | %{"removed" => true} |
Batches are available once your account has bought a credit pack.
YouTubeTranscriptNet.Account
Account.usage(client) returns your credits ("balance", "freeToday", "available") and limits.
Errors
case Transcripts.get(client, video, language: "de") do
{:ok, result} ->
result
{:error, %YouTubeTranscriptNet.Error{} = e} ->
e.code # "LANGUAGE_NOT_AVAILABLE", "INSUFFICIENT_CREDITS", "RATE_LIMITED", ...
e.status # HTTP status, 0 for local errors
e.message
e.request_id # X-Request-Id, include it when you contact support
e.retry_after # seconds, on 429
e.details # the server's error object, e.g. e.details["availableLanguages"]
e.retryable # true for 429, 5xx and network errors
endA video that costs more than 10 credits returns CREDITS_CONFIRMATION_REQUIRED first; call again with confirm_credits: true to accept the cost.
Retries
GET and DELETE requests, and Transcripts.get / submit (sent with an Idempotency-Key, so the server never runs or charges them twice), are retried up to max_retries times on 429, 5xx and network errors, waiting for Retry-After when the server sends it. Batches.create and Batches.retry are never retried automatically.
Development
mix test # unit tests, no network
YTT_API_KEY=ytt_... mix run examples/e2e.exs # end-to-end against the API