如何从网络摄像头捕获视频并将其上传到服务器?

时间:2013-11-11 11:05:04

标签: javascript html5 getusermedia

我尝试使用getusermedia录制网络摄像头视频。

我按照这个链接

How to capture the video to the server using getusermedia

我们如何知道视频是否真正被录制。 webcamstream.record是否正常运行?

3 个答案:

答案 0 :(得分:1)

  

function fallback(e){video.src ='fallbackvideo.webm'; }

     

功能成功(流){video.src =   window.URL.createObjectURL(流); }

     

if(!navigator.getUserMedia){fallback();其他{
  navigator.getUserMedia({video:true},success,fallback); }

您可以在此处获取有关它的更多信息: http://www.html5rocks.com/en/tutorials/getusermedia/intro/

答案 1 :(得分:0)

您链接的问题中的示例使用从画布中定期拍摄的帧/图片,最后使用Whammy转换为webm。生成的视频没有声音。

幸运的是,您现在可以使用两者:

  • 客户端 Media Recorder API
  • 服务器端 WebRTC流记录

两者都依赖于浏览器的原生视频/音频编码功能,并得到Chrome和Firefox的广泛支持。

有关详细信息/代码/链接,请参阅this answer

答案 2 :(得分:0)

这里是工作正常的代码,用于录制视频并将其保存到本地以及发送到服务器..............: 注意:根据您的需要编辑此代码...........

 <html>
        <div class="left">
            <div id="startButton" class="button">
            Start
            </div>
            <h2>Preview</h2>
            <video id="preview" width="160" height="120" autoplay muted></video>
        </div>

        <div class="right">
            <div id="stopButton" class="button">
            Stop
            </div>
            <h2>Recording</h2>
            <video id="recording" width="160" height="120" controls></video>
            <a id="downloadButton" class="button">
            Download
            </a>
        </div>

        <script>

        let preview = document.getElementById("preview");
        let recording = document.getElementById("recording");
        let startButton = document.getElementById("startButton");
        let stopButton = document.getElementById("stopButton");
        let downloadButton = document.getElementById("downloadButton");
        let logElement = document.getElementById("log");

        let recordingTimeMS = 5000;


        function log(msg) {
            //logElement.innerHTML += msg + "\n";
        }

        function wait(delayInMS) {
            return new Promise(resolve => setTimeout(resolve, delayInMS));
        }

        function startRecording(stream, lengthInMS) {
            let recorder = new MediaRecorder(stream);
            let data = [];

            recorder.ondataavailable = event => data.push(event.data);
            recorder.start();
            log(recorder.state + " for " + (lengthInMS/1000) + " seconds...");

            let stopped = new Promise((resolve, reject) => {
            recorder.onstop = resolve;
            recorder.onerror = event => reject(event.name);
            });

            let recorded = wait(lengthInMS).then(
            () => recorder.state == "recording" && recorder.stop()
            );

            return Promise.all([
                stopped,
                recorded
            ])
            .then(() => data);
        }

        function stop(stream) {
            stream.getTracks().forEach(track => track.stop());
        }

        startButton.addEventListener("click", function() {
            navigator.mediaDevices.getUserMedia({
                video: true,
                audio: false
            }).then(stream => {
                    preview.srcObject = stream;
                    downloadButton.href = stream;
                    preview.captureStream = preview.captureStream || preview.mozCaptureStream;
                    return new Promise(resolve => preview.onplaying = resolve);
                  }).then(() => startRecording(preview.captureStream(), recordingTimeMS))
                  .then (recordedChunks => {
                    let recordedBlob = new Blob(recordedChunks, { type: "video/webm" });

                 //upload it to server part start............................

/*you have **recordedBlob**  data that is nothing but video data which is getting recorded you can send this data directly to server */

                 //Here I am writing sample code to send it to server
                  //making request
                   var xhr = new XMLHttpRequest();
                   //creating form data to append files
                   var fd = new FormData();
                   //append the recorded blob
                   fd.append("video",recordedBlob);
                 //send data to server..............
                   xhr.send(fd);

                 //upload it to server part finish............................

                    recording.src = URL.createObjectURL(recordedBlob);  
                    downloadButton.href = recording.src;
                    downloadButton.download = "RecordedVideo.webm";

                    log("Successfully recorded " + recordedBlob.size + " bytes of " +
                        recordedBlob.type + " media.");
                  })
                  .catch(log);
            }, false);


            stopButton.addEventListener("click", function() {
            stop(preview.srcObject);
            }, false);

        </script>
    </html>