bashcurlibm-watsonspeech-to-text

How to for loop CURL commands?


I am using Watson's Speech-To-Text Lite service and I am trying to find a way to automate the loading of new audio files to transcribe. I am very new to Bash and so I'm unclear of even the more rudimentary terms - so I'm finding the problem hard to find a solution for.

For a single use-case, I run the following file (my API key omitted with 'MY APIKEY')

curl -X POST -u "apikey: MY APIKEY" --header "Content-Type: audio/flac" --data-binary "@audiofile_1.flac" "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&speaker_labels=true" > C:/Users/outputpath/output_1.txt

What I am essentially trying to achieve is to overcome having to manually type and retype the names of the audio files and output. So if I had three (or more) audio files (i.e. audiofile_1, 2, and 3.flac), i would like to create an output file corresponding to each audio file - Some psuedo-code that might help explain what I mean would be

files = [file_1, file_2, file_3]

for file_x in files:
    run curl command
    save as output_x

Solution

  • You almost got it. You just need to learn some shell syntax:

    files=("file_1" "file_2" "file_3")
    
    for file_x in "${files[@]}"
    do
        curl -X POST -u "apikey: MY APIKEY" --header "Content-Type: audio/flac" --data-binary "@${file_x}" "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&speaker_labels=true" > "C:/Users/outputpath/${file_x}.txt"
    done
    

    First you create the files array with your list of files. Then, you iterate over those files and run the curl command on each of them.