使用速率限制发布音频数据

时间:2018-06-06 20:01:13

标签: bash api http curl audio

我正在尝试使用curl发布音频数据,用于允许传输/接收音频文件的HTTP-API。

首先我尝试了这个:

curl -vv --http1.0 -H "Content-Type: audio/basic" -H "Content-Length: 9999999" -H "Connection: Keep-Alive" -H "Cache-Control: no-cache" --data-binary @- 'http://IP/API-Endpoint.cgi'

这似乎有效:

*   Trying [IP]...
* TCP_NODELAY set
* Connected to [IP] ([IP]) port 80 (#0)
> POST /API-Endpoint.cgi HTTP/1.0
> Host: [IP]
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: audio/basic
> Content-Length: 9999999
> Connection: Keep-Alive
> Cache-Control: no-cache
> 
* upload completely sent off: 17456 out of 17456 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/plain
< Content-Length: 0
* HTTP/1.0 connection set to keep alive!
< Connection: keep-alive
< Date: Wed, 06 Jun 2018 19:38:37 GMT
< Server: lighttpd/1.4.45

但我只能听到音频文件的最后一部分。 (该文件具有适用于API的正确音频格式:G.711μ-law,8000 Hz)我的下一个猜测是,音频传输速度太快,必须实时发送到API端点。所以我尝试了curl的--limit-rate参数,该参数没有效果。然后我尝试使用速率限制将数据传输到curl:

cat myfile.wav | pv -L 10k | curl -vv --http1.0 -H "Content-Type: audio/basic" -H "Content-Length: 9999999" -H "Connection: Keep-Alive" -H "Cache-Control: no-cache" --data-binary @- 'http://IP/API-Endpoint.cgi'

但结果总是一样的:我只能听到音频文件的最后一部分。似乎curl正在等待管道输入完成,然后像以前一样发送请求。

是否有选项可以“实时”从bash将音频发布到HTTP-API?

更新 在不强制HTTP 1.0的情况下,我得到以下结果:

curl -vv -H "Content-Type: audio/basic" --data-binary '@myfile.wav' 'http://[IP]/API-Endpoint.cgi'
*   Trying [IP]...
* TCP_NODELAY set
* Connected to [IP] ([IP]) port 80 (#0)
> POST /API-Endpoint.cgi HTTP/1.1
> Host: [IP]
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: audio/basic
> Content-Length: 15087
> Expect: 100-continue
> 
< HTTP/1.1 417 Expectation Failed
< Content-Type: text/html
< Content-Length: 363
< Connection: close
< Date: Wed, 06 Jun 2018 20:34:22 GMT
< Server: lighttpd/1.4.45
< 
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>417 - Expectation Failed</title>
 </head>
 <body>
  <h1>417 - Expectation Failed</h1>
 </body>
</html>
* Closing connection 0

1 个答案:

答案 0 :(得分:0)

使用-H "Content-Length: 9999999"时,您说音频文件的长度恰好为9999999字节(大约10兆字节),但是curl报告您的文件为17456字节:

* upload completely sent off: 17456 out of 17456 bytes

(大约0.02兆字节),因此您的Content-Length标头是错误的(这是我的最佳猜测),或者是将音频文件馈送至curl的程序有问题,过早关闭了stdin。

要么修复您的Content-Length标头,要么修复提供curl的stdin的程序,希望可以完整发送整个文件。

编辑:哦,看来服务器无法处理Expect: 100-continue,要禁用该标头,请添加参数-H 'Expect:'

(空的Expect标头将使curl完全忽略标头,而不是将标头发送为空)

...但是要回答标题中的问题,是的,--limit-rate参数。