3

我想使用 curl的 Alexa 语音 API ( https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/speechrecognizer-requests )。voicerecogniser API 调用比我习惯使用的更复杂,并且需要一个包含语音样本的 MP3 文件附件。谁能建议如何使用 curl 构建以下内容?(在给定的链接上有更多信息)

POST /v1/avs/speechrecognizer/xxxxxxxxxxxx HTTP/1.1

Host: access-alexa-na.amazon.com
Authorization: Bearer xxxxxxxxxxxx
Content-Type: multipart/form-data; boundary=boundary_term
Transfer-Encoding: chunked

--boundary_term
Content-Disposition: form-data; name="request"
Content-Type: application/json; charset=UTF-8

{
    "messageHeader": {
        "deviceContext": [
            {
                "name": "playbackState",
                "namespace": "AudioPlayer"
                "payload": {
                    "streamId": "xxxxxxxxxxxx",
                    "offsetInMilliseconds": "xxxxxxxxxxxx",
                    "playerActivity": "xxxxxxxxxxxx"
                }
            },
            {
                ...
            },
            ...
        ]
    },
    "messageBody": {
        "profile": "alexa-close-talk",
        "locale": "en-us",
        "format": "audio/L16; rate=16000; channels=1"
    }
}

--boundary_term
Content-Disposition: form-data; name="audio"
Content-Type: audio/L16; rate=16000; channels=1

...encoded_audio_data...

--boundary_term--
4

1 回答 1

0

我不是 bash 专家,但这就是我能够使用 cURL 与 AVS 交互的方式。我生成一个包含多部分正文内容的文件,其中包括二进制音频数据并将其传递给 cURL。

############################################################
# First we creat a bunch of variables to hold data.
############################################################

# Auth token
TOKEN="Atza|IQEBLjAsAhR..."

# Boundary
BOUNDARY="BOUNDARY1234"
BOUNDARY_DASHES="--"

# Newline characters
NEWLINE='\r\n';

# Metadata headers
METADATA_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"metadata\"";
METADATA_CONTENT_TYPE="Content-Type: application/json; charset=UTF-8";

# Audio headers
AUDIO_CONTENT_TYPE="Content-Type: audio/L16; rate=16000; channels=1";
AUDIO_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"audio\"";

# Metadata JSON body
METADATA="{\
\"messageHeader\": {},\
\"messageBody\": {\
\"profile\": \"alexa-close-talk\",\
\"locale\": \"en-us\",\
\"format\": \"audio/L16; rate=16000; channels=1\"\
}\
}"

############################################################
# Then we start composing the body using the variables.
############################################################

# Compose the start of the request body
POST_DATA_START="
${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}${METADATA_CONTENT_DISPOSITION}${NEWLINE}\
${METADATA_CONTENT_TYPE}\
${NEWLINE}${NEWLINE}${METADATA}${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}\
${AUDIO_CONTENT_DISPOSITION}${NEWLINE}${AUDIO_CONTENT_TYPE}${NEWLINE}"

# Compose the end of the request body
POST_DATA_END="${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${BOUNDARY_DASHES}${NEWLINE}"

# Now we create a request body file to hold everything including the binary audio data.

# Write metadata to body file
echo -e $POST_DATA_START > multipart_body.txt

# Append binary audio data to body file
cat hello.wav >> multipart_body.txt

# Append closing boundary to body file
echo -e $POST_DATA_END >> multipart_body.txt

############################################################
# Finally we get to compose the cURL request command
# passing it the generated request body file as the multipart body.
############################################################

# Compose cURL command and write to output file
curl -X POST \
  -H "Authorization: Bearer ${TOKEN}"\
  -H "Content-Type: multipart/form-data; boundary=${BOUNDARY}"\
  --data-binary @foo.txt\
  https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize\
  > response.txt

音频必须单声道以 16k Hz 采样,并带有符号的 16 位 PCM。否则 AVS 什么也不发回。

有关更多信息,请查看我的带有 cURL 的 Alexa 语音服务 (AVS)博客文章。

于 2016-01-18T06:07:44.560 回答