Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit fbee096

Browse files
committed
Add transcode.html
1 parent 0445c4b commit fbee096

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

wasm/transcode.html

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<html>
2+
<head>
3+
<style>
4+
html, body {
5+
margin: 0;
6+
width: 100%;
7+
height: 100%
8+
}
9+
body {
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
<h3>Upload a video to transcode to mp4 (x264) and play!</h3>
18+
<video id="output-video" controls></video><br/>
19+
<input type="file" id="uploader">
20+
<p id="message">Remeber to wait for 5 seconds for ffmpeg.wasm to load</p>
21+
<script type="text/javascript">
22+
const readFromBlobOrFile = (blob) => (
23+
new Promise((resolve, reject) => {
24+
const fileReader = new FileReader();
25+
fileReader.onload = () => {
26+
resolve(fileReader.result);
27+
};
28+
fileReader.onerror = ({ target: { error: { code } } }) => {
29+
reject(Error(`File could not be read! Code=${code}`));
30+
};
31+
fileReader.readAsArrayBuffer(blob);
32+
})
33+
);
34+
const message = document.getElementById('message');
35+
const transcode = async ({ target: { files } }) => {
36+
const { name } = files[0];
37+
message.innerHTML = 'Writing file to MEMFS';
38+
const data = await readFromBlobOrFile(files[0]);
39+
Module.FS.writeFile(name, new Uint8Array(data));
40+
const ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
41+
const args = ['ffmpeg', '-hide_banner', '-nostdin', '-report', '-i', name, 'out.mp4'];
42+
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
43+
args.forEach((s, idx) => {
44+
const buf = Module._malloc(s.length + 1);
45+
Module.writeAsciiToMemory(s, buf);
46+
Module.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32');
47+
});
48+
message.innerHTML = 'Start to transcode';
49+
ffmpeg(args.length, argsPtr);
50+
51+
/*
52+
* The execution of ffmpeg is not synchronized,
53+
* so we need to parse the log file to check if completed.
54+
*/
55+
const timer = setInterval(() => {
56+
const logFileName = Module.FS.readdir('.').find(name => name.endsWith('.log'));
57+
if (typeof logFileName !== 'undefined') {
58+
const log = String.fromCharCode.apply(null, Module.FS.readFile(logFileName));
59+
if (log.includes("frames successfully decoded")) {
60+
clearInterval(timer);
61+
message.innerHTML = 'Finish transcoding';
62+
const out = Module.FS.readFile('out.mp4');
63+
const video = document.getElementById('output-video');
64+
video.src = URL.createObjectURL(new Blob([out.buffer], { type: 'video/mp4' }));
65+
}
66+
}
67+
}, 500);
68+
};
69+
document.getElementById('uploader').addEventListener('change', transcode);
70+
</script>
71+
<script type="text/javascript" src="./dist/ffmpeg-core.js"></script>
72+
</body>
73+
</html>

0 commit comments

Comments
 (0)