HTML5 getUserMedia record webcam, both audio and video

别说谁变了你拦得住时间么 提交于 2019-11-27 06:41:21

MediaStreamRecorder is a WebRTC API for recording getUserMedia() streams( still under implementation) . It allows web apps to create a file from a live audio/video session.

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

As far as i am aware there is no such way to record audio and video together and save them as one file. it is possible to capture and save the audio as a wav file and the video as a webm file.

here is a great post on how to save your video; http://ericbidelman.tumblr.com/post/31486670538/creating-webm-video-from-getusermedia

and a usefully utillity to save your audio

https://github.com/mattdiamond/Recorderjs

There are currently several production ready solutions for recording audio and video on the web.

Desktop Browsers

MediaRecorder API (HTML)

The MediaRecorder API (MediaStream Recorder) relies on getUserMedia() for webcam access and is supported by Firefox 30+ and Chrome 49+.

Flash client + rtmp media server

You will need a Flash (.swf) application that's embedded in your web page, captures the visitors webcam and mic, encodes the raw video and audio data (using the builtin codecs: H.264, Spark, Nellymoser ASAO and Speex) and streams the data as it is recorded (through rtmp) to a media server.

Because the data is streamed, as soon as you stop the recording, all the data is already on the media sevrer (no upload times). Another advantage is that the video is not lost if the client computer crashes.

You have at least 3 options for the media server:

  1. Red5 is free and open source (I've personally contributed code patches to the recording process in it and I can guarantee it works great)
  2. Wowza ($65/month)
  3. Adobe Media Server Pro ($4500)

The media server receives (again through streaming/rtmp not through http) the data and, depending on the codec used on the client and the media server used, the audio and video data is multiplexed in mp4, flv or f4v files.

This Flash client + media server recording process - which has worked pretty well since Flash Player 6 in 2002.

Mobile browsers

HTML Media Capture

You can use HTML Media Capture (explained here with screenshots) to record video using the device's native video recording app and codecs. HTML Media Capture records locally (on the device) and then it uploads (normal HTTP upload process) the file to the web server.

When using HTML Media Capture in Safari on iOS devices like the iPhone it will create a .mov file that's not playable on Android. The solution is to convert it to .mp4 server side using FFmpeg.

When using HTML Media Capture in the Android browser the end result will be a .mp4 file that's playable on the iPhone. Some older Android phones will create .3gp files.

A commercial solution that implements both (Flash client + media server on desktop and HTML Media Capture on mobile) is HDFVR.

This is my github repo which provides a library for recording audio + video and finally upload the content to the server as chunks

  1. This supports chrome, Opera
  2. Uploading time is reduced, since blobs are sliced and uploaded

Html_Audio_Video_Recorder

Use the following:

<input type="file" accept="image/*;capture=camera"> \\ Snapshot
<input type="file" accept="video/*;capture=camcorder"> \\ Video
<input type="file" accept="audio/*;capture=microphone"> \\ Audio

Then treat the form as a normal file in your php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!