javascript Web Audio API - 录制到 MP3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16881703/
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
Web Audio API - record to MP3?
提问by Light
I am asking because I couldn't find the answer anywhere. I have successfully implemented RecorderJSin order to record microphone input in JS. However, the recorded file is WAV which results in large files. I am looking for a way to record with JS directly to MP3, or encode the bits somehow to MP3 instead of WAV.
我问是因为我在任何地方都找不到答案。我已经成功地实现了RecorderJS,以便在 JS 中记录麦克风输入。但是,录制的文件是 WAV,导致文件很大。我正在寻找一种使用 JS 直接录制到 MP3 的方法,或者以某种方式将位编码为 MP3 而不是 WAV。
How can it be done? Is there a Web Audio API function that can do that or JS MP3 encoder of some sort?
怎么做到呢?是否有可以做到这一点的 Web Audio API 函数或某种 JS MP3 编码器?
回答by cwilso
The only Javascript MP3 encoder I've seen is https://github.com/akrennmair/libmp3lame-js, which is a port using emscripten. It's supposed to be slow, and I've never used it.
我见过的唯一 Javascript MP3 编码器是https://github.com/akrennmair/libmp3lame-js,这是一个使用 emscripten 的端口。它应该很慢,我从未使用过它。
I don't know of any natively-written Javascript MP3 encoders, and encoding is not covered by the Web Audio API.
我不知道有任何本地编写的 Javascript MP3 编码器,并且 Web Audio API 不涵盖编码。
回答by Alex Zhukov
There's a library written in pure javascript, called lamejs. To encode mp3s from raw audio. It is much faster than emscripten compile of libmp3lame. https://github.com/zhuker/lamejs
有一个用纯 javascript 编写的库,称为 lamejs。从原始音频编码 mp3。它比 libmp3lame 的 emscripten 编译快得多。https://github.com/zhuker/lamejs
Example usage:
用法示例:
lib = new lamejs();
mp3encoder = new lib.Mp3Encoder(1, 44100, 128); //mono 44.1khz encode to 128kbps
samples = new Int16Array(44100); //one second of silence
var mp3 = mp3encoder.encodeBuffer(samples); //encode mp3
回答by Sam
I was frustrated with this problem, and existing solutions, so I came up with something simpler:
我对这个问题和现有的解决方案感到沮丧,所以我想出了一些更简单的方法:
https://github.com/sb2702/audioRecord.js
https://github.com/sb2702/audioRecord.js
Usage
用法
Create a recorder object (async because requires user permission)
创建记录器对象(异步,因为需要用户权限)
Recorder.new(function(recorder){
});
Start recording
开始录音
recorder.start();
Stops recording
停止录音
recorder.stop();
Export as mp3
导出为 mp3
recorder.exportMP3(function(mp3Blob){
console.log("Here is your blob: " + URL.createObjectURL(mp3Blob));
});
Mostly based on RecorderJS, but changed some things around to export to mp3 files, and to not have to muck around with AudioContext / navigator.getUs
主要基于 RecorderJS,但更改了一些内容以导出到 mp3 文件,并且不必与 AudioContext / navigator.getUs 混在一起
回答by denis.peplin
I have found a nice library with live demos: MediaStreamRecorder
我找到了一个带有现场演示的不错的库:MediaStreamRecorder
One of demos is here: Audio Recording
演示之一在这里:录音
RecordRTCis also can be useful but MSR it seems is easier to start with.
RecordRTC也很有用,但 MSR 似乎更容易开始。
回答by Ich
Encoding into smaller formats is currently only supported by Firefox:
目前仅 Firefox 支持编码成更小的格式:
- http://w3c.github.io/mediacapture-record/MediaRecorder.html
- https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
- http://w3c.github.io/mediacapture-record/MediaRecorder.html
- https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Not supported 25.0 (25.0) Not supported Not supported Not supported
Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Not supported 25.0 (25.0) Not supported Not supported Not supported
AFAIK only OGG is supported. But better OGG than WAV.
AFAIK 仅支持 OGG。但是OGG比WAV好。
回答by oliverbachmann
To record mp3 using javascript without any other framework using a web worker, you can use this project: https://github.com/nusofthq/Recordmp3jswhich is also very well explained here:
要使用 javascript 录制 mp3 而没有任何其他框架使用 web worker,你可以使用这个项目:https: //github.com/nusofthq/Recordmp3js,这里也有很好的解释:
http://audior.ec/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/
http://audior.ec/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/
With this, it's also possible to write to a .mp3 file and to make it downloadable.
有了这个,还可以写入 .mp3 文件并使其可下载。