教程:使用 Node.js 体验语音转文本 API
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
使用 Deepgram 的 API 将音频文件或音频流转换为文本。
创建这个博客的目的是记录详细的操作过程以及我学习 Node.js 的笔记。
如果你也感兴趣并想亲自动手实践,那就按照下面的步骤来,尽情享受吧!
先决条件
入门
我们首先应该导航到我们想要的目录,然后使用以下命令创建一个文件夹(例如,命名为 sttApp):
mkdir sttApp
然后使用你常用的IDE打开文件夹。我用的是VS Code。现在可以看到目录是空的,没有任何文件。
下一步,让我们使用终端,导航到当前目录/sttApp:
cd sttApp
运行以下代码初始化新应用程序:
npm init
多次按下回车键以保留这些参数的默认配置,然后您的 CLI 应该会得到类似这样的结果:
接下来,我们使用以下命令安装 Deepgram Node.js SDK:
npm install @deepgram/sdk
如果前面的步骤都正确,那么到目前为止,你的代码IDE中应该会有一个类似如下的目录:
现在,在你的代码 IDE 的当前目录(/sttAPP )中创建一个名为index.js 的文件,并将以下代码复制粘贴到index.js 文件中,然后保存文件:
const { Deepgram } = require('@deepgram/sdk');
const fs = require('fs');
// The API key you created in step 1
const deepgramApiKey = 'YOUR_API_KEY';
// Replace with your file path and audio mimetype
const pathToFile = 'SOME_FILE.wav';
const mimetype = 'audio/wav';
// Initializes the Deepgram SDK
const deepgram = new Deepgram(deepgramApiKey);
console.log('Requesting transcript...')
console.log('Your file may take up to a couple minutes to process.')
console.log('While you wait, did you know that Deepgram accepts over 40 audio file formats? Even MP4s.')
console.log('To learn more about customizing your transcripts check out developers.deepgram.com.')
deepgram.transcription.preRecorded(
{ buffer: fs.readFileSync(pathToFile), mimetype },
{ punctuate: true, language: 'en-US' },
)
.then((transcription) => {
console.dir(transcription, {depth: null});
})
.catch((err) => {
console.log(err);
});
下一步是登录您的Deepgram 账户,导航至您的控制面板,然后选择通过 API 或 SDK 获取转录稿:
点击“显示密钥”并复制您的API 密钥:
下一步,将您的API 密钥粘贴到 index.js 文件的第 5 行,如下所示:
然后,让我们将第 8 行和第 9 行替换为语音文件路径和 MIME 类型
(提示:使用新的 CLI 导航到语音文件所在的目录,并使用它pwd来获取绝对路径):
最后,让我们使用以下命令运行我们的应用程序(请确保您位于 /sttApp 目录下):
node index.js
您将收到一个 JSON 响应,其中包含您想要的转录文本,包括词数组、时间戳和置信度分数:
太酷了!
如果您对以上内容仍有疑问,请随时在下方留言,或访问我的 Git 仓库查看整个项目:linkToGit
参考
https://console.deepgram.com/project/850abca5-449a-47fa-8c40-6a463e59ad00/mission/transcript-via-api-or-sdk
https://dev.to/devteam/join-us-for-a-new-kind-of-hackathon-on-dev-brought-to-you-by-deepgram-2bjd
我的提交内容概述
这是一篇面向初学者的教程,教你如何使用 Deepgram 的 STT API 学习 node.js。
提交类别:
分析大使
GitHub 代码链接
更多资源/信息
没有任何
文章来源:https://dev.to/yongchanghe/tutorial-play-with-a-speech-to-text-api-using-nodejs-2527











