Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 

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?

I found few examples of:

  • sending binary images to server over websocket
  • Periodically capture frame of streaming video and send that as image
like image 558
Vikas Avatar asked Nov 17 '12 13:11

Vikas


People also ask

How does HTML5 video streaming work?

HTML5 is not itself a streaming protocol. Websites built with HTML5 can use several different streaming protocols to play video, including HTTP live streaming (HLS) and MPEG-DASH. This is configured on the server side, not in the HTML markup code.

How do you code a camera in HTML?

Inside the <body> tag, create a <main> element with an ID of camera . Inside the <main id=”camera”> , create a <canvas> element with the ID of camera--sensor . Similar to a real camera sensor, the canvas element will grab a frame from the video stream when we tell it to, and draw it to the next element we'll create.

How do I watch HTML5 video?

The webmasters need to use special HTML5 coding and include WebM, MP4 and OGG formats on their web pages. Before HTML5 videos were only played using the Flash Player. You can view HTML5 videos on all popular browsers such as Google Chrome, Internet Explorer, Mozilla Firefox, and Safari.


1 Answers

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.

like image 164
kongaraju Avatar answered Oct 02 '22 15:10

kongaraju