如何使用webrtc录制视频

时间:2014-06-30 19:25:12

标签: webrtc

我需要在使用nodejs构建的网站上使用笔记本电脑相机录制视频。为此,我使用webRTC。到目前为止,我可以使用笔记本电脑相机拍照,但我需要录制视频。有人会帮助代码如何进行吗?我目前的代码如下:

<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>

1 个答案:

答案 0 :(得分:2)

我不会为你编写代码(如果你已经做到这一点,你似乎很有能力)但是这里有一些指示可以让你朝着正确的方向前进。

  • 在抓取时为流全局变量赋值(这样就可以在众多函数中重用相同的流
  • 获得流后,您可以轻松地按照RTC-Recording中的教程进行操作,有一种写入磁盘的方法可以帮助您下​​载录制内容

如果你有一个流,这就是如何开始使用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
    });