二进制文件转base64 nodejs
Binary file to base64 nodejs
当我调用 api tts.speech.microsoft.com 时,我得到了一个二进制音频文件,我想将这个二进制文件转换为 base64 字符串。
我已经尝试了很多东西,例如:
Buffer.from(body, "binary").toString("base64");
无效。
我不确定 'binary' 是准确的词,但它不是可读格式。
感谢您的帮助。
我猜你是按照官方文档 Quickstart: Convert text-to-speech using Node.js
的 Make a request and save the response
部分来编写代码的,如下所示。
var request = require('request');
let options = {
method: 'POST',
baseUrl: 'https://westus.tts.speech.microsoft.com/',
url: 'cognitiveservices/v1',
headers: {
'Authorization': 'Bearer ' + accessToken,
'cache-control': 'no-cache',
'User-Agent': 'YOUR_RESOURCE_NAME',
'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
'Content-Type': 'application/ssml+xml'
},
body: body
};
function convertText(error, response, body){
if (!error && response.statusCode == 200) {
console.log("Converting text-to-speech. Please hold...\n")
}
else {
throw new Error(error);
}
console.log("Your file is ready.\n")
}
// Pipe the response to file.
request(options, convertText).pipe(fs.createWriteStream('sample.wav'));
所以我修改了上面的官方代码,创建了一个函数encodeWithBase64
,用Base64对body
进行编码。
function encodeWithBase64(error, response, body){
if (!error && response.statusCode == 200) {
var strBase64 = Buffer.from(body).toString('base64');
console.log(strBase64);
}
else {
throw new Error(error);
}
console.log("Your file is encoded with Base64.\n")
}
// Pipe the response to file.
request(options, convertText);
或者您可以使用 npm 包 base64-stream
and get-stream
从 body
.
获取带有 Base64 的字符串
var base64 = require('base64-stream');
const getStream = require('get-stream');
(async () => {
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
var strBase64 = await getStream(b64s);
console.log(strBase64);
})();
否则stream-string
也可以
var base64 = require('base64-stream');
const ss = require('stream-string');
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
ss(b64s).then(data => {
console.log(data);
})
当我调用 api tts.speech.microsoft.com 时,我得到了一个二进制音频文件,我想将这个二进制文件转换为 base64 字符串。
我已经尝试了很多东西,例如:
Buffer.from(body, "binary").toString("base64");
无效。
我不确定 'binary' 是准确的词,但它不是可读格式。
感谢您的帮助。
我猜你是按照官方文档 Quickstart: Convert text-to-speech using Node.js
的 Make a request and save the response
部分来编写代码的,如下所示。
var request = require('request'); let options = { method: 'POST', baseUrl: 'https://westus.tts.speech.microsoft.com/', url: 'cognitiveservices/v1', headers: { 'Authorization': 'Bearer ' + accessToken, 'cache-control': 'no-cache', 'User-Agent': 'YOUR_RESOURCE_NAME', 'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm', 'Content-Type': 'application/ssml+xml' }, body: body }; function convertText(error, response, body){ if (!error && response.statusCode == 200) { console.log("Converting text-to-speech. Please hold...\n") } else { throw new Error(error); } console.log("Your file is ready.\n") } // Pipe the response to file. request(options, convertText).pipe(fs.createWriteStream('sample.wav'));
所以我修改了上面的官方代码,创建了一个函数encodeWithBase64
,用Base64对body
进行编码。
function encodeWithBase64(error, response, body){
if (!error && response.statusCode == 200) {
var strBase64 = Buffer.from(body).toString('base64');
console.log(strBase64);
}
else {
throw new Error(error);
}
console.log("Your file is encoded with Base64.\n")
}
// Pipe the response to file.
request(options, convertText);
或者您可以使用 npm 包 base64-stream
and get-stream
从 body
.
var base64 = require('base64-stream');
const getStream = require('get-stream');
(async () => {
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
var strBase64 = await getStream(b64s);
console.log(strBase64);
})();
否则stream-string
也可以
var base64 = require('base64-stream');
const ss = require('stream-string');
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
ss(b64s).then(data => {
console.log(data);
})