HTML5解决方案,用于将网络摄像头/摄像头视频流上传到服务器 [英] HTML5 solution to upload a webcam/camera video stream to server

查看:861
本文介绍了HTML5解决方案,用于将网络摄像头/摄像头视频流上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using getUserMedia I can capture video stream from client's webcam/camera. And using video tag I can show it on client's browser. Code:

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

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

Now is it possible to send this video stream, either as a realtime feed or after user has done recording and decided to upload, to a server?

我发现了一些例子:

  • sending binary images to server over websocket
  • Periodically capture frame of streaming video and send that as image

推荐答案

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

MediaStreamRecorder is a WebRTC API for recording getUserMedia() streams . It allows web apps to create a file from a live audio/video 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/

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

you can send recorded file to server.

这篇关于HTML5解决方案,用于将网络摄像头/摄像头视频流上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆