简体   繁体   English

如何使用 javascript 将 mp3 文件转换为原始音频格式

[英]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.

相关问题 如何在javascript中将音频(mp3)文件转换为二进制文件? - how to convert an audio (mp3) file into a binary file in javascript? 如何使用 javascript 将 mp3 转换为声波图像 - How to convert mp3 to the sound wave image using javascript fluent-ffmpeg将音频块转换为mp3并将其附加到mp3文件 - fluent-ffmpeg to convert audio chunks to mp3 and append them to an mp3 file 如何在JavaScript中将原始数据转换为音频 - How to convert raw data to audio in javascript Electron 不播放 mp3 音频文件<audio>树莓派 3b+ 中的 Html5 标签</audio> - Electron does not play mp3 audio file using <audio> Html5 tag in Raspberry Pi 3b+ 通过HTML5查找流式MP3文件 <audio> 标签 - Seeking through a streamed MP3 file with HTML5 <audio> tag 如何从 HTTP 响应中保存音频(mp3) - How to save an audio(mp3) from an HTTP response 如何 stream 从服务器到客户端的媒体文件,例如我在环回 4 nodejs 中的 mp3 音频文件? - How to stream a media file from server to client side for example an mp3 audio file in my case in loopback 4 nodejs? 如何使用JavaScript将音频文件转换为字节数组 - How convert a audio file into byte array with javascript 在启动时使用Meteor FS集合将wav转换为mp3 - Convert wav to mp3 using Meteor FS Collections on Startup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM