【问题标题】:HTML5 solution to upload a webcam/camera video stream to server将网络摄像头/摄像头视频流上传到服务器的 HTML5 解决方案
【发布时间】:2023-07-04 19:27:01
【问题描述】:

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

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

我发现了几个例子:

【问题讨论】:

    标签: html websocket html5-video


    【解决方案1】:

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

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

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

    【讨论】:

    • 你不觉得这太慢了吗?我目前正在使用相同的解决方案,但对于 1 分钟的音频,您需要上传很多内容。
    • 我仍在寻找流式传输解决方案,因为我可以在字节到达后立即发送。
    • navigator.getUserMedia 已被弃用并替换为 MediaDevices.getUserMedia,请参阅 developer.mozilla.org/en-US/docs/Web/API/MediaDevices/…
    • 我想通过 javascript 中的 web Socket 发送视频数据。在您的解决方案中是否有执行此操作的选项.. 提前致谢..
    • @susan097 你可以从运行相机提要的html 5 canva获取图像数据并通过套接字发送到服务器,它会非常快,我知道它的位带宽消耗解决方案是在我的项目中这样做,感觉就像是视频流,因为在另一端我正在获取这些图像并将其写回画布
    【解决方案2】:

    看看这篇文章:http://www.smartjava.org/content/face-detection-using-html5-javascript-webrtc-websockets-jetty-and-javacvopencv

    它显示了Webrtc的用法:

    这些 API 应该能够构建可以在浏览器中运行的应用程序,不需要额外的下载或插件,允许各方之间使用音频、视频和补充实时通信进行通信,而无需使用干预服务器(除非需要防火墙穿越,或用于提供中介服务)。

    【讨论】:

    • 我看到了这个。它类似于我在问题中提到的示例 2。来自文章:The next step is to grab the image from the canvas, convert it to binary, and send it over a websocket。问题是我也需要音频,所以这对我不起作用。
    • 但似乎音频也可用:smartjava.org/content/…