DEV Community

Cover image for How To Transcribe YouTube Videos From Your Terminal
Kevin Lewis for Deepgram

Posted on • Originally published at developers.deepgram.com

How To Transcribe YouTube Videos From Your Terminal

In our internal Deepgram Slack workspace, there's a channel where folks can share fun and wacky things they've achieved on the terminal (#bash-hall-of-fame). Over five years ago, our CEO Scott shared a nice little snippet that allows you to download just the audio from a YouTube video. Today, I'm going to take that still-functional piece of code and show you how to download audio from a YouTube video and then transcribe it with Deepgram's Speech Recognition API.

The steps are remarkably similar to our Transcribing YouTube Videos with Node.js post, but entirely on the terminal.

You will need to download youtube-dl, ffmpeg, and jq for this tutorial to work. If you use macOS and have homebrew installed, this is brew install youtube-dl, brew install ffmpeg, and brew install jq. You will also need a Deepgram API Key - get one here.

Download Audio From YouTube Video with youtube_dl

We'll use the following YouTube ID: 9NZDwZbyDus. Starting with Scott's original snippet:

youtube-dl 9NZDwZbyDus --extract-audio --audio-format wav -o 9NZDwZbyDus.wav
Enter fullscreen mode Exit fullscreen mode

Given that we use the same value twice, let's abstract the video ID into a variable:

VIDEO_ID=9NZDwZbyDus; youtube-dl $VIDEO_ID --extract-audio --audio-format wav -o $VIDEO_ID.wav
Enter fullscreen mode Exit fullscreen mode

Transcribe With Deepgram

Now that we have a local file and know its file format, we can use cURL to get a transcript from Deepgram:

curl https://api.deepgram.com/v1/listen?punctuate=true -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" -H "Content-Type: audio/wav" --data-binary @${VIDEO_ID}.wav
Enter fullscreen mode Exit fullscreen mode

Using jq to extract just the transcript text and saving that to a file:

curl https://api.deepgram.com/v1/listen?punctuate=true -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" -H "Content-Type: audio/wav" --data-binary @${VIDEO_ID}.wav | jq '.results.channels[0].alternatives[0].transcript' > "$VIDEO_ID.txt"
Enter fullscreen mode Exit fullscreen mode

Delete Audio File

Finally, if you no longer require the audio file, delete it:

rm $VIDEO_ID.wav
Enter fullscreen mode Exit fullscreen mode

Bringing It All Together

When we first introduced a variable to this script, we separated the declaration and the cURL command with a semicolon. We can do exactly the same with all subsequent steps. The one-liner for this project is:

VIDEO_ID=EmIhbFeJgiE; youtube-dl ${VIDEO_ID} --extract-audio --audio-format wav -o ${VIDEO_ID}.wav; curl https://api.deepgram.com/v1/listen\?punctuate\=true -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" -H "Content-Type: audio/wav" --data-binary @${VIDEO_ID}.wav | jq '.results.channels[0].alternatives[0].transcript' > "$VIDEO_ID.txt"; rm "$VIDEO_ID.wav"
Enter fullscreen mode Exit fullscreen mode

If you have any questions, please let us know - we love to help!

Top comments (0)