Skip to content

Commit bfc0092

Browse files
committed
Add webcam example
1 parent 434bc8e commit bfc0092

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

examples/browser/run.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<h3>Upload a video to transcode to mp4 (x264) and play!</h3>
1919
<video id="output-video" controls></video><br/>
2020
<input type="file" id="uploader">
21-
<p id="message" />
21+
<p id="message"></p>
2222
<script>
2323
const { createWorker } = FFmpeg;
2424
const worker = createWorker({

examples/browser/transcode.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<h3>Upload a video to transcode to mp4 (x264) and play!</h3>
1919
<video id="output-video" controls></video><br/>
2020
<input type="file" id="uploader">
21-
<p id="message" />
21+
<p id="message"></p>
2222
<script>
2323
const { createWorker } = FFmpeg;
2424
const worker = createWorker({

examples/browser/webcam.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<html>
2+
<head>
3+
<script src="/dist/ffmpeg.dev.js"></script>
4+
<style>
5+
html, body {
6+
margin: 0;
7+
width: 100%;
8+
height: 100%
9+
}
10+
body {
11+
display: flex;
12+
flex-direction: column;
13+
align-items: center;
14+
}
15+
</style>
16+
</head>
17+
<body>
18+
<h3>Record video from webcam and transcode to mp4 (x264) and play!</h3>
19+
<div>
20+
<video id="webcam" width="320px" height="180px"></video>
21+
<video id="output-video" width="320px" height="180px" controls></video>
22+
</div>
23+
<button id="record" disabled>Start Recording</button>
24+
<p id="message"></p>
25+
<script>
26+
const { createWorker } = FFmpeg;
27+
const worker = createWorker({
28+
corePath: '../../node_modules/@ffmpeg/core/ffmpeg-core.js',
29+
logger: ({ message }) => console.log(message),
30+
});
31+
32+
const webcam = document.getElementById('webcam');
33+
const recordBtn = document.getElementById('record');
34+
const startRecording = () => {
35+
const rec = new MediaRecorder(webcam.srcObject);
36+
const chunks = [];
37+
38+
recordBtn.textContent = 'Stop Recording';
39+
recordBtn.onclick = () => {
40+
rec.stop();
41+
recordBtn.textContent = 'Start Recording';
42+
recordBtn.onclick = startRecording;
43+
}
44+
45+
rec.ondataavailable = e => chunks.push(e.data);
46+
rec.onstop = async () => {
47+
transcode(new Uint8Array(await (new Blob(chunks)).arrayBuffer()));
48+
};
49+
rec.start();
50+
};
51+
52+
(async () => {
53+
webcam.srcObject = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
54+
await webcam.play();
55+
recordBtn.disabled = false;
56+
recordBtn.onclick = startRecording;
57+
})();
58+
59+
const transcode = async (webcamData) => {
60+
const message = document.getElementById('message');
61+
const name = 'record.webm';
62+
message.innerHTML = 'Loading ffmpeg-core.js';
63+
await worker.load();
64+
message.innerHTML = 'Start transcoding';
65+
await worker.write(name, webcamData);
66+
await worker.transcode(name, 'output.mp4');
67+
message.innerHTML = 'Complete transcoding';
68+
const { data } = await worker.read('output.mp4');
69+
70+
const video = document.getElementById('output-video');
71+
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
72+
}
73+
</script>
74+
</body>
75+
</html>

0 commit comments

Comments
 (0)