[英]php IBM Watson Speech to text "unable to transcode data stream audio/flac " error
I am trying to use IBM Watson's speech-to-text service using PHP.我正在尝试使用 PHP 使用 IBM Watson 的语音到文本服务。 I don't know much about the curl language.
我对 curl 语言了解不多。 In the documentation of IBM cloud, there's this curl code:
在 IBM 云的文档中,有这个 curl 代码:
curl -X POST -u "apikey:{apikey}" --header "Content-Type: audio/flac" --data-binary
@{path_to_file}audio-file.flac "{url}/v1/recognize"
I used a curl to PHP converter service and got this:我使用了 curl 到 PHP 转换器服务并得到了这个:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '{url}/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '@' .realpath('{path_to_file}audio-file.flac')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . '{apikey}');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I am working on a server, my overall PHP code successfully connects to the server and the user can upload a file on the server.我在服务器上工作,我的整体 PHP 代码成功连接到服务器,用户可以在服务器上上传文件。 But when I want to send the file on the server to the IBM cloud, I get an error.
但是当我想将服务器上的文件发送到 IBM 云时,我得到一个错误。 I modified the curl code so that I send the binary content of the file:
我修改了 curl 代码,以便发送文件的二进制内容:
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/fd2c1d8f-3fba-48aa-846c-f0b4d603b724/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$file = file_get_contents('uploads/hello.flac');
$post = array(
'file' => $file
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'dummyKey');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error :' . curl_error($ch);
}
echo $result;
curl_close($ch);
?>
So I changed the associative array when making $post
since I want to pass the binary content of the file to IBM.所以我在制作
$post
时更改了关联数组,因为我想将文件的二进制内容传递给 IBM。 uploads/hello.flac
is the link to the target file. uploads/hello.flac
是目标文件的链接。 The uploads directory and my PHP file are in the same directory so addressing is true.上传目录和我的 PHP 文件位于同一目录中,因此寻址是正确的。
I checked several other links such as this and after a couple hours of tweaking failed to solve the error.我检查了其他几个链接,例如这个,经过几个小时的调整未能解决错误。
When I echo $result
, this is what I get { "error": "unable to transcode data stream audio/flac -> audio/l16 ", "code": 400, "code_description": "Bad Request" }
, instead of getting something like this:当我
echo $result
时,这就是我得到的{ "error": "unable to transcode data stream audio/flac -> audio/l16 ", "code": 400, "code_description": "Bad Request" }
,而不是得到这样的东西:
{
"result_index": 0,
"results": [
{
"alternatives": [
{
"confidence": 0.96
"transcript": "several tornadoes touch down as a line of severe thunderstorms swept through Colorado on Sunday "
}
],
"final": true
}
]
}
Thanks.谢谢。
Sorry, very late to the party on this one, but...this cURL sequence works for us:抱歉,这个聚会很晚了,但是……这个 cURL 序列对我们有用:
$ibm_instance_url = ""; /// YOUR IBM INSTANCE URL
/// EXAMPLE: https://api.eu-gb.speech-to-text.watson.cloud.ibm.com/instances/123456789...
$username = "apikey";
$password = ""; /// YOUR IBM API KEY GOES HERE
$path_to_file = "/path/to/file/";
$file_name = ""; /// YOUR FILENAME GOES HERE
$file_extention = "mp3"; /// YOUR IBM AUDIO FORMAT GOES HERE
$open_path_to_filename = $path_to_file . $file_name .".". $file_extention;
$file = fopen($open_path_to_filename, 'r');
$filesize = filesize($open_path_to_filename);
$bytes = fread($file,$filesize);
$headers = array("Content-Type: audio/".$file_extention ,"Transfer-Encoding: chunked"); // ,"Transfer-Encoding: binary"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ibm_instance_url."/v1/recognize");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bytes);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$executed = curl_exec($ch);
curl_close($ch);
We found that the key to success was making sure that the audio file uploaded to IBM was correctly / formally encoded.我们发现成功的关键是确保上传到 IBM 的音频文件正确/正式编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.