Skip to content

Cross-Browser recording of audio/video media streams; targets WebRTC/getUserMedia/WebAudio/etc.

License

Notifications You must be signed in to change notification settings

alejo-reyes/MediaStreamRecorder

 
 

Repository files navigation

MediaStreamRecorder.js / Demo

A cross-browser implementation to record audio/video streams:

  1. MediaStreamRecorder can record both audio and video in single WebM file on Firefox.
  2. MediaStreamRecorder can record audio as WAV and video as either WebM or animated gif on Chrome.

=

There is a smiliar project: RecordRTC!

=

Record audio+video on Firefox in single WebM

<script src="//www.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
var mediaConstraints = {
    audio: true, // don't forget audio!
    video: true  // don't forget video!
};

navigator.mozGetUserMedia(mediaConstraints, onMediaSuccess, onMediaError);

function onMediaSuccess(stream) {
    var mediaRecorder = new MediaStreamRecorder(stream);
    mediaRecorder.mimeType = 'video/webm';
    mediaRecorder.ondataavailable = function (blob) {
        // POST/PUT "Blob" using FormData/XHR2
        window.open(URL.createObjectURL(blob));
    };
    mediaRecorder.start(3000);
}

function onMediaError(e) {
    console.error('media error', e);
}

=

Record only audio on chrome

<script src="//www.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
var mediaConstraints = {
    audio: true
};

navigator.mozGetUserMedia(mediaConstraints, onMediaSuccess, onMediaError);

function onMediaSuccess(stream) {
    var mediaRecorder = new MediaStreamRecorder(stream);
    mediaRecorder.mimeType = 'audio/ogg';
    mediaRecorder.ondataavailable = function (blob) {
        // POST/PUT "Blob" using FormData/XHR2
        window.open(URL.createObjectURL(blob));
    };
    mediaRecorder.start(3000);
}

function onMediaError(e) {
    console.error('media error', e);
}

=

Record video/gif on chrome

<script src="//www.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
var mediaConstraints = {
    video: true
};

navigator.mozGetUserMedia(mediaConstraints, onMediaSuccess, onMediaError);

function onMediaSuccess(stream) {
    var mediaRecorder = new MediaStreamRecorder(stream);
    mediaRecorder.mimeType = 'video/webm';
	
    // for gif recording
    // mediaRecorder.mimeType = 'image/gif';
	
    mediaRecorder.videoWidth = 320;
    mediaRecorder.videoHeight = 240;
	
    mediaRecorder.ondataavailable = function (blob) {
        // POST/PUT "Blob" using FormData/XHR2
        window.open(URL.createObjectURL(blob));
    };
    mediaRecorder.start(3000);
}

function onMediaError(e) {
    console.error('media error', e);
}

=

How to upload record files using PHP?

PHP code:

<?php
foreach(array('video', 'audio') as $type) {
    if (isset($_FILES["${type}-blob"])) {
        
		$fileName = $_POST["${type}-filename"];
        $uploadDirectory = "uploads/$fileName";
        
        if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $uploadDirectory)) {
            echo("problem moving uploaded file");
        }
		
		echo($uploadDirectory);
    }
}
?>

JavaScript Code:

var fileType = 'video'; // or "audio"
var fileName = 'ABCDEF.webm';  // or "wav" or "ogg"

var formData = new FormData();
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);

xhr('save.php', formData, function (fileURL) {
    window.open(fileURL);
});

function xhr(url, data, callback) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            callback(location.href + request.responseText);
        }
    };
    request.open('POST', url);
    request.send(data);
}

=

Browser Support
Browser Support
Firefox Stable / Aurora / Nightly
Google Chrome Stable / Canary / Beta / Dev
Android Chrome Beta

=

Demos using MediaStreamRecorder.js library
Experiment Name Demo Source Code
Audio Recording Demo Source
Video/Gif Recording Demo Source

=

License

MediaStreamRecorder.js library is released under MIT licence . Copyright (c) 2013 Muaz Khan and neizerth.

About

Cross-Browser recording of audio/video media streams; targets WebRTC/getUserMedia/WebAudio/etc.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published