Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将本地视频文件转换为媒体流_Javascript_Webrtc - Fatal编程技术网

Javascript 将本地视频文件转换为媒体流

Javascript 将本地视频文件转换为媒体流,javascript,webrtc,Javascript,Webrtc,我想尝试使用webRTC进行文件流传输。它与camera/micro live(getUserMedia)完美配合,但当我尝试给它一个预录视频文件时,我得到了以下答案: Uncaught TypeError: Failed to execute 'addStream' on 'RTCPeerConnection': parameter 1 is not of type 'MediaStream'. 是否有一种简单的方法可以将RTPeerConnection与.getUserMedia()等选定

我想尝试使用webRTC进行文件流传输。它与camera/micro live(getUserMedia)完美配合,但当我尝试给它一个预录视频文件时,我得到了以下答案:

Uncaught TypeError: Failed to execute 'addStream' on 'RTCPeerConnection': parameter 1 is not of type 'MediaStream'.
是否有一种简单的方法可以将RTPeerConnection与.getUserMedia()等选定文件一起使用

编辑:

下面是我获取文件的方式,但当我尝试使用RTPeerConnection的addStream方法添加“localStream”时,它失败了

var localVideo = document.createElement("video");
var remoteVideo = document.createElement("video");

function onchange( event ){

    var file = this.files[0];
    var type = file.type;

    if( localVideo.canPlayType(type) ){

        var localStream = window.URL.createObjectURL(file);

        localVideo.src = localStream;

        ...

        peer.addStream(localStream); // fail here

    };

};
a) createObjectURL是邪恶的。在应用程序导航离开之前,您永远无法知道应用程序何时完成了对资源的处理。(和其他问题)使用video.srcObject(当前为video.mozSrcObject)

b) 仅仅因为视频元素可以为流媒体文件使用src URL,并不意味着其他任何东西都可以。AddStream采用MediaStreams,而不是URL

工作组计划添加stream=video.captureStream(),但它甚至还没有被指定

更新:规范如下:
它在Firefox和Chrome 53及更高版本中实现(请参见)

使用您需要的视频创建一个
视频元素:

<video controls src="http://example.com/example.webm">

这与
音频
元素的工作方式相同。

您如何尝试使用本地视频文件?你能把密码寄出去吗?FireFox有一个内置的对象,但Chrome没有。你找到办法了吗?我需要的正是这个功能。更新与规范和例子链接的答案
const video = document.querySelector('video');
video.onplay = function() {
  const stream = video.captureStream();
  peer.addStream(stream);
};