简体   繁体   English

在使用 Javascript 上传之前更改音频文件的比特率

[英]Changing bitrate of audio file before uploading using Javascript

Implementing file uploads with different bitrates , compressing them into different qualities and then uploading.实现不同比特率的文件上传,将它们压缩成不同的质量然后上传。 Is there a way this could be achieved using javascript ?有没有办法使用javascript来实现? (Audio file format is mp3) (音频文件格式为 mp3)

There's nothing built into JavaScript to do things like MP3 encoding, but there is a library for that here: https://github.com/Kagami/ffmpeg.js/ JavaScript 没有内置任何东西来做 MP3 编码之类的事情,但这里有一个库: https : //github.com/Kagami/ffmpeg.js/

I also found an article about using that library, albeit for a somewhat different task than you have in mind, here: https://medium.com/jeremy-gottfrieds-tech-blog/javascript-tutorial-record-audio-and-encode-it-to-mp3-2eedcd466e78我还找到了一篇关于使用该库的文章,尽管它的任务与您的想法有所不同,请点击此处: https : //medium.com/jeremy-gottfrieds-tech-blog/javascript-tutorial-record-audio-and-编码到 mp3-2eedcd466e78

I don't think there's any simple, straight-forward way to accomplish what you have in mind, unfortunately.不幸的是,我认为没有任何简单直接的方法可以实现您的想法。 You'll have to plod through this API and figure out how to use it to accomplish your goal.您必须仔细研究这个 API 并弄清楚如何使用它来实现您的目标。

Firstly, I recommend not bothering with MP3 at all.首先,我建议根本不要理会 MP3。 Opus and AAC will give you better sound quality for a given bitrate, and are well-supported. Opus 和 AAC 将为给定的比特率提供更好的音质,并且得到很好的支持。

Next, you should start with a lossless source to begin with, if at all possible.接下来,如果可能的话,您应该从无损源开始。 By using lossy compression on something that was already lossy-compressed, you're losing even more quality while wasting even more bandwidth by emphasizing compression artifacts.通过对已经进行了有损压缩的内容使用有损压缩,您会损失更多质量,同时通过强调压缩伪影来浪费更多带宽。

Now, on to your question.现在,回到你的问题。 The only optimized way to do this in-browser is to use the Media Recorder API .在浏览器中执行此操作的唯一优化方法是使用Media Recorder API Basically, you can create a few instances of Media Recorder for the bitrates you want to target.基本上,您可以为要定位的比特率创建一些 Media Recorder 实例。 Then, playback audio through a Media Stream, and stream the outputs of the Media Recorders up to your server.然后,通过媒体流播放音频,并将媒体记录器的输出流式传输到您的服务器。 Ideally, this could happen faster than realtime through an Offline Audio Context.理想情况下,这可能比通过离线音频上下文实时发生得更快。 However, this isn't working in browsers at the moment so you will have to do it in realtime.但是,目前这在浏览器中不起作用,因此您必须实时进行。

Unless you have a very strange edge use case, I wouldn't bother with this.除非你有一个非常奇怪的边缘用例,否则我不会打扰这个。 Transcode on the server side, for a much more efficient and predictable outcome.在服务器端转码,以获得更高效和可预测的结果。 Perhaps in the near future, we'll have a better codec API on the browser.也许在不久的将来,我们将在浏览器上拥有更好的编解码器 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM