For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
PlaygroundChangelogSign In
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
  • Getting started
    • Transcribe a pre-recorded audio file
    • Model selection
    • View model benchmarks
    • Evaluate model accuracy
    • Cloud endpoints & data residency
    • Manage concurrent requests
    • Webhooks
  • Models
    • Medical Mode
  • Features
    • Boost specific terms
    • Label speakers
    • Transcribe multiple audio channels
    • Transcribe audio with mixed languages
    • Correct spelling of terms
    • Include filler words
    • Search for words in transcript
    • Set the start and end of the transcript
  • Guides
      • Build a meeting notetaker
      • Build a medical scribe
      • Build a contact center application
        • Get YouTube Video Transcripts with yt-dlp
        • Build a UI for Transcription with Gradio and Python
        • Detect Low Confidence Words in a Transcript
LogoLogo
PlaygroundChangelogSign In
On this page
  • Quickstart
  • Step-by-step guide
  • Install Dependencies
  • Option 1: Download video via CLI
  • Option 2: Download video via code
GuidesTutorialsBasic transcription workflows

Get YouTube Video Transcripts with yt-dlp

Was this page helpful?
Previous

Build a UI for Transcription with Gradio and Python

Next
Built with

In this guide, we’ll show you how to transcribe YouTube videos.

For this, we use the yt-dlp library to download YouTube videos and then transcribe it with the AssemblyAI API.

yt-dlp is a youtube-dl fork with additional features and fixes. It is better maintained and preferred over youtube-dl nowadays.

In this guide we’ll show 2 different approaches:

  • Option 1: Download video via CLI
  • Option 2: Download video via code

Let’s get started!

Quickstart

1import assemblyai as aai
2import yt_dlp
3
4def transcribe_youtube_video(video_url: str, api_key: str) -> str:
5 """
6 Transcribe a YouTube video given its URL.
7
8 Args:
9 video_url: The YouTube video URL to transcribe
10 api_key: AssemblyAI API key
11
12 Returns:
13 The transcript text
14 """
15 # Configure yt-dlp options for audio extraction
16 ydl_opts = {
17 'format': 'm4a/bestaudio/best',
18 'outtmpl': '%(id)s.%(ext)s',
19 'postprocessors': [{
20 'key': 'FFmpegExtractAudio',
21 'preferredcodec': 'm4a',
22 }]
23 }
24
25 # Download and extract audio
26 with yt_dlp.YoutubeDL(ydl_opts) as ydl:
27 ydl.download([video_url])
28 # Get video ID from info dict
29 info = ydl.extract_info(video_url, download=False)
30 video_id = info['id']
31
32 # Configure AssemblyAI
33 aai.settings.api_key = api_key
34
35 # Transcribe the downloaded audio file
36 config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
37 transcriber = aai.Transcriber()
38 transcript = transcriber.transcribe(f"{video_id}.m4a", config)
39
40 return transcript.text
41
42transcript_text = transcribe_youtube_video("https://www.youtube.com/watch?v=wtolixa9XTg", "YOUR-API-KEY")
43print(transcript_text)

Step-by-step guide

Install Dependencies

Install yt-dlp and the AssemblyAI Python SDK via pip.

$pip install -U yt-dlp
$pip install assemblyai

Option 1: Download video via CLI

In this approach we download the YouTube video via the command line and then transcribe it via the AssemblyAI API. We use the following video here:

  • https://www.youtube.com/watch?v=wtolixa9XTg

To download it, use the yt-dlp command with the following options:

  • -f m4a/bestaudio: The format should be the best audio version in m4a format.
  • -o "%(id)s.%(ext)s": The output name should be the id followed by the extension. In this example, the video gets saved to “wtolixa9XTg.m4a”.
  • wtolixa9XTg: the id of the video.
$yt-dlp -f m4a/bestaudio -o "%(id)s.%(ext)s" https://www.youtube.com/watch?v=wtolixa9XTg

Next, set up the AssemblyAI SDK and trancribe the file. Replace YOUR_API_KEY with your own key. If you don’t have one, you can sign up here for free.

Make sure that the path you pass to the transcribe() function corresponds to the saved filename.

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"
4
5config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
6transcriber = aai.Transcriber()
7transcript = transcriber.transcribe("wtolixa9XTg.m4a", config)
8print(transcript.text)

Option 2: Download video via code

In this approach we download the video with a Python script instead of the command line.

You can download the file with the following code:

1import yt_dlp
2
3URLS = ['https://www.youtube.com/watch?v=wtolixa9XTg']
4
5ydl_opts = {
6 'format': 'm4a/bestaudio/best', # The best audio version in m4a format
7 'outtmpl': '%(id)s.%(ext)s', # The output name should be the id followed by the extension
8 'postprocessors': [{ # Extract audio using ffmpeg
9 'key': 'FFmpegExtractAudio',
10 'preferredcodec': 'm4a',
11 }]
12}
13
14
15with yt_dlp.YoutubeDL(ydl_opts) as ydl:
16 error_code = ydl.download(URLS)

After downloading, you can use the same code from option 1 to transcribe the file:

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"
4
5config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
6transcriber = aai.Transcriber()
7transcript = transcriber.transcribe("wtolixa9XTg.m4a", config)