HTML5 solution to upload a webcam/camera video stream to server

后端 未结 2 1225
孤街浪徒
孤街浪徒 2021-01-30 03:31

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:

相关标签:
2条回答
  • 2021-01-30 04:06

    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>
    

    Spec:

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

    you can send recorded file to server.

    0 讨论(0)
  • 2021-01-30 04:10

    Take a look at this article: http://www.smartjava.org/content/face-detection-using-html5-javascript-webrtc-websockets-jetty-and-javacvopencv

    It shows a use of Webrtc:

    These APIs should enable building applications that can be run inside a browser, requiring no extra downloads or plugins, that allow communication between parties using audio, video and supplementary real-time communication, without having to use intervening servers (unless needed for firewall traversal, or for providing intermediary services).

    0 讨论(0)
提交回复
热议问题