我可以通过Web RTC将视频从一个客户端流式传输到另一个客户端吗?

时间:2015-08-01 07:48:16

标签: javascript webrtc

我知道我可以使用WebRTC在用户之间传输来自摄像头的视频,但Node.js具有以流形式阅读视频的功能,允许大型文件,甚至一小时长的视频流式传输(一个无法读取在Node.js上同时播放一个非常大的视频,我不确定基于浏览器的JS是否有工具将用户选择的视频读入流格式以供WebRTC使用。

可以这样做吗?

2 个答案:

答案 0 :(得分:0)

如果你想在JS中处理mp4,请看:

http://download.tsi.telecom-paristech.fr/gpac/mp4box.js/

答案 1 :(得分:0)

是的,这可以在Firefox中使用FileReaderHTMLMediaElement.captureStream()

完成



browse.onchange = e => {
  var reader = new FileReader();
  reader.readAsDataURL(browse.files[0]);
  reader.onloadend = e => v1.src = reader.result;
};

var pc1 = new mozRTCPeerConnection(), pc2 = new mozRTCPeerConnection();
var add = (p, can) => can && p.addIceCandidate(can).catch(failed);

pc1.onicecandidate = e => add(pc2, e.candidate);
pc2.onicecandidate = e => add(pc1, e.candidate);
pc2.onaddstream = e => v2.mozSrcObject = e.stream;

function send() {
  pc1.addStream(v1.mozCaptureStreamUntilEnded());
  pc1.createOffer()
  .then(offer => pc1.setLocalDescription(offer))
  .then(() => pc2.setRemoteDescription(pc1.localDescription))
  .then(() => pc2.createAnswer())
  .then(answer => pc2.setLocalDescription(answer))
  .then(() => pc1.setRemoteDescription(pc2.localDescription))
  .then(() => log("Connected!"))
  .catch(failed);
}

var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e +", line "+ e.lineNumber);
&#13;
<video id="v1" height="90" width="160" autoplay></video>
<video id="v2" height="90" width="160" autoplay></video><br>
<input type="file" id="browse" accept="video/*"></input>
<button onclick="send()">Send!</button><div id="div"></div>
&#13;
&#13;
&#13;

尝试将this video file保存到您的磁盘中。

这是标准跟踪实验功能。如果你喜欢它,请询问你最喜欢的浏览器供应商吧!