【问题标题】:Why doesn't node-lame encode properly (nodeJS library)?为什么 node-lame 不能正确编码(nodeJS 库)?
【发布时间】:2021-10-30 10:12:05
【问题描述】:

我一直在尝试使用 node-lame library 将文件从上传的比特率编码为 32 kbps 以节省空间,就像我使用Sharp压缩图像一样。

我的代码首先检查文件是否为音频文件。如果是,则制作编码器,它应该对其进行编码:

if (aud.test(user_file)){

        const encoder = new Lame({
            "output": req.file.path,
            "bitrate": 32,
        }).setFile(req.file.path);
        
        await encoder
            .encode()
            .then(() => {})
            .catch((error) => {
                // Something went wrong
            });
    }

问题在于它实际上并没有被编码。我也在我的.then 中尝试过,但没有帮助。

.then(data => {
    fs.writeFileSync(req.file.path + '.mp3', data);
    user_file = user_file + '.mp3';
    fs.unlinkSync(req.file.path)
})

这应该是一个相当简单的库,所以我不知道我做错了什么。我正在尝试从一个文件到另一个文件进行编码。

也试过这个:

const encoder = new Lame({
            "output": user_file + '.mp3',
            "bitrate": 32,
        }).setFile(req.file.path);

【问题讨论】:

  • 您做了哪些故障排除工作?您尝试转换的文件是否真的有效?如果有的话,你会得到什么错误? unlinkSync 是做什么的?你试过没有那个吗?这是您正在编码的文件的正确比特率吗?
  • 我检查了错误,但没有得到任何错误。我尝试编写一个新的编码文件并用 fs 删除旧的文件,比如用 sharp 看看它是否有帮助,但判断它可能没有的文档。这个想法是输入一个上传的文件,通过编码器运行它并获得相同的剪辑,但比特率较低。
  • 你的路线是什么样的?您确定您已成功上传文件吗?

标签: javascript node.js lame node-lame


【解决方案1】:

我继续为此编写了一个演示。你可以find the full repo here。我已经验证这确实有效,但请记住,这只是概念验证。

这是我的 Express 服务器的样子:


const express = require('express');
const fs = require('fs');
const path = require('path');
const fileUpload = require('express-fileupload');
const Lame = require('node-lame').Lame;

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(fileUpload());

// File upload path
app.post('/upload', async (req, res) => {
  const fileToEncode = req.files.uploadedFile;
  if (!fileToEncode) {
    res.status(500).end();
    return;
  }

  const filePath = path.resolve('./uploads', fileToEncode.name);
  const outputPath = path.resolve('./uploads', fileToEncode.name + '-encoded.mp3');

  // Save uploaded file to disk
  await fileToEncode.mv(filePath);

  try {
    const encoder = new Lame({ 
      output: outputPath,
      bitrate: 8,
    }).setFile(filePath);
    await encoder.encode();
    res.download(outputPath);
  } catch (encodingError) {
    console.error(encodingError);
    res.status(500).send(encodingError);
  }

  // Removed files we saved on disk
  res.on('finish', async () => {
    await fs.unlinkSync(filePath);
    await fs.unlinkSync(outputPath);
  })
});

// Home page
app.get('*', (req, res) => {
    res.status(200).send(`
    <!DOCTYPE html>
    <html>
    <body>

    <p id="status"></p>

    <form method="post" enctype="multipart/form-data" action="/upload" onsubmit="handleOnSubmit(event, this)">
      <input name="uploadedFile" type="file" />
      <button id="submit">Submit Query</button>
    </form>

    <script>
    async function handleOnSubmit(e,form) {
      const statusEl = document.getElementById("status");
      statusEl.innerHTML = "Uploading ...";
      e.preventDefault();
      const resp = await fetch(form.action, { method:'post', body: new FormData(form) });
      const blob = await resp.blob();
      const href = await URL.createObjectURL(blob);
      Object.assign(document.createElement('a'), {
        href,
        download: 'encoded.mp3',
      }).click();
      statusEl.innerHTML = "Done. Check your console.";
    }
    </script>

    </body>
    </html>    
    `);
});

process.env.PORT = process.env.PORT || 3003;

app.listen(process.env.PORT, () => { 
    console.log(`Server listening on port ${process.env.PORT}`);
});

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多