在客户端 JavaScript 中将 WAV 转换为任何压缩音频格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17457029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:25:05  来源:igfitidea点击:

Converting WAV to any compressed audio format in client-side JavaScript

javascriptwavaudio-recordinggetusermedia

提问by Arjun Bajaj

I am recording audio from getUserMedia({audio:true});in the browser using Recorder.jsand then exporting it as a WAV file because that's the only option the library provides.

我正在getUserMedia({audio:true});使用Recorder.js在浏览器中录制音频,然后将其导出为 WAV 文件,因为这是库提供的唯一选项。

A 1 minute 20 seconds file is 14.1 MB large. I need to upload the audio to a server and I need to do it fast. How do I convert the WAV audio in any other compressed format to lower the file size?

1 分 20 秒的文件大小为 14.1 MB。我需要将音频上传到服务器,并且需要快速完成。如何将 WAV 音频转换为任何其他压缩格式以减小文件大小?

I don't mind converting to:

我不介意转换为:

  • MP3
  • Opus
  • WebM
  • Ogg
  • FLAC
  • any other format you know of
  • MP3
  • 作品
  • 网络管理器
  • 奥格
  • FLAC
  • 您知道的任何其他格式

If there is no way as of now to convert to any of these formats, how can I compress the WAV file on the client?

如果目前无法转换为这些格式中的任何一种,我该如何在客户端上压缩 WAV 文件?

PS: I did a lot of searches to find anything that converts WAV in JS, but found nothing. libmp3lame.jsisn't working in Chrome.

PS:我进行了大量搜索以找到任何可以在 JS 中转换 WAV 的内容,但一无所获。libmp3lame.js在 Chrome 中不起作用。

Thanks!

谢谢!

采纳答案by Remus Negrota

I've made an audio recorder that records to mp3 directly from the browser combining RecorderJS and libmp3lame.js

我制作了一个录音机,可以直接从结合 RecorderJS 和 libmp3lame.js 的浏览器录制到 mp3

You can find the gitHub project here: https://github.com/nusofthq/Recordmp3js

你可以在这里找到 gitHub 项目:https: //github.com/nusofthq/Recordmp3js

and a more detailed explanation of the implementation: http://nusofthq.com/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/

以及更详细的实现说明:http: //nusofthq.com/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/

回答by jesup

What you really want is the mediaStream "Recording API", which is currently being worked on. Until that's available, I advise using emscriptem on C/C++ source, and consider running it in a webworker to avoid blocking the UI and other tabs.

您真正想要的是目前正在开发的 mediaStream“Recording API”。在可用之前,我建议在 C/C++ 源代码上使用 emscriptem,并考虑在 webworker 中运行它以避免阻塞 UI 和其他选项卡。

回答by AddingColor

I was facing the same problem and came up with quite a quick and dirty solution:

我遇到了同样的问题,并想出了一个非常快速和肮脏的解决方案:

  • zip the wav-filewith zip.js (works with Chrome, Firefox, Safari 6 and Internet Explorer 10)
  • 使用 zip.js压缩 wav 文件(适用于 Chrome、Firefox、Safari 6 和 Internet Explorer 10)

Further intel see Documentation zip.js

更多英特尔请参阅文档 zip.js

At least this is reducing size a lot, file is about >75% smaller so 1:4 compression

至少这大大减少了文件的大小,文件大约小了 75% 以上,因此1:4 压缩

UPDATE: Maybe have a look at this: https://webrtc.github.io/samples/

更新:也许看看这个:https: //webrtc.github.io/samples/

It is a Chat Application for Chrome and Firefox developed by google, I assume with kind of CC-License

它是由谷歌开发的 Chrome 和 Firefox 聊天应用程序,我假设有一种 CC-License

回答by CodingIntrigue

I had a similar issue (also using recorder.js) and have managed to resolve using the superb videoconverter.jsproject which includes a port of ffmpeg to Javascript using emscripen. Downside to this is the ffmpeg.js file is about 25Mb.

我有一个类似的问题(也使用 recorder.js),并设法使用极好的videoconverter.js项目解决,该项目包括使用 emscripen 的 ffmpeg 到 Javascript 的端口。缺点是 ffmpeg.js 文件大约为 25Mb。

I modified the existing exportWAVfunction in recorderWorker.js to return both a WAV (for HTML5 <audio>playback) and a Blob containing an encoded MP2 file:

我修改了exportWAVrecorderWorker.js 中的现有函数以返回 WAV(用于 HTML5<audio>播放)和包含编码 MP2 文件的 Blob:

function encodeWAV(samples) {

  var buffer = new ArrayBuffer(44 + samples.length * 2);
  var view = new DataView(buffer);

  /* ... various writing methods */

  return { wavdata: new Blob([buffer], { type: "audio/wav" }), mp2data: ffmpeg_convert(buffer) };
}

function ffmpeg_convert(buffer) {
    console.log("starting mp2 conversion");
    var args = "-i input -f mp2 output.mp2";
    var results = ffmpeg_run({
        arguments: args.split(" "),
        files: [
          {
              data: new Uint8Array(buffer),
              "name": "input"
          }
        ]
    });
    if (results) {
        var file = results[0];
        console.log("File recieved", file.name, file.data);
        return new Blob([file.data], { type: "audio/mpeg" });
    }
    return null;
}

This method can be used to encode the WAV to any codec suppored by ffmpeg's libavcodec

此方法可用于将 WAV 编码为 ffmpeg 支持的任何编解码器 libavcodec

回答by mido

I was able to achieve compression using opus.js,

我能够使用opus.js实现压缩,

you can find my implementation here: recordOpus, but there is a catch, mine is coupled with server-side, and I use node.jsserver....

你可以在这里找到我的实现:recordOpus,但有一个问题,我的与服务器端耦合,我使用node.js服务器....