Node 中的直播 mp3
Live stream mp3 in Node
所以我正在尝试捕获音频,然后 return 在 NodeJS 中进行音频直播。我希望响应像这样 example。请注意浏览器如何在媒体播放器中打开文件。
我当前的解决方案看起来像这样,但它会呈现为 "no video with supported format and mime type found"
const express = require("express");
const app = express();
const AudioRecorder = require("node-audiorecorder");
app.get("/stream.wav", function (req, res) {
res.set({
"Content-Type": "audio/wav",
});
audioRecorder.start().stream().pipe(res);
});
如果我先将流写入文件(下面的代码)并使用 VLC 打开所述文件,则实时流有效。它会不断地写入文件,VLC 不会有任何问题。
const fileStream = fs.createWriteStream(fileName, { encoding: "binary" });
// Start and write to the file.
//audioRecorder.start().stream().pipe(fileStream);
我整天都在摸不着头脑,但我被困住了。任何帮助将不胜感激!
我不确定输出的音频格式。是MP3还是WAV?你的回答暗示了两者。在第一种情况下,MIME 类型应该是 audio/mpeg.
还应该用以下任何帮助替换 res.set
吗?
res.setHeader('Content-Type', contentType)
res.writeHead(statusCode,{'Content-Type': contentType})
.
contentType
是"audio/wav",statusCode
应该是200。
我通过像这样添加 fluent-ffmpeg 解决了错误:
var stream = ffmpeg().input(audioRecorder.start().stream()).toFormat("mp3");
app.get("/stream.mp3", function (req, res) {
res.type("mp3");
stream.pipe(res);
});
我需要处理大约 10 秒的延迟。
所以我正在尝试捕获音频,然后 return 在 NodeJS 中进行音频直播。我希望响应像这样 example。请注意浏览器如何在媒体播放器中打开文件。
我当前的解决方案看起来像这样,但它会呈现为 "no video with supported format and mime type found"
const express = require("express");
const app = express();
const AudioRecorder = require("node-audiorecorder");
app.get("/stream.wav", function (req, res) {
res.set({
"Content-Type": "audio/wav",
});
audioRecorder.start().stream().pipe(res);
});
如果我先将流写入文件(下面的代码)并使用 VLC 打开所述文件,则实时流有效。它会不断地写入文件,VLC 不会有任何问题。
const fileStream = fs.createWriteStream(fileName, { encoding: "binary" });
// Start and write to the file.
//audioRecorder.start().stream().pipe(fileStream);
我整天都在摸不着头脑,但我被困住了。任何帮助将不胜感激!
我不确定输出的音频格式。是MP3还是WAV?你的回答暗示了两者。在第一种情况下,MIME 类型应该是 audio/mpeg.
还应该用以下任何帮助替换 res.set
吗?
res.setHeader('Content-Type', contentType)
res.writeHead(statusCode,{'Content-Type': contentType})
.
contentType
是"audio/wav",statusCode
应该是200。
我通过像这样添加 fluent-ffmpeg 解决了错误:
var stream = ffmpeg().input(audioRecorder.start().stream()).toFormat("mp3");
app.get("/stream.mp3", function (req, res) {
res.type("mp3");
stream.pipe(res);
});
我需要处理大约 10 秒的延迟。