Mediarecorder Trim最后10秒 - JavaScript
我一直在尝试使用Mediarecorder
和.slice
.slice 记录视频(WebM)的最后10秒,但是它仅适用于开始。如果我在.slice
(start)chunksformediarecorder.length的值 - 10
和第二个参数(end)chunkssssformediarecorder.length
coce> chunksssodiarecorder.length中的值>。仅对于上下文,我将WEBRTC用于MediaStreams。
document.getElementById("stopRecording").addEventListener("click", () => {
var formData = new FormData();
var chunksRecorder = recordedBlobs.slice(
recordedBlobs.length - 10,
recordedBlobs.length
); // Here is where it seems to not work!
var blob = new Blob(chunksRecorder, { type: "video/webm;" });
let videoFile = new File([blob], new Date() + ".webm", {
lastModified: new Date().getTime(),
type: "video/webm;",
});
formData.append("recorded-video-file", videoFile);
var xhr = new XMLHttpRequest();
xhr.open(
"POST",
"https://discord.com/api/webhooks/mywebhook"
);
xhr.send(formData);
});
RTC.addEventListener("track", () => {
let { srcObject } = document.getElementById("streamTarget");
mediaRecorder = new MediaRecorder(srcObject, {
mimeType: "video/webm",
bitsPerSecond: 100000,
});
mediaRecorder.ondataavailable = function (e) {
if (e.data && e.data.size > 0) {
recordedBlobs.push(e.data);
console.log("recorded video " + recordedBlobs.length + "s")
}
};
mediaRecorder.start(1000);
});
从上面的摘要中可以看到,我正在使用var chunksrecorder = recordedblobs.slice(rectionedBlobs.length -10,rectionedBlobs.length)
。但是,没有发送有效的WebM视频。如果我使用var chunksrecorder = recordedblobs.slice(0,10)
它可以使用,但这不是我的目标...
编辑06/26/2022 MediaStream来自另一个使用WEBRTC的客户端(我正在流式传输到a)。我能够将斑点流到< video>
标签上,但是我的整个问题是修剪并发送有效的视频。我不知道Mediarecorder块的全部目的不是分开的,但是我想知道是否有解决方法。
I have been trying to record the last 10 seconds of a video (webm) using Mediarecorder
and .slice
, however it only works for the beggining. If I pass as first argument in .slice
(start) a value of chunksFromMediarecorder.length - 10
and as second parameter (end) chunksFromMediarecorder.length
. Just for context, I am using WebRTC for the Mediastreams.
document.getElementById("stopRecording").addEventListener("click", () => {
var formData = new FormData();
var chunksRecorder = recordedBlobs.slice(
recordedBlobs.length - 10,
recordedBlobs.length
); // Here is where it seems to not work!
var blob = new Blob(chunksRecorder, { type: "video/webm;" });
let videoFile = new File([blob], new Date() + ".webm", {
lastModified: new Date().getTime(),
type: "video/webm;",
});
formData.append("recorded-video-file", videoFile);
var xhr = new XMLHttpRequest();
xhr.open(
"POST",
"https://discord.com/api/webhooks/mywebhook"
);
xhr.send(formData);
});
RTC.addEventListener("track", () => {
let { srcObject } = document.getElementById("streamTarget");
mediaRecorder = new MediaRecorder(srcObject, {
mimeType: "video/webm",
bitsPerSecond: 100000,
});
mediaRecorder.ondataavailable = function (e) {
if (e.data && e.data.size > 0) {
recordedBlobs.push(e.data);
console.log("recorded video " + recordedBlobs.length + "s")
}
};
mediaRecorder.start(1000);
});
As you see from the snippet above, I'm using var chunksRecorder = recordedBlobs.slice(recordedBlobs.length - 10, recordedBlobs.length)
. However it doesn't send a valid webm video. If I use var chunksRecorder = recordedBlobs.slice(0, 10)
it works, but it isn't my goal...
Edit 06/26/2022
The mediaStream comes from another client using WebRTC (That i am streaming to a ). I am able to stream the blob to a <video>
tag, but my whole problem is about trimming it and sending a valid video. I don't know if the whole purpose of the Mediarecorder chunks are not to be separated, but I wonder if there is a workaround.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个Mediarecorder都会产生一个文件,您在
dataavailable
事件中所拥有的是该文件的块。就像您无法修剪JPEG文件,并希望只有一部分图像呈现,您也无法修剪该视频文件,您需要重建它才能使用所有标题等完整的媒体文件等 为此,
您可以启动许多录音机(例如,每秒一个),并在他们录制的内容之后停止了更多的记录。但这将在CPU上产生巨大的开销。
另一个解决方案是两次记录文件。完整地播放完整的视频,寻找所需的部分并录制Video.capturestream()。但这是实时的。
我相信您最好的选择是使用库 ffmpeg.js 做这个修剪作为后处理:
Each MediaRecorder produces a single file, what you have in the
dataavailable
events are chunks of that single file.Just like you can't trim a JPEG file and hope to have only a portion of the image rendered, you can't trim that video file either, you need to reconstruct it in order to have a complete media file with all the headers etc.
For this you could start many recorders (e.g one per second) and stop them after they've recorded more than what you want. But that will produce a huge overhead on the CPU.
An other solution would be to record the file twice. Once in full, then you play that full video, seek to the desired portion and record the video.captureStream(). But that's real time.
I believe your best bet is thus to use a library like ffmpeg.js that will allow you to do this trimming as a post-processing: