Commit 7b4bbb41 authored by Jerome Wu's avatar Jerome Wu

Update example and tests

parent 4c5b5c51
*.mp4
/examples/*.mp4
/node_modules
/dist
/cache
......@@ -17,7 +17,7 @@
<h3>Upload a video to transcode to mp4 (x264) and play!</h3>
<video id="output-video" controls></video><br/>
<input type="file" id="uploader">
<p id="message">Remeber to wait for 5 seconds for ffmpeg.wasm to load</p>
<p id="message">Update a video</p>
<script type="text/javascript">
const readFromBlobOrFile = (blob) => (
new Promise((resolve, reject) => {
......@@ -33,43 +33,41 @@
);
const message = document.getElementById('message');
const transcode = async ({ target: { files } }) => {
const { name } = files[0];
let resolve = null;
const Core = await createFFmpegCore({
printErr: (m) => console.log(m),
print: (m) => {
console.log(m);
if (m.startsWith('FFMPEG_END')) {
resolve();
}
}
});
message.innerHTML = 'Writing file to MEMFS';
const data = await readFromBlobOrFile(files[0]);
Module.FS.writeFile(name, new Uint8Array(data));
const ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
const args = ['ffmpeg', '-hide_banner', '-nostdin', '-report', '-i', name, 'out.mp4'];
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
Core.FS.writeFile('video.avi', new Uint8Array(data));
const ffmpeg = Core.cwrap('proxy_main', 'number', ['number', 'number']);
// const args = ['ffmpeg', '-hide_banner', '-nostdin', '-i', 'video.mp4', '-vf', 'scale=512:-1', '-vcodec', 'h264', '-acodec', 'copy', 'scaled.mp4'];
const args = ['ffmpeg', '-hide_banner', '-nostdin', '-i', 'video.avi', '-acodec', 'copy', 'video.mp4'];
const argsPtr = Core._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
args.forEach((s, idx) => {
const buf = Module._malloc(s.length + 1);
Module.writeAsciiToMemory(s, buf);
Module.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32');
const buf = Core._malloc(s.length + 1);
Core.writeAsciiToMemory(s, buf);
Core.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32');
});
const d = Date.now();
console.time(`[${d}] ${files[0].name} execution time`);
message.innerHTML = 'Start to transcode';
console.time('execution time');
ffmpeg(args.length, argsPtr);
/*
* The execution of ffmpeg is not synchronized,
* so we need to parse the log file to check if completed.
*/
const timer = setInterval(() => {
const logFileName = Module.FS.readdir('.').find(name => name.endsWith('.log'));
if (typeof logFileName !== 'undefined') {
const log = String.fromCharCode.apply(null, Module.FS.readFile(logFileName));
if (log.includes("frames successfully decoded")) {
clearInterval(timer);
message.innerHTML = 'Finish transcoding';
const out = Module.FS.readFile('out.mp4');
const video = document.getElementById('output-video');
video.src = URL.createObjectURL(new Blob([out.buffer], { type: 'video/mp4' }));
console.timeEnd('execution time');
}
}
}, 500);
await new Promise((_resolve) => { resolve = _resolve });
const output = Core.FS.readFile('video.mp4');
Core.FS.unlink('video.mp4');
const video = document.getElementById('output-video');
video.src = URL.createObjectURL(new Blob([output.buffer], { type: 'video/mp4' }));
console.timeEnd(`[${d}] ${files[0].name} execution time`);
};
document.getElementById('uploader').addEventListener('change', transcode);
</script>
<script type="text/javascript" src="./dist-old/ffmpeg-core.js"></script>
<script type="text/javascript" src="../dist/ffmpeg-core.js"></script>
</body>
</html>
const path = require('path');
const fs = require('fs');
const createFFmpegCore = require('./dist/ffmpeg-core');
const createFFmpegCore = require('../dist/ffmpeg-core');
(async () => {
let resolve = null;
......@@ -13,7 +13,7 @@ const createFFmpegCore = require('./dist/ffmpeg-core');
}
}
});
const filePath = path.join(__dirname, 'tests', 'data', 'video-3s.avi');
const filePath = path.join(__dirname, '..', 'tests', 'data', 'video-3s.avi');
const data = Uint8Array.from(fs.readFileSync(filePath));
Core.FS.writeFile('video.avi', data);
......
......@@ -3,13 +3,15 @@ const path = require('path');
const createFFmpegCore = require('../dist/ffmpeg-core');
const { parseArgs, ffmpeg } = require('./utils');
const filePath = path.join(__dirname, 'data', 'video-3s.avi');
let Core = null;
let resolve = null;
let data = null;
beforeAll(async () => {
data = Uint8Array.from(fs.readFileSync(filePath));
Core = await createFFmpegCore({
});
test('transcode avi to x264 mp4', async () => {
let resolve = null;
const Core = await createFFmpegCore({
printErr: () => {},
print: (m) => {
if (m.startsWith('FFMPEG_END')) {
......@@ -18,9 +20,6 @@ beforeAll(async () => {
},
});
Core.FS.writeFile('video.avi', data);
});
test('transcode avi to x264 mp4', async () => {
ffmpeg(Core, ['-i', 'video.avi', 'video.mp4']);
await new Promise((_resolve) => { resolve = _resolve });
const fileSize = Core.FS.readFile('video.mp4').length;
......@@ -30,6 +29,16 @@ test('transcode avi to x264 mp4', async () => {
test('transcode avi to x264 mp4 twice', async () => {
for (let i = 0 ; i < 2; i++) {
let resolve = null;
const Core = await createFFmpegCore({
printErr: () => {},
print: (m) => {
if (m.startsWith('FFMPEG_END')) {
resolve();
}
},
});
Core.FS.writeFile('video.avi', data);
ffmpeg(Core, ['-i', 'video.avi', 'video.mp4']);
await new Promise((_resolve) => { resolve = _resolve });
const fileSize = Core.FS.readFile('video.mp4').length;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment