All posts

How To Transcribe YouTube Videos From Your Terminal

Kevin Lewis August 22, 2022 in Tutorials

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

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

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

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"

Delete Audio File

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

rm $VIDEO_ID.wav

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"

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

If you have any feedback about this post, or anything else around Deepgram, we'd love to hear from you. Please let us know in our GitHub discussions.

More with these tags:

Share your feedback

Thank you! Can you tell us what you liked about it? (Optional)

Thank you. What could we have done better? (Optional)

We may also want to contact you with updates or questions related to your feedback and our product. If don't mind, you can optionally leave your email address along with your comments.

Thank you!

We appreciate your response.