How to record a video using webrtc

2019-05-10 10:15发布

I need to record a video using the laptop camera on my website built using nodejs. For this I am using webRTC. So far I could take a photo using the laptop camera but I need to record a video. Could some one help as to how the code would go? My current code is as follows:

<video id="video"></video>
<button id="startbutton">Take photo</button>
<button id="pausebutton">Pause</button>
<button id="resumebutton">Resume</button>
<canvas id="canvas"></canvas>

<script type="text/javascript">
(function() {

  var streaming = false,
      video        = document.querySelector('#video'),
      canvas       = document.querySelector('#canvas'),
      //photo        = document.querySelector('#photo'),
      startbutton  = document.querySelector('#startbutton'),
      width = 620,
      height = 50;

  navigator.getMedia = ( navigator.getUserMedia ||
                         navigator.webkitGetUserMedia ||
                         navigator.mozGetUserMedia ||
                         navigator.msGetUserMedia);

  navigator.getMedia(
    {
      video: true,
      audio: false
    },
    function(stream) {
      if (navigator.mozGetUserMedia) {
        video.mozSrcObject = stream;
      } else {
        var vendorURL = window.URL || window.webkitURL;
        video.src = vendorURL.createObjectURL(stream);
      }
      video.play();
    },
    function(err) {
      console.log("An error occured! " + err);
    }
  );

  video.addEventListener('canplay', function(ev){
    if (!streaming) {
      height = video.videoHeight / (video.videoWidth/width);
      video.setAttribute('width', width);
      video.setAttribute('height', height);
      canvas.setAttribute('width', width);
      canvas.setAttribute('height', height);
      streaming = true;
    }
  }, false);

  function takepicture() {
    canvas.width = width;
    canvas.height = height;
    canvas.getContext('2d').drawImage(video, 0, 0, width, height);
    var data = canvas.toDataURL('image/png');
   // photo.setAttribute('src', data);
  }


  function pausevideo() {
    canvas.width = width;
    canvas.height = height;
    video.pause();
  }
  function resumevideo() {
    canvas.width = width;
    canvas.height = height;
    video.play();
  }


  startbutton.addEventListener('click', function(ev){
      takepicture();
    ev.preventDefault();
  }, false);

  pausebutton.addEventListener('click', function(ev){
      pausevideo();
    ev.preventDefault();
  }, false);

  resumebutton.addEventListener('click', function(ev){
      resumevideo();
    ev.preventDefault();
  }, false);


})();
</script>

标签: webrtc
1条回答
孤傲高冷的网名
2楼-- · 2019-05-10 10:37

I am not going to write code for you(you seem pretty capable if you have gotten this far) but here are some pointers to get you in the right direction.

  • Assign a global variable the value of the stream when you grab it(this way you can reuse the same stream in numerous functions
  • Once you have the stream you can easily follow the tutorials at RTC-Recording, there is a write to disk method that should help you out in downloading the recording

If you have a stream, this is how to start using RecordRTC.

   var options = {
      type: 'video'
    };
    var recordRTC = RecordRTC(mediaStream, options);
    recordRTC.startRecording();
    recordRTC.stopRecording(function(videoURL) {
        mediaElement.src = videoURL; //plays the recorded blob url on that src
    });
查看更多
登录 后发表回答