将base64音频转换为文件 [英] Convert base64 audio to file

查看:1380
本文介绍了将base64音频转换为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个音频blob,然后我运行

I have an audio blob, I then run

var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
  var base64data = reader.result;
  //log of base64data is "data:audio/ogg; codecs=opus;base64,GkX..."
}

现在我将这些数据发送到我的服务器,我想要做的就是转换为'.ogg'文件(首选wav或mp3)。 base64在传递给HTML音频播放器时工作正常。
在我试过的服务器上

Now I send this data to my server and all I'm trying to do is convert to an '.ogg' file (wav or mp3 preferred). The base64 works fine when passed to an HTML audio player. On the server I tried

fs.writeFileSync('file.ogg', base64data);

我总是创建文件,但它永远不会播放,我做错了什么?

I always get the file created however it never plays, what I'm I doing wrong please?

推荐答案

这里有以base64字符串编码的二进制数据。首先,您需要修剪数据网址元信息。然后你可以从base64字符串创建二进制缓冲区并将其存储到文件中。

You have binary data encoded in base64 string here. First of all you need trim data url meta info. Then you can create binary buffer from base64 string and store it to file.

fs.writeFileSync('file.ogg', Buffer.from(base64data.replace('data:audio/ogg; codecs=opus;base64,', ''), 'base64'));

这篇关于将base64音频转换为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆