forked from muaz-khan/RecordRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow-animated-bar-on-video.html
93 lines (70 loc) · 2.7 KB
/
show-animated-bar-on-video.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<style>
html, body, video, canvas {
margin: 0!important;
padding: 0!important;
}
</style>
<title>Show Animated Bar on the recorded video | RecordRTC</title>
<h1>Show Animated Bar on the recorded video | RecordRTC</h1>
<p>It will record 10-seconds video and automatically stop the recording.</p>
<video id="video-preview" controls autoplay playsinline></video>
<script src="/RecordRTC.js"></script>
<script>
var videoPreview = document.getElementById('video-preview');
navigator.mediaDevices.getUserMedia({video: true}).then(function(camera) {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.style = 'position: absolute; top: 0; left: 0; opacity: 0; margin-top: -9999999999; margin-left: -9999999999; top: -9999999999; left: -9999999999; z-index: -1;';
document.body.appendChild(canvas);
var video = document.createElement('video');
video.autoplay = true;
video.srcObject = camera;
var canvasStream = canvas.captureStream(15);
var audioPlusCanvasStream = new MediaStream();
canvasStream.getVideoTracks().forEach(function(videoTrack) {
audioPlusCanvasStream.addTrack(videoTrack);
});
camera.getAudioTracks().forEach(function(audioTrack) {
audioPlusCanvasStream.addTrack(audioTrack);
});
var recorder = RecordRTC(audioPlusCanvasStream, {
type: 'video'
});
recorder.setRecordingDuration(10 * 1000).onRecordingStopped(function() {
var blob = recorder.getBlob();
recorder = null;
camera.stop();
videoPreview.srcObject = null;
videoPreview.src = URL.createObjectURL(blob);
});
recorder.startRecording();
videoPreview.srcObject = canvasStream;
var rectX = 240;
var rectY = 240 - 20;
(function looper() {
if(!recorder) return; // ignore/skip on stop-recording
canvas.width = 320;
canvas.height = 240;
rectX-=5;
if(rectX <= -40) {
rectX = 240 + 50;
}
context.drawImage(video, 0, 0, canvas.width, canvas.height);
context.save();
context.fillStyle = 'black';
context.globalAlpha = 0.4;
context.fillRect(0, rectY - 20, 1000, 30)
context.restore();
context.font = '20px Georgia';
context.fillStyle = 'white';
context.fillText('Hello', rectX, rectY);
// repeat (looper)
setTimeout(looper, 100);
})();
}).catch(function(error) {
alert('Unable to capture camera. Please check console logs.');
console.error(error);
});
</script>
<footer style="margin-top: 20px;"><small id="send-message"></small></footer>
<script src="https://www.webrtc-experiment.com/common.js"></script>