Most transcript code only reads the caption track a video already has. That works for English talks on YouTube, but it falls over quickly once your users paste anything else. A Spanish interview with no captions comes back empty. A Japanese video whose only subtitles are YouTube's auto-translated English track comes back in English, which isn't what was said.

There are two honest ways to deal with it.

Run speech-to-text yourself. Download the audio and run an open-source model like Whisper over it. It's free and handles most languages well. The cost is everything around it: you need the audio file first (which is the part that breaks once your code runs on a server and the site starts blocking it), a GPU or a lot of patience, and something to detect the language when you don't know it up front.

Use one API call that does both. I built Importly for this. You send the link, it uses the caption track in the language you want when there is one, and transcribes the audio when there isn't. Same request and same response shape for every language.

The script

Node 18+, no dependencies:

const url = process.argv[2] const API = 'https://api.importly.io' const headers = { Authorization: `Bearer ${process.env.IMPORTLY_API_KEY}`, 'Content-Type': 'application/json', } const fail = (message) => { console.error(message) process.exit(1) } // Leave `language` out and the spoken language is detected; // pass a code such as `language: 'es'` when you already know it. const start = await fetch(`${API}/transcribe`, { method: 'POST', headers, body: JSON.stringify({ url, maxCostDollars: 1.0, // caps the charge if the video has no captions }), }) const started = await start.json() if (!started.success) fail(`${started.code}: ${started.error}`) const { jobId } = started.data let result while (true) { const res = await fetch(`${API}/transcribe/status?id=${jobId}`, { headers }) const { data } = await res.json() if (data.status === 'completed') { result = data.result break } if (data.status === 'failed') fail(`${data.code}: ${data.message}`) await new Promise((resolve) => setTimeout(resolve, 5000)) } console.log(`Language: ${result.language} (from ${result.source})`) console.log(result.text) 
IMPORTLY_API_KEY=your_api_key node transcribe-any-language.mjs "https://www.youtube.com/watch?v=jNQXAC9IVRw" 
Language: en (from platform_captions) All right, so here we are, in front of the elephants the cool thing about these guys is that they have really... 

That video has English captions, so they were used. On a video with no captions in its language, source comes back as importly, meaning the audio was transcribed, and language is the language that was detected.

Things that bite

Speech-to-text transcribes, it doesn't translate. Only pass language when it's the language actually spoken. Ask for Japanese on a Spanish video and you get a garbled transcript labelled ja. When you don't know, leave it out and read result.language.

Short clips and music need a hint. Detection needs some speech to go on. For a few seconds of audio, or a track that's mostly music, pass language if you know it.

Regional codes. pt-BR and en-GB pick that caption track when the video has one. Speech-to-text uses the base language, so you get pt or en back.

Cost. Existing captions are $0.003 a video. Transcribing the audio is $0.01 per started minute, and maxCostDollars stops a long video from running over what you expected. It's free to try, no card required.

If you also need the file

If your app keeps the video or audio too, the same thing works inside an import: POST /import with includeTranscript: true and transcriptLanguage, and one job gives you the file and its transcript.

The full recipe, with a Python version and the list of 99 supported languages, is in the docs: https://www.importly.io/docs/recipes/transcribe-any-language