【问题标题】:How to upload Google Cloud text to speech API's response to Cloud Storage [Node.js]如何将 Google Cloud 文本上传到语音 API 对 Cloud Storage [Node.js] 的响应
【发布时间】:2020-05-26 04:46:06
【问题描述】:

我使用 Node.js 服务器制作了一个简单的音频创建 Web 应用程序。我想使用 Cloud text to Speech API 创建音频,然后将该音频上传到云存储。

(我使用 Windows 10、适用于 Linux 的 Windows 子系统、Debian 10.3 和 Google Chrome 浏览器。)

这是 Node.js 服务器中的代码。

const client = new textToSpeech.TextToSpeechClient();
        async function quickStart() {

            // The text to synthesize
            const text = 'hello, world!';

            // Construct the request
            const request = {
                input: {text: text},
                // Select the language and SSML voice gender (optional)
                voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
                // select the type of audio encoding
                audioConfig: {audioEncoding: 'MP3'},
            };

            // Performs the text-to-speech request
            const [response] = await client.synthesizeSpeech(request);
            // Write the binary audio content to a local file
            console.log(response);

我想将response 上传到云存储。

我可以直接将response 上传到云存储吗?还是我必须将response 保存在 Node.js 服务器中并上传到云存储?

我搜索了互联网,但找不到将response直接上传到云存储的方法。所以,如果你有提示,请告诉我。提前谢谢你。

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-storage google-text-to-speech


    【解决方案1】:

    您应该能够做到这一点,您的所有代码都在同一个文件中。实现这一目标的最佳方式是使用云功能,它将文件发送到云存储。 但是,是的,您需要使用 Node.js 保存文件,然后将其上传到 Clou Storage。

    为此,您需要将文件保存在本地,然后将其上传到云存储。正如您可以在其他帖子here 中查看完整教程一样,您需要构建文件,将其保存在本地,然后上传。下面的代码是您需要在代码中添加的主要部分。

    ...
    const options = { // construct the file to write
         metadata: {
              contentType: 'audio/mpeg',
              metadata: {
                  source: 'Google Text-to-Speech'
              }
         }
    };
    
    // copied from https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-usage-nodejs
    const [response] = await client.synthesizeSpeech(request);
    // Write the binary audio content to a local file
    // response.audioContent is the downloaded file
         return await file.save(response.audioContent, options)
         .then(() => {
             console.log("File written to Firebase Storage.")
             return;
         })
         .catch((error) => {
             console.error(error);
         });
    ...
    

    实施此部分后,您将下载保存在本地的文件并准备好上传。我建议您更好地查看我mentioned 的其他帖子,以防您对如何实现它有更多疑问。

    如果这些信息对您有帮助,请告诉我!

    【讨论】:

    • 非常感谢您的详细回答。您的解决方案完美运行!
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2019-04-10
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多