Generating and Saving Transcripts From the Terminal

It can be helpful to know how to see and save Deepgram transcripts directly in your terminal. Learn how to use bash commands and scripts to execute directly in your terminal.

Before You Start

Before you run the code, you'll need to do a few things.

Create a Deepgram Account

Before you can use Deepgram products, you'll need to create a Deepgram account. Signup is free and includes:

Create a Deepgram API Key

To access Deepgram’s API, you'll need to create a Deepgram API Key. Make note of your API Key; you will need it later.

Install JQ

In this guide, we use jq, which is an excellent command-line utility that allows you to display and manipulate JSON data, so you will also need to install jq.

Making a curl Request

Open your terminal, and run the following curl command:

curl \
  --request POST \
  --header 'Authorization: Token YOUR_DEEPGRAM_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"url":"https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"}' \
  --url 'https://api.deepgram.com/v1/listen?punctuate=true'

⚠️

Be sure to replace the placeholder YOUR_DEEPGRAM_API_KEY with the Deepgram API Key you created earlier in this tutorial.

Let’s look more closely at this request:

  • --request POST: An HTTP request with the POST method.
  • --header 'Authorization: Token YOUR_DEEPGRAM_API_KEY': Includes authorization details and links the request with your Deepgram account and Project.
  • --header 'Content-Type: application/json': JSON data that will be sent with this request.
  • --data '{"url":"<https://static.deepgram.com/examples/nasa-spacewalk-interview.wav>"}': JSON data sent to Deepgram (an object containing one URL parameter).
  • --url 'https://api.deepgram.com/v1/listen?punctuate=true': URL to which the request should be made (Deepgram's endpoint). punctuate=true enables the punctuation feature.
  • \: Allows you to break one command over several lines for readability.

When you run this command and it is finished processing (often after only a few seconds), you’ll receive a JSON response that looks similar to the following:

This may look daunting, so let's look at a way to make both our curl request and our response easier to read.

Shortening Your curl Request

Now that you've seen a full curl request and understand the required parameters, let's look at a more concise way to make the same request:

curl https://api.deepgram.com/v1/listen?punctuate=true \
     -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"url":"https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"}'

In this example, notice:

  • The URL comes immediately after the curl command.
  • No HTTP method is present. Normally, this would default to a GET request, but because this request has a body, curl assumes a POST request.
  • --header is shortened to -H
  • --data is shortened to -d.

Formatting Your JSON Response

To make the JSON response more readable, you can use jq, which is an excellent command-line utility that allows you to display and manipulate JSON data.

In the terminal, a pipe (|) is often used to send the output of one command as an input for a second command. jq expects some JSON as input, plus an expression to describe how to display it.

The following jq expression will extract just the transcript from the returned data object:

| jq '.results.channels[0].alternatives[0].transcript'

To use it, add it to the end of your curl request:

curl https://api.deepgram.com/v1/listen?punctuate=true \
  -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"}' \
  | jq '.results.channels[0].alternatives[0].transcript'

⚠️

Be sure to replace the placeholder YOUR_DEEPGRAM_API_KEY with the Deepgram API Key you created earlier in this tutorial.

When you run this command and it is finished processing (often after only a few seconds), you’ll receive a JSON response that looks similar to the following:

Saving Output to a File

Once you have the transcript extracted and formatted using jq, you can redirect the output to a new file by appending > output.txt to any command that prints to the terminal. For example:

curl https://api.deepgram.com/v1/listen?punctuate=true \
  -H "Authorization: Token YOUR_DEEPGRAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"}' \
  | jq '.results.channels[0].alternatives[0].transcript'
  > output.txt

Processing Multiple Files

To process multiple files, you can create .sh files containing multiple lines of bash script to execute from your terminal.

Create a new file called transcripts.sh, and open it in a code editor. Populate it with the following:

#!/bin/bash

urls=("https://static.deepgram.com/examples/TrumpDemocratsMeeting.nancyshort.wav" "https://static.deepgram.com/examples/nasa-spacewalk-interview.wav" "https://static.deepgram.com/examples/deep-learning-podcast-clip.wav")

dg_features="punctuate=true&utterances=true&diarize=true&tier=enhanced"
dg_key="YOUR_DEEPGRAM_API_KEY"

for url in ${urls[@]}; do
  filename=${url##*/}

  RESPONSE=$(
    curl -X POST https://api.deepgram.com/v1/listen?$dg_features \
         -H "Authorization: Token $dg_key" \
         -H "Content-Type: application/json" \
         -d "{\"url\":\"$url\"}"
  )

  echo $RESPONSE | jq '.results.channels[0].alternatives[0].transcript' > $filename.txt
done

⚠️

Be sure to replace the placeholder YOUR_DEEPGRAM_API_KEY with the Deepgram API Key you created earlier in this tutorial.

In this file, notice:

  • The first line (#!/bin/bash) is a shebang and specifies which program should be called to run the script. In this case, bash.
  • urls is a variable containing an array with three URLs. The arrays use parentheses, and items are separated by only one space.
  • dg_features and dg_key are variables you should alter for your exact use case. Note that the request, as written, uses Deepgram's punctuation, utterances, diarize, and tier features included in the dg_features variable.
  • Inside the for loop:
    • filename extracts the last part of the URL (the filename), which will later be used to name the output file.
    • The curl command is the same as before, but with variables interpolated. The output is stored in a new variable called RESPONSE.
    • RESPONSE is sent to jq, and then redirected into a new text file.

Run the file in your terminal:

./transcripts.sh

Experimenting with jq

jq is a remarkable powerful tool that you can use to format data in more advanced ways. For example, the following expression will loop through the results.utterances array and format a string for each item, interpolating the speaker identifier and transcript text:

echo $RESPONSE | jq -r '.results.utterances[] | "[Speaker:\(.speaker)] \(.transcript)"' > $filename.txt

When you run a previous curl command with this jq appended and it is finished processing, you’ll receive a JSON response formatted in the following way:

[Speaker:0] agreement on other things that are really good. Nancy, would you like to say something?
[Speaker:1] Well, thank you, mister president for the opportunity to meet with you so that we can work together in a bipartisan way
[Speaker:1] to meet the needs of the American people. I think the American people recognize
[Speaker:1] that we must keep government open, that a shutdown is not worth anything.
[Speaker:1] And that you should