[英]How to convert an mp3 file to raw audio format using javascript
I'm working on a project that involves song matching, so I am integrating with rapidApi's shazam endpoints.我正在从事一个涉及歌曲匹配的项目,因此我正在与 rapidApi 的 shazam 端点集成。 But the thing is, the song matching endpoint needs the audio snippet to be a base64 string of the audio in raw audio format.
但问题是,歌曲匹配端点需要音频片段是原始音频格式的 base64 字符串。 I know the API works.
我知道 API 作品。 I downloaded a 3rd party application to do the conversion from mp3 to.raw, and converted it to base64 before making the request with it.
我下载了一个 3rd 方应用程序来进行从 mp3 到 .raw 的转换,并在使用它发出请求之前将其转换为 base64。
Now, I need to integrate this flow programmatically.现在,我需要以编程方式集成此流程。 How do I convert an mp3 or any audio source really to a.raw file?
如何将 mp3 或任何音频源真正转换为 .raw 文件? I've done a lot of searching but I can't find any solution.
我做了很多搜索,但找不到任何解决方案。
I went with the @ffmpeg/ffmpeg webassembly package. A javascript port of ffmpeg.我使用了@ffmpeg/ffmpeg webassembly package。ffmpeg 的 javascript 端口。
const convertToRaw = async (filePath)=>{
const ffmpeg = createFFmpeg({ log: true })
await ffmpeg.load();
const file = await fetchFile(filePath)
ffmpeg.FS('writeFile', 'input.mp3', file)
await ffmpeg.run('-i', 'input.mp3', '-f', 's16le', '-acodec', 'pcm_s16le', '-ac', '1', 'output.raw')
const data = ffmpeg.FS('readFile', 'output.raw')
const base64 = Buffer.from(data.buffer).toString('base64')
return base64
}
I think the web audio's decodeAudioData is what you are looking for.我认为 web 音频的 decodeAudioData 就是您要找的。
You want the PCM data, right?你想要 PCM 数据,对吧?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.