> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Transcribe a video in any language from a URL

> Send a video or audio URL in Spanish, Japanese, Arabic or any of 99 languages and get the transcript back, even when the video has no captions in that language. Node and Python.

Most transcript tools only read a video's existing caption track, and often only the English one. A Spanish talk with no captions comes back empty, and a Japanese interview whose only subtitles are an auto-translated English track comes back as English, not what was said. Importly takes the same URL for every language: it uses the caption track in the language you ask for when the video has one, and transcribes the audio itself when it doesn't.

This script transcribes a link in whatever language the media is in, prints which language that was, and shows how to force a specific one. It works on any public video or audio URL and needs no dependencies in Node, and `requests` in Python.

## The code

```js transcribe-any-language.mjs theme={null}
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)
}

// Start the transcription. 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, // cap the charge if the video has no captions and runs speech-to-text
  }),
})
const started = await start.json()
if (!started.success) fail(`${started.code}: ${started.error}`)

// Collect the result by polling (a webhook works too).
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))
}

// `language` is the code of the transcript you got back; `source` says whether
// it came from the video's own captions or from speech-to-text.
console.log(`Language: ${result.language} (from ${result.source})`)
console.log(result.text)
```

```python transcribe_any_language.py theme={null}
import os
import sys
import time

import requests

url = sys.argv[1]
API = "https://api.importly.io"
headers = {"Authorization": f"Bearer {os.environ['IMPORTLY_API_KEY']}"}


def fail(message):
    raise SystemExit(message)


# Start the transcription. Leave `language` out and the spoken language is
# detected; pass a code such as "language": "es" when you already know it.
started = requests.post(
    f"{API}/transcribe",
    headers=headers,
    json={
        "url": url,
        "maxCostDollars": 1.0,  # cap the charge if the video has no captions and runs speech-to-text
    },
).json()
if not started["success"]:
    fail(f"{started['code']}: {started['error']}")

# Collect the result by polling (a webhook works too).
job_id = started["data"]["jobId"]
while True:
    data = requests.get(
        f"{API}/transcribe/status", headers=headers, params={"id": job_id}
    ).json()["data"]
    if data["status"] == "completed":
        result = data["result"]
        break
    if data["status"] == "failed":
        fail(f"{data['code']}: {data['message']}")
    time.sleep(5)

# `language` is the code of the transcript you got back; `source` says whether
# it came from the video's own captions or from speech-to-text.
print(f"Language: {result['language']} (from {result['source']})")
print(result["text"])
```

Run it on a public video in any language:

```bash theme={null}
# Node
node transcribe-any-language.mjs "https://www.youtube.com/watch?v=VIDEO_ID"

# Python
python transcribe_any_language.py "https://www.youtube.com/watch?v=VIDEO_ID"
```

## Forcing a language

Leave `language` out to have the spoken language detected. Pass a code when you know it, or when you want a specific caption track on a multilingual video:

```json theme={null}
{ "url": "https://www.youtube.com/watch?v=VIDEO_ID", "language": "ja" }
```

Regional codes work too: `pt-BR` and `en-GB` match that caption track, and speech-to-text transcribes in the base language, so `language` comes back as `pt` or `en`. See the full list of [99 supported languages](/docs/features/transcripts.md#supported-languages).

## What comes back

The response is the same shape for every language, whether the words came from captions or from speech-to-text:

| Field | What it holds |
| - | - |
| `text` | The whole transcript as plain text |
| `language` | The code of the transcript you got: the one you sent, or the one detected when you sent none |
| `source` | `platform_captions` if it used the video's caption track, `importly` if it transcribed the audio |

## Notes that save you a round trip

When the video has captions in the language you want, you get them for $0.003. When it doesn't, Importly transcribes the audio in that language at $0.01 per started minute, so a foreign-language video with no subtitles still returns a transcript instead of nothing. `maxCostDollars` caps that charge: a long video over the cap fails with `MAX_COST_EXCEEDED` rather than running over it.

Speech-to-text transcribes, it doesn't translate. Send `language` only when it's the language actually spoken; asking for a different language than the audio gives a garbled transcript labelled with the wrong code. When you don't know the language, leave it out and read it off `result.language`. Detection needs some speech to go on, so on a clip of a few seconds or one that's mostly music, pass `language` when you can.

Need the media file as well as the transcript? Add `includeTranscript: true` and `transcriptLanguage` to [`POST /import`](/docs/endpoints/import) and one job gives you the file and its transcript. See [transcripts inside an import](/docs/features/transcripts.md#transcripts-inside-an-import).

> [card] [Full reference: languages](/docs/features/transcripts.md#languages)
>
> How `language` behaves on captions and speech-to-text, and the 99 supported languages.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.