【发布时间】:2018-08-12 16:13:27
【问题描述】:
我正在开展一个项目,以从音频文件中获取脚本。音频文件的格式为 flac。我正在使用 AWS Lambda 并在 node.js 中编写了代码。此外,我正在使用 IBM Speech to text 服务并使用他们提供的基本示例代码,可以在 here 找到。问题是我的 lambda 函数在运行这些函数之前完成。
我正在从 s3 下载文件并将其存储在本地(工作正常)。之后,我试图将相同的文件传递给 IBM Speech to Text SDK,它应该将音频文件的脚本返回到本地存储
代码如下:
const downloadFile = function (url1, dest, cb) {
const file = fs.createWriteStream(dest);
https.get(url1, function (res) {
//res.setEncoding('binary');
res.pipe(file);
file.on('finish', function () {
const stats = fs.statSync(dest);
const fileSizeInBytes = stats.size;
//Convert the file size to megabytes (optional)
const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
console.log(fileSizeInMegabytes);
file.close();
RunIBMWatson(dest);
callback(null,"Nice");
});
});
};
function RunIBMWatson(dest){
console.log(dest);
console.log("I am here");
const recognizeStream = speech_to_text.createRecognizeStream(params);
fs.createReadStream(dest).pipe(recognizeStream);
recognizeStream.pipe(fs.createWriteStream('/tmp/transcription.txt'));
recognizeStream.setEncoding('utf8');
recognizeStream.on('results', function(event) { onEvent('Results:', event); });
recognizeStream.on('data', function(event) { onEvent('Data:', event); });
recognizeStream.on('error', function(event) { onEvent('Error:', event); });
recognizeStream.on('close', function(event) { onEvent('Close:', event); });
recognizeStream.on('speaker_labels', function(event) { onEvent('Speaker_Labels:', event); });
function onEvent(name, event) {
console.log("I am in onEvent");
if (name === 'data'){
console.log(event);
}
这是我从 AWS Lambda 获得的函数日志:
2018-03-05 03:31:53.585 54.093469
2018-03-05 03:31:53.588 /tmp/sample.flac
2018-03-05 03:31:53.588 I am here
我是 AWS Lambda 和 Node.js 的初学者。所以如果有人能指出我犯的错误。
【问题讨论】:
标签: node.js aws-lambda ibm-watson