简体   繁体   English

您可以合并 canvas 和视频元素并使用 mediarecorder 记录 output 吗?

[英]Can you merge a canvas and video element and record the output using mediarecorder?

I have an app that allows me to draw on a canvas, and play a video under the canvas element.我有一个应用程序,允许我在 canvas 上绘图,并在 canvas 元素下播放视频。 So onscreen I see a video being played with my drawings on top of it with a canvas.所以在屏幕上我看到一个视频正在播放,上面有我的图纸,上面有 canvas。

I can use the following code to create an image of the canvas drawing on top of the video:我可以使用以下代码在视频顶部创建 canvas 绘图的图像:

var tempcanvasDrawing = document.getElementById("drawcanvas");
var img = new Image();
img.src = tempcanvasDrawing.toDataURL();
img.onload = function() {
    var tempcanvasFrame = document.createElement("canvas");
    tempcanvasFrame.width = 1660;
    tempcanvasFrame.height = 925;
    var ctx = tempcanvasFrame.getContext('2d');
    var video = $("video").get(1);  
    ctx.drawImage(video, 0, 0, 1660, 925);
    ctx.drawImage(img, 0, 0, 1660, 925);            
    var dataURL = tempcanvasFrame.toDataURL('image/jpeg');
    $.post('img_upload.php', {
        imgBase64: dataURL,
        userid: window.userid
    }, function(o) {
    }); 
}

I am now trying to find a way to record my drawing and video in reatime and export it to a video.我现在正试图找到一种方法来实时录制我的绘图和视频并将其导出到视频中。

I've played a bit with mediarecorder and can record the canvas but i am really struggling to find a way to combine the video and canvas like I do above into a stream where I can record with mediarecorder.我用 mediarecorder 玩了一点,可以录制 canvas,但我真的很难找到一种方法将视频和 canvas 像我上面做的那样组合成 stream 可以用 mediarecorder 录制的地方

Has anyone been able to do this kind of thing?有没有人能够做这种事情?

With help from @dandavid and TyTy396 i managed to get a little further:在@dandavid 和 TyTy396 的帮助下,我设法更进一步:

I can capture all the streams I need but am having trouble linking them together.我可以捕获我需要的所有流,但无法将它们链接在一起。

async function startRecording() {
    let types = [
        "video/webm", 
        "audio/webm", 
        "video/webm\;codecs=vp8", 
        "video/webm\;codecs=daala", 
        "video/webm\;codecs=h264", 
        "audio/webm\;codecs=opus", 
        "video/mpeg"
    ];
    for (let i in types) {
        if (MediaRecorder.isTypeSupported(types[i])) {
            supportedType = types[i];
            break;
        }
    }
    if (supportedType == null) {
        console.log("No supported type found for MediaRecorder");
    }
    let options = { 
        mimeType: supportedType,
        audio: true,
        audioBitsPerSecond: 64000,
        videoBitsPerSecond: 1750000, // 1.75Mbps
    };

    //assign inputs to be captured
    stream['canvas'] = document.getElementById('whiteboard'); //This is the drawing canvas
    stream['input1'] = $("video").get(1); //this is the video behind tha canvas
    stream['input2'] = stream['canvas'].captureStream(30); //capture canvas stream

    //compensate for firefox
    if( browserName == "firefox"){
        stream['input1'] = $("video").get(1).mozCaptureStream(30); //capture video stream using mozcapture
    } else {
        stream['input1'] = $("video").get(1).captureStream(30); //capture video stram using capture
    }

    stream['obj'] = stream['input1'];
    //stream['obj'].addTrack(stream['audio'].getAudioTracks()[0]);

    //stream['output'].srcObject = stream['obj']; //preview video

    recordedBlobs = [];
    try {
        stream['mediaRecorder'] = new MediaRecorder(stream['obj'], options); //can i add all streams
    } catch (e) {
        alert('MediaRecorder is not supported by this browser.');
        console.error('Exception while creating MediaRecorder:', e);
        return;
    }
    stream['mediaRecorder'].onstop = handleStop;
    stream['mediaRecorder'].ondataavailable = handleDataAvailable;
    stream['mediaRecorder'].start(100); // collect 100ms of data blobs
    timer.reset();
    timer.start( {target: {minutes: 30} });
    timer.addEventListener('secondsUpdated', function (e) {
        $('.record-menu-time').text(timer.getTimeValues().toString());
    });     
    timer.addEventListener('targetAchieved', function (e) {
        $(".record-menu-stop").trigger("click");
    });     
}

function handleDataAvailable(event) {
    if (event.data && event.data.size > 0) {
        recordedBlobs.push(event.data);
    }
}

function handleStop(event) {
    stream['superBuffer'] = new Blob(recordedBlobs, { type: supportedType });
    stream['blobUrl'] = window.URL.createObjectURL(stream['superBuffer']);
    var preview = document.createElement("video");
    preview.src = stream['blobUrl'];
    preview.controls = true;
    preview.load();
    $( "#screen-recorder-video" ).replaceWith( preview );
    preview.id = "screen-recorder-video";
    $(preview).addClass("screen-recorder-video");
    $(".record-menu-preview-show").hide();  
    $(".record-menu-preview-hide").show();  
    $(".video-preview").show();
    $(".hide-preview").show();
    $(".show-preview").hide();      
}

function stopRecording() {
    stream['mediaRecorder'].stop();
    stream['output'].controls = true;
    timer.stop();
}

function pauseRecording() {
    console.log("pause recording");
    timer.pause();
    stream['mediaRecorder'].pause();
}

function resumeRecording() {
    console.log("resume recording");
    stream['mediaRecorder'].resume();
    timer.start();
}

Maybe consider using the <video> element?也许考虑使用<video>元素?

var video = document.createElement('video');
video.setAttribute('playsinline', '');
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
video.style.width = screenX + 'px;';
video.style.height = screenY + 'px;';
document.body.appendChild(video);

var facingMode = "user";
var constraints = {
  audio: true,
  video: {
   facingMode: facingMode
  }
};

var s;
navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
  video.srcObject = stream;
});

Hope this helps!希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML 使用 MediaRecorder 记录 canvas 里面有一个视频 - HTML using MediaRecorder record canvas with a video inside 如何使用 MediaRecorder 将 canvas 录制到与另一个视频完全相同的持续时间? - How can I record a canvas with MediaRecorder to the exact same duration as another video? 是否可以使用JavaScript使用MediaRecorder对屏幕进行视频录制? - Is it possible to video record the screen using MediaRecorder using javascript? 使用MediaRecorder将HTML canvas元素记录到视频中会导致视频损坏,并且只能在Chrome中使用 - Recording an HTML canvas element to video with MediaRecorder results in a corrupted video and only works in Chrome <video>使用 MediaRecorder() 从<canvas>使用 canvas.captureStream() 在 firefox、chrome 上呈现不同 - <video> playback of recorded stream using MediaRecorder() from <canvas> using canvas.captureStream() renders differently at firefox, chromium 你能用一个 HTML5<canvas> Video.js 视频的元素?</canvas> - Can you use a HTML5 <canvas> element for Video.js video? 如何录制带有视频的画布动画? - How can i record a canvas animation with a video? 使用captureStream和mediaRecorder进行画布录制 - Canvas recording using captureStream and mediaRecorder 使用 MediaRecorder 记录多个 canvas - Recording multiple canvas using MediaRecorder 使用 NodeJS 将 MediaRecorder 块合并到一个视频文件中 - Merge MediaRecorder chunks into a single video file with NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM