javascript - 是否可以将 MediaRecorder API 与 html5 视频一起使用?

标签 javascript html html5-video mediarecorder

我想录制视频标签,以便可以将 blob 数据流式传输到 websocket 服务器,但是,当我尝试启动 mediaRecorder 时,出现以下错误:

The MediaRecorder failed to start because there are no audio or video tracks available.

是否可以将 html5 视频标签中的音频/视频轨道添加到媒体流中?

<script>
var video = document.getElementById('video');
var mediaStream = video.captureStream(30);
var mediaRecorder = new MediaRecorder(
            mediaStream,
            {
                mimeType: 'video/webm;codecs=h264',
                videoBitsPerSecond: 3000000
            }
        );

fileElem.onchange = function () {
            var file = fileElem.files[0],
                canplay = !!video.canPlayType(file.type).replace("no", ""),
                isaudio = file.type.indexOf("audio/") === 0 && file.type !== "audio/mpegurl";
            video.src = URL.createObjectURL(file);
            video.play();
            mediaRecorder.start(1000); // Start recording, and dump data every second
        };
</script>

<p id="choice" class="info"><input type="file" id="file"> File type: <span id="ftype"></span></p>
<video width="640" height="360" id="video" src="" controls="false"></video>

最佳答案

Is it possible to use the MediaRecorder API with html5 video?

,但是您需要在 HTMLVideoElement.readyState has metadata 之后初始化 MediaRecorder .

这是一个示例,但它仅在视频源来自同一来源 时有效(因为 captureStream 无法从具有跨来源数据的元素中捕获)

注意:在这个示例中,我使用onloadedmetadata在视频获取元数据后初始化MediaRecorder

var mainVideo = document.getElementById("mainVideo"),
  displayVideo = document.getElementById("displayVideo"),
  videoData = [],
  mediaRecorder;

var btnStart = document.getElementById("btnStart"),
  btnStop = document.getElementById("btnStop"),
  btnResult = document.getElementById("btnResult");

var initMediaRecorder = function() {
  // Record with 25 fps
  mediaRecorder = new MediaRecorder(mainVideo.captureStream(25));

  mediaRecorder.ondataavailable = function(e) {
    videoData.push(e.data);
  };
}

function startCapture() {
  videoData = [];
  mediaRecorder.start();

  // Buttons 'disabled' state
  btnStart.setAttribute('disabled', 'disabled');
  btnStop.removeAttribute('disabled');
  btnResult.setAttribute('disabled', 'disabled');
};

function endCapture() {
  mediaRecorder.stop();

  // Buttons 'disabled' state
  btnStart.removeAttribute('disabled');
  btnStop.setAttribute('disabled', 'disabled');
  btnResult.removeAttribute('disabled');
};

function showCapture() {
  return new Promise(resolve => {
    setTimeout(() => {
      // Wrapping this in setTimeout, so its processed in the next RAF
      var blob = new Blob(videoData, {
          'type': 'video/mp4'
        }),
        videoUrl = window.URL.createObjectURL(blob);

      displayVideo.src = videoUrl;
      resolve();
    }, 0);
  });
};
video {
  max-width: 300px;
  max-height: 300px;
  display: block;
  margin: 10px 0;
}
<video id="mainVideo" playsinline="" webkit-playsinline="1" controls="1" onloadedmetadata="initMediaRecorder()">
    <source src="sampleVideo.mp4" type="video/mp4">
  </video>

<button id="btnStart" onclick="startCapture()"> Start </button>
<button id="btnStop" disabled='disabled' onclick="endCapture()"> Stop </button>
<button id="btnResult" disabled='disabled' onclick="showCapture()"> Show Result </button>

<video id="displayVideo" playsinline="" webkit-playsinline="1" controls="1"></video>

关于javascript - 是否可以将 MediaRecorder API 与 html5 视频一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52720894/

相关文章:

javascript - 重新加载 html5 视频标签的源在 chrome 上不起作用

javascript - 检查 div 是否在幻灯片上有图像

javascript - 单击 anchor 标记更改页面标题

css - 在 Firefox 中不遵守 SVG 的百分比变换原点,仅在 WebKit 中有时才遵守

css - 无法通过 iPhone 上的 HTML5 视频元素访问文本字段

javascript - 滑动幻灯片内图像周围出现奇怪的灰色边框

javascript - 增加 d3.js 中的属性值

javascript - 创建从侧面滑入和滑出的动画 div

javascript - Angularjs:在 ng-repeat 中过滤多个条件

html - 如何在 css/html 中获取图像上的文本?