简体   繁体   English

使用Node Js将bin文件解码为mp3

[英]decoding a bin file to mp3 using Node Js

I am encoding a MP3 file to Base64 in Node Js using this method : 我正在使用此方法在Node Js中将MP3文件编码为Base64:

encodebase64 = function(mp3file){
var bitmap = fs.readFileSync(mp3file);
var encodedstring = new Buffer(bitmap).toString('base64');
fs.writeFileSync('encodedfile.bin', encodedstring);}

and then again I want to construct the MP3 file from the Base64 bin file, but the file created is missing some headers , so obviously there's a problem with the decoding. 然后我又想从Base64 bin文件中构造MP3文件,但是创建的文件缺少一些标头,因此显然解码存在问题。 the decoding function is : 解码功能是:

decodebase64 = function(encodedfile){
var bitmap = fs.readFileSync(encodedfile);
var decodedString = new Buffer(bitmap, 'base64');
fs.writeFileSync('decodedfile.mp3', decodedString);}

I wondered if anyone can help Thanks. 我想知道是否有人可以帮助谢谢。

Perhaps it is an issue with the encoding parameter. 也许这是编码参数的问题。 See this answer for details. 有关详细信息,请参见此答案 Try using utf8 when decoding to see if that makes a difference. 解码时尝试使用utf8 ,以查看是否有区别。 What platforms are you running your code on? 您在什么平台上运行代码?

@Noah mentioned an answer about base64 decoding using Buffers, but if you use the same code from the answer, and you try to create MP3 files with it, then they won't play and their file size will be larger than original ones just like you experienced in the beginning. @Noah提到了有关使用Buffer进行base64解码的答案 ,但是如果您使用答案中的相同代码,并尝试使用它创建MP3文件,则它们将无法播放,并且文件大小将比原始文件大,就像您在一开始就经历过。

We should write the buffer directly to the mp3 file we want to create without converting it(the buffer) to an ASCII string: 我们应该将缓冲区直接写到要创建的mp3文件中,而不要将其(缓冲区)转换为ASCII字符串:

// const buff = Buffer.from(audioContent, 'base64').toString('ascii'); // don't
const buff = Buffer.from(audioContent, 'base64');
fs.writeFileSync('test2.mp3', buff);

More info about fs.writeFile / fs.writeFileAsync 有关fs.writeFile / fs.writeFileAsync的更多信息

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

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