Permalink
cca56d3 Oct 6, 2018
1 contributor

Users who have contributed to this file

115 lines (88 sloc) 2.76 KB
<style>
* {
margin: 0;
padding: 0;
}
video {
max-width: 100%;
}
h1, div, video {
margin: 5px 20px;
}
</style>
<title>Calculate Recording Duration | RecordRTC</title>
<h1>Calculate Recording Duration using RecordRTC</h1>
<br>
<div>
<button id="btn-start-recording">Start Recording</button>
<button id="btn-stop-recording" disabled>Stop Recording</button>
</div>
<hr>
<video controls autoplay></video>
<script src="https://cdn.webrtc-experiment.com/RecordRTC.js"></script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script>
var video = document.querySelector('video');
var dateStarted;
function captureCamera(callback) {
navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(function(camera) {
callback(camera);
}).catch(function(error) {
alert('Unable to capture your camera. Please check console logs.');
console.error(error);
});
}
function stopRecordingCallback() {
var blob = recorder.getBlob();
video.src = URL.createObjectURL(blob);
video.play();
recorder.camera.stop();
recorder = null;
}
function calculateTimeDuration(secs) {
var hr = Math.floor(secs / 3600);
var min = Math.floor((secs - (hr * 3600)) / 60);
var sec = Math.floor(secs - (hr * 3600) - (min * 60));
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
if(hr <= 0) {
return min + ':' + sec;
}
return hr + ':' + min + ':' + sec;
}
var recorder; // globally accessible
document.getElementById('btn-start-recording').onclick = function() {
this.disabled = true;
captureCamera(function(camera) {
setSrcObject(camera, video);
video.play();
recorder = RecordRTC(camera, {
recorderType: MediaStreamRecorder,
mimeType: 'video/webm',
timeSlice: 1000 // pass this parameter
});
recorder.startRecording();
dateStarted = new Date().getTime();
(function looper() {
if(!recorder) {
return;
}
document.querySelector('h1').innerHTML = 'Recording Duration: ' + calculateTimeDuration((new Date().getTime() - dateStarted) / 1000);
setTimeout(looper, 1000);
})();
// release camera on stopRecording
recorder.camera = camera;
document.getElementById('btn-stop-recording').disabled = false;
});
};
document.getElementById('btn-stop-recording').onclick = function() {
this.disabled = true;
recorder.stopRecording(stopRecordingCallback);
};
</script>
<footer style="margin-top: 20px;"><small id="send-message"></small></footer>
<script src="https://cdn.webrtc-experiment.com/common.js"></script>