Search code examples
bashcurlgoogle-speech-api

Loop through multiple cURL api request body parameters using Bash script


I want to convert multiple audio files to text using Google Cloud's Speech Recognize API.

I successfully transcribed one audio file called '1.flac'...

Request:

curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESSTOKEN" \
https://speech.googleapis.com/v1/speech:recognize \
-d '
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/1.flac"}}
' 

Response:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "cat",
          "confidence": 0.9999999
        }
      ]
    }
  ]
}

I successfully generated multiple lines for the data/body portion of the above request...

Request:

for i in 1 2 3
do
echo "{\"config\": {\"languageCode\": \"pt-BR\", \"audioChannelCount\": 2},\"audio\":{\"uri\": \"gs://PROJECTID/$i.flac\"}}"
done

Response:

{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/1.flac"}}
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/2.flac"}}
{"config": {"languageCode": "pt-BR", "audioChannelCount": 2},"audio":{"uri": "gs://PROJECTID/3.flac"}}

How can I combine these two scripts, so that the curl API executes once for each of the three files, with one response like this:

{
      "results": [
        {
          "alternatives": [
            {
              "transcript": "cat",
              "confidence": 0.9999999
            }
          ]
        }
      ]
    }
{
      "results": [
        {
          "alternatives": [
            {
              "transcript": "dog",
              "confidence": 0.9999999
            }
          ]
        }
      ]
    }
{
      "results": [
        {
          "alternatives": [
            {
              "transcript": "horse",
              "confidence": 0.9999999
            }
          ]
        }
      ]
    }

Solution

  • Your code is almost right.

    for i in 1 2 3
    do
        curl -s -H "Content-Type: application/json" \
             -H "Authorization: Bearer ACCESSTOKEN" \
             -d '{"config": {"languageCode": "pt-BR", "audioChannelCount": 2}
                 ,"audio":{"uri": "gs://PROJECTID/'$i'.flac"}}' \
              https://speech.googleapis.com/v1/speech:recognize
     done
    

    I put URL at the end, because usually, options come before arguments

    The value of -d is composed of 3 parts ['...'] [$i] ['...'] chained together. This allows the expansion of [$i]