将网络摄像头/相机视频流上传到服务器的 HTML5 解决方案

标签 html websocket html5-video

使用 getUserMedia我可以从客户的网络摄像头/相机捕获视频流。并使用 video 标签,我可以在客户端的浏览器上显示它。代码:

<video autoplay></video>

<script type="text/javascript">
    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

    var video = $('video')[0];

    var failed = function(e) {
        console.log('Denied!', e);
    };

    if( navigator.getUserMedia ) {
        navigator.getUserMedia( {video: true, audio: true}, function( stream ) {
                video.src = window.URL.createObjectURL(stream);
            }, failed
        )
    } else {
        console.log( 'Not supported!' );
    }
</script>

现在是否可以将此视频流作为实时源或在用户完成录制并决定上传后发送到服务器?

我找到了几个例子:

最佳答案

MediaStreamRecorder 是一个用于记录 getUserMedia() 流的 WebRTC API。它允许网络应用程序从实时音频/视频 session 创建文件。

 <video autoplay></video>

    <script language="javascript" type="text/javascript">
    function onVideoFail(e) {
        console.log('webcam fail!', e);
      };

    function hasGetUserMedia() {
      // Note: Opera is unprefixed.
      return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia || navigator.msGetUserMedia);
    }

    if (hasGetUserMedia()) {
      // Good to go!
    } else {
      alert('getUserMedia() is not supported in your browser');
    }

    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia  = navigator.getUserMedia || 
                             navigator.webkitGetUserMedia ||
                              navigator.mozGetUserMedia || 
                               navigator.msGetUserMedia;

    var video = document.querySelector('video');
    var streamRecorder;
    var webcamstream;

    if (navigator.getUserMedia) {
      navigator.getUserMedia({audio: true, video: true}, function(stream) {
        video.src = window.URL.createObjectURL(stream);
        webcamstream = stream;
    //  streamrecorder = webcamstream.record();
      }, onVideoFail);
    } else {
        alert ('failed');
    }

    function startRecording() {
        streamRecorder = webcamstream.record();
        setTimeout(stopRecording, 10000);
    }
    function stopRecording() {
        streamRecorder.getRecordedData(postVideoToServer);
    }
    function postVideoToServer(videoblob) {

        var data = {};
        data.video = videoblob;
        data.metadata = 'test metadata';
        data.action = "upload_video";
        jQuery.post("http://www.kongraju.in/uploadvideo.php", data, onUploadSuccess);
    }
    function onUploadSuccess() {
        alert ('video uploaded');
    }

    </script>

    <div id="webcamcontrols">
        <button class="recordbutton" onclick="startRecording();">RECORD</button>
    </div>

规范:

http://www.w3.org/TR/mediastream-recording/

您可以将录制的文件发送到服务器。

关于将网络摄像头/相机视频流上传到服务器的 HTML5 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431251/

相关文章:

html - 覆盖在响应式视频之上

javascript - Onclick 播放多个视频的 Jquery 触发器

html - 我如何在我的 wordpress 网站的帖子中实现这种样式

javascript - 从 javascript 更改 css 时,CSS 转换不起作用

html - 在 IE8 中折叠 DIV

javascript - 路由中具有变量/id的Socket.io命名空间

c# - ASP.NET SignalR - 空闲时消耗的带宽

android - 当我在 android webview 上加载时 html 文件中的图像路径

sockets - 是否可以仅通过客户端证书验证而不是服务器证书验证来建立 ssl 握手?

javascript - 将 YouTube/Vimeo 视频转换为无声的动画图像(想想哈利波特中的 "moving pictures")?