gpt4 book ai didi

javascript - 如何使用 ffmpeg 通过媒体记录器 api 从 block 中创建视频文件 webm

转载 作者:行者123 更新时间:2023-12-04 22:58:12 28 4
gpt4 key购买 nike

我正在尝试使用 FFMPEG 从 NodeJS 服务器中 MediaRecorderAPI 生成的 blob 创建一个 webm 视频文件。我可以创建 .webm 文件,但它不能播放,我运行了这个命令 $ ffmpeg.exe -v error -i lel.webm -f null - >error.log 2>&1 要生成错误日志,错误日志文件包含以下内容:

[null @ 000002ce7501de40] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1 >= 1

[h264 @ 000002ce74a727c0] Invalid NAL unit size (804 > 74).

[h264 @ 000002ce74a727c0] Error splitting the input into NAL units.

Error while decoding stream #0:0: Invalid data found when processing input


这是我的网络服务器代码
const app = require("express")();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const fs = require("fs");
const child_process = require("child_process");

app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});

io.on("connection", (socket) => {
console.log("a user connected");

const ffmpeg = child_process.spawn("ffmpeg", [
"-i",
"-",
"-vcodec",
"copy",
"-f",
"flv",
"rtmpUrl.webm",
]);

ffmpeg.on("close", (code, signal) => {
console.log(
"FFmpeg child process closed, code " + code + ", signal " + signal
);
});

ffmpeg.stdin.on("error", (e) => {
console.log("FFmpeg STDIN Error", e);
});

ffmpeg.stderr.on("data", (data) => {
console.log("FFmpeg STDERR:", data.toString());
});

socket.on("message", (msg) => {
console.log("Writing blob! ");
ffmpeg.stdin.write(msg);
});

socket.on("stop", () => {
console.log("Stop recording..");
ffmpeg.kill("SIGINT");
});
});

http.listen(3000, () => {
console.log("listening on *:3000");
});

这是我的客户端代码,使用 HTML、JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
let mediaRecorder = null;
const startRecording = (someStream) => {
const mediaStream = new MediaStream();
const videoTrack = someStream.getVideoTracks()[0];
const audioTrack = someStream.getAudioTracks()[0];
console.log("Video trac ", videoTrack);
console.log("audio trac ", audioTrack);
mediaStream.addTrack(videoTrack);
mediaStream.addTrack(audioTrack);

const recorderOptions = {
mimeType: "video/webm;codecs=h264",
videoBitsPerSecond: 3 * 1024 * 1024,
};

mediaRecorder = new MediaRecorder(mediaStream, recorderOptions);
mediaRecorder.start(1000); // 1000 - the number of milliseconds to record into each Blob
mediaRecorder.ondataavailable = (event) => {
console.debug("Got blob data:", event.data);
if (event.data && event.data.size > 0) {
socket.emit("message", event.data);
}
};
};

const getVideoStream = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
});
startRecording(stream);
myVideo.srcObject = stream;
} catch (e) {
console.error("navigator.getUserMedia error:", e);
}
};

const stopRecording = () => {
mediaRecorder.stop();
socket.emit("stop");
};
</script>
<body>
<p>hello world</p>
<button onclick="getVideoStream()">start rec</button>
<button onclick="stopRecording()">stop rec</button>

<video width="300" height="300" autoplay id="myvideo" />
<script>
const myVideo = document.getElementById("myvideo");
myVideo.muted = true;
</script>
</body>
</html>

任何帮助表示赞赏!

最佳答案

看起来您正在编码为 flv,而不是 webm。所以你在 webm 容器中有 flv 编码的视频 - 播放设备只是不知道如何处理:)
在您计算机上的 mp4 上尝试上面的命令 - 我敢打赌它不会工作。要编码成 webm - 查看这些 ffmpeg commands

关于javascript - 如何使用 ffmpeg 通过媒体记录器 api 从 block 中创建视频文件 webm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64371135/

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