Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit/stream video from browser to a server?

I can record video using getUserMedia() in a browser. However, I have not find a convenient way to submit (recorded) or stream (live) video from browser to a server.

Only what I've found is to render video to canvas and then submit or stream rendered images e.g. by data uri. (Which is not effective.)

Is there a better way? (For instance, stream directly the binary data or store them in a file and then send this file.)

UPDATE: I have found similar old question: Stream getUserMedia to an Icecast server?

like image 381
TN. Avatar asked Jun 12 '13 15:06

TN.


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.foundthru.co.uk/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.

like image 148
kongaraju Avatar answered Nov 09 '22 10:11

kongaraju