gpt4 book ai didi

node.js - 使用 FFMPEG 直播到网络音频 api

转载 作者:搜寻专家 更新时间:2023-10-31 22:45:56 30 4
gpt4 key购买 nike

我正在尝试使用 node.js + ffmpeg 将音频流式传输到仅使用网络音频 api 连接到局域网的浏览器。

不使用元素,因为它添加了自己的 8 到 10 秒缓冲区,我希望获得尽可能高的延迟(最多 1 到 2 秒左右)。

音频播放成功,但音频非常不稳定和嘈杂。

这是我的 node.js(服务器端)文件:

var ws = require('websocket.io'), 
server = ws.listen(3000);
var child_process = require("child_process");
var i = 0;
server.on('connection', function (socket)
{

console.log('New client connected');

var ffmpeg = child_process.spawn("ffmpeg",[
"-re","-i",
"A.mp3","-f",
"f32le",
"pipe:1" // Output to STDOUT
]);

ffmpeg.stdout.on('data', function(data)
{
var buff = new Buffer(data);
socket.send(buff.toString('base64'));
});
});

这是我的 HTML:

var audioBuffer = null;
var context = null;
window.addEventListener('load', init, false);
function init() {
try {
context = new webkitAudioContext();
} catch(e) {
alert('Web Audio API is not supported in this browser');
}
}

var ws = new WebSocket("ws://localhost:3000/");

ws.onmessage = function(message)
{
var d1 = base64DecToArr(message.data).buffer;
var d2 = new DataView(d1);

var data = new Float32Array(d2.byteLength / Float32Array.BYTES_PER_ELEMENT);
for (var jj = 0; jj < data.length; ++jj)
{
data[jj] = d2.getFloat32(jj * Float32Array.BYTES_PER_ELEMENT, true);
}

var audioBuffer = context.createBuffer(2, data.length, 44100);
audioBuffer.getChannelData(0).set(data);

var source = context.createBufferSource(); // creates a sound source
source.buffer = audioBuffer;
source.connect(context.destination); // connect the source to the context's destination (the speakers)
source.start(0);
};

谁能告诉我哪里出了问题?

问候,纳言

最佳答案

我成功了!!

我所要做的就是调整 channel 数量。

我已将 FFMPEG 设置为输出单声道音频,效果非常好。这是我的新 FFMOEG 命令:

var ffmpeg = child_process.spawn("ffmpeg",[
"-re","-i",
"A.mp3",
"-ac","1","-f",
"f32le",
"pipe:1" // Output to STDOUT
]);

关于node.js - 使用 FFMPEG 直播到网络音频 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204582/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com