-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV2.html
More file actions
135 lines (115 loc) · 3.5 KB
/
V2.html
File metadata and controls
135 lines (115 loc) · 3.5 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>🎥 Bharat Video Recorder + Preview</title>
<style>
body {
text-align: center;
font-family: sans-serif;
background: #f5f5f5;
}
video {
width: 90%;
max-width: 400px;
border-radius: 10px;
margin-top: 10px;
}
button, select {
margin: 10px;
padding: 10px 15px;
font-size: 16px;
border-radius: 5px;
}
#recordedVideo {
display: none;
}
</style>
</head>
<body>
<h2>🎥 Bharat Video Recorder</h2>
<select id="cameraSelect"></select><br>
<video id="preview" autoplay playsinline muted></video><br>
<button onclick="startSelectedCamera()">🔄 Switch Camera</button>
<button onclick="startRecording()">⏺ Start Recording</button>
<button onclick="stopRecording()">⏹ Stop & Preview</button>
<h3>🎬 Last Recording</h3>
<video id="recordedVideo" controls></video><br>
<script>
let currentStream = null;
let mediaRecorder;
let recordedChunks = [];
const videoElement = document.getElementById('preview');
const recordedVideo = document.getElementById('recordedVideo');
const cameraSelect = document.getElementById('cameraSelect');
async function getCameraList() {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
cameraSelect.innerHTML = '';
videoDevices.forEach((device, index) => {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${index + 1}`;
cameraSelect.appendChild(option);
});
}
async function startCamera(deviceId) {
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { deviceId: { exact: deviceId } },
audio: true
});
currentStream = stream;
videoElement.srcObject = stream;
} catch (err) {
alert("Error: " + err.message);
}
}
function startSelectedCamera() {
const deviceId = cameraSelect.value;
if (deviceId) {
startCamera(deviceId);
}
}
function startRecording() {
if (!currentStream) return alert("❗ Camera not ready");
recordedChunks = [];
mediaRecorder = new MediaRecorder(currentStream);
mediaRecorder.ondataavailable = function (e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
};
mediaRecorder.onstop = function () {
const blob = new Blob(recordedChunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
recordedVideo.src = url;
recordedVideo.style.display = 'block';
// Optional: Auto download too
const a = document.createElement('a');
a.href = url;
a.download = `bharat_recording_${Date.now()}.webm`;
a.click();
};
mediaRecorder.start();
alert("🔴 Recording started...");
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
alert("🟢 Stopped. Preview ready below!");
}
}
// Init
(async () => {
await getCameraList();
if (cameraSelect.options.length > 0) {
await startCamera(cameraSelect.value);
}
})();
</script>
</body>
</html>