Commit d0b4ff36 authored by Jerome Wu's avatar Jerome Wu

Modularize ffmepg-core and update tests

parent eb607621
......@@ -17,8 +17,10 @@ ARGS=(
-s USE_PTHREADS=1 # enable pthreads support
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
-s INVOKE_RUN=0 # not to run the main() in the beginning
-s MODULARIZE=1 # use modularized version to be more flexible
-s EXPORT_NAME="createFFmpegCore" # assign export name for browser
-s EXPORTED_FUNCTIONS="[_main, _proxy_main]" # export main and proxy_main funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, setValue, writeAsciiToMemory]" # export preamble funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" # export preamble funcs
-s INITIAL_MEMORY=1073741824 # 1073741824 bytes = 1 GB
)
emcc "${ARGS[@]}"
const path = require('path');
const fs = require('fs');
const Module = require('./dist/ffmpeg-core');
const createFFmpegCore = require('./dist/ffmpeg-core');
Module.onRuntimeInitialized = () => {
const filePath = path.join(__dirname, 'tests', 'data', 'video-15s.avi');
(async () => {
let resolve = null;
const Core = await createFFmpegCore({
printErr: (m) => console.log(m),
print: (m) => {
console.log(m);
if (m.startsWith('FFMPEG_END')) {
resolve();
}
}
});
const filePath = path.join(__dirname, 'tests', 'data', 'video-3s.avi');
const data = Uint8Array.from(fs.readFileSync(filePath));
Module.FS.writeFile('video.avi', data);
Core.FS.writeFile('video.avi', data);
const ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
const ffmpeg = Core.cwrap('proxy_main', 'number', ['number', 'number']);
const args = ['ffmpeg', '-hide_banner', '-report', '-i', 'video.avi', 'video.mp4'];
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
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');
});
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);
const output = Module.FS.readFile('video.mp4');
fs.writeFileSync('video.mp4', output);
console.timeEnd('execution time');
process.exit(1);
}
}
}, 500);
};
await new Promise((_resolve) => { resolve = _resolve });
const output = Core.FS.readFile('video.mp4');
fs.writeFileSync(path.join(__dirname, 'video.mp4'), output);
console.timeEnd('execution time');
process.exit(1);
})();
const {
initModule,
parseArgs,
} = require('./utils');
let Module = null;
let ffmpeg = null;
beforeAll(async () => {
Module = await initModule();
ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
});
test('test', () => {
const ret = ffmpeg(...parseArgs(['ffmpeg', '-v']));
expect(ret).toBe(0);
});
const fs = require('fs');
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({
printErr: () => {},
print: (m) => {
if (m.startsWith('FFMPEG_END')) {
resolve();
}
},
});
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;
Core.FS.unlink('video.mp4');
expect(fileSize).toBe(98326);
}, 30000);
test('transcode avi to x264 mp4 twice', async () => {
for (let i = 0 ; i < 2; i++) {
ffmpeg(Core, ['-i', 'video.avi', 'video.mp4']);
await new Promise((_resolve) => { resolve = _resolve });
const fileSize = Core.FS.readFile('video.mp4').length;
Core.FS.unlink('video.mp4');
expect(fileSize).toBe(98326);
}
}, 30000);
let Module = null;
exports.initModule = () => (
new Promise((resolve) => {
const _Module = require('../dist/ffmpeg-core.js');
_Module.onRuntimeInitialized = () => {
Module = _Module;
resolve(Module);
}
})
);
exports.parseArgs = (args) => {
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
const parseArgs = (Core, args) => {
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');
});
return [args.length, argsPtr];
};
const ffmpeg = (Core, args) => {
Core.ccall(
'proxy_main',
'number',
['number', 'number'],
parseArgs(Core, ['ffmpeg', '-nostdin', ...args]),
);
}
module.exports = {
ffmpeg,
parseArgs,
}
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