1

修复

await storage.bucket(bucketName).upload(`./uploads/${filename}`, {
    gzip: false,
    metadata: {
      content_type : "audio/flac",
      originalMimeType: "audio/flac",
      cacheControl: 'public, max-age=31536000',
      uploadType: "multipart"      
    },

我不完全确定为什么会这样,但我的导师建议进行此更改:设置 gzip:false。现在,这成功地从存储桶中转录了短音频和长音频。这可能是内容编码的问题。

上一期

我正在 Node.js 中为编码训练营项目制作语音到文本的快速应用程序。目前,我遇到了 multer 问题,或者文件上传到我的谷歌“存储桶”的过程。我可以手动将 .flac 文件上传到 Google 存储桶,并且文件类型保持为“audio/flac”。我可以将这些文件发送到 API 以进行非常准确的转录。但是,当通过我的多部分表单和异步函数 uploadFile 发送文件时,对象将作为“audio/x-flac”上传到 Google 存储桶,并且无法播放或转录。在我的前端,用户应该会看到一个表单,他们可以在其中提交 .flac 文件。我的问题是,我需要采取哪些步骤来确保文件在上传到 Google 存储桶时保持“audio/flac”与“audio/x-flac”?

以下是该表单在前端的工作方式:

        method="POST" enctype="multipart/form-data">
        <input type="file" id="audio" name="filename" class="btn">
        <input type="submit" class="btn">
    </form>

这是我的后端的外观:

//set the destination of the form uploads to disk storage
let mStorage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./uploads")
  },
  filename: function (req, file, cb) {
    //set the extension to the original extension from form input
    cb(null, file.fieldname + Date.now() + path.extname(file.originalname));
    // cb(null, file.buffer)
  }
})

//initialize the upload variable
let uploads = multer({storage: mStorage})

//Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();

router.post("/results", uploads.single("filename"),(req, res) => {
  console.log("post route hit");
  //get the input field from the user
  // console.log(req.file);
  let filename = req.file.filename;
  console.log(filename);
  const bucketName = 'voiceappbucket';
  async function uploadFile() {
    // Uploads a local file to the bucket
  await storage.bucket(bucketName).upload(`./uploads/${filename}`, {
    gzip: true,
    metadata: {
      content_type : "audio/flac",
      cacheControl: 'public, max-age=31536000',
      uploadType: "multipart"      
    },
  });
  console.log(`${filename} uploaded to ${bucketName}.`);
  res.redirect(`/${req.file.filename}`)
  }
  uploadFile().catch(console.error);
})

如果有人遇到过类似问题并成功解决了这些问题,我很乐意听取您的意见。对我的代码或我提出这个问题的方式的反馈表示赞赏,因为我是社区的新手!:-)

4

0 回答 0