如何从浏览器向服务器提交/流式传输视频?

时间:2013-06-12 15:34:23

标签: video-streaming html5-video webrtc getusermedia

我可以在浏览器中使用getUserMedia()录制视频。但是,我没有找到一种方便的方法从浏览器向服务器提交(录制)或流媒体(实时)视频。

我发现只有将视频渲染到画布然后提交或流式传输渲染图像,例如通过数据uri。 (这没有效果。)

有更好的方法吗? (例如,直接流式传输二进制数据或将它们存储在文件中,然后发送此文件。)

更新:我发现了类似的旧问题:Stream getUserMedia to an Icecast server?

2 个答案:

答案 0 :(得分:2)

MediaStreamRecorder是用于记录getUserMedia()流的WebRTC API。它允许Web应用程序通过实时音频/视频会话创建文件。

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

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

答案 1 :(得分:0)

目前它不是任何浏览器的一部分,但至少会在Chrome中出现。查看http://code.google.com/p/chromium/issues/detail?id=113676