Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Build some "core" variations. (#16)
Browse files Browse the repository at this point in the history
* Revert "Upgrade to emscripten v2.0.24"
Because of confusing due to API changes.

* Build some "core" variations.

* add png-to-mp4

* add test for limited-function core

Co-authored-by: jeromewu <jeromewus@gmail.com>
  • Loading branch information
novogrammer and jeromewu committed Aug 14, 2022
1 parent e87c5e2 commit 9e96b1c
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 19 deletions.
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ run-all() {
#build-aom # disabled as it is extremely slow
configure-ffmpeg
build-ffmpeg
configure-ffmpeg.mp4-scale
build-ffmpeg.mp4-scale
configure-ffmpeg.png-to-mp4
build-ffmpeg.png-to-mp4
)
run ${SCRIPTS[@]}
}
Expand Down
30 changes: 23 additions & 7 deletions wasm/build-scripts/build-ffmpeg.mp4-scale.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,42 @@
set -eo pipefail
source $(dirname $0)/var.sh

mkdir -p wasm/dist
emmake make -j
if [[ "$FFMPEG_ST" != "yes" ]]; then
mkdir -p wasm/packages/core.mp4-scale/dist
EXPORTED_FUNCTIONS="[_main, _proxy_main]"
EXTRA_FLAGS=(
-pthread
-s USE_PTHREADS=1 # enable pthreads support
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
-o wasm/packages/core.mp4-scale/dist/ffmpeg-core.js
)
else
mkdir -p wasm/packages/core-st.mp4-scale/dist
EXPORTED_FUNCTIONS="[_main]"
EXTRA_FLAGS=(
-o wasm/packages/core-st.mp4-scale/dist/ffmpeg-core.js
)
fi

FLAGS=(
-I. -I./fftools -I$BUILD_DIR/include
-Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -L$BUILD_DIR/lib
-Wno-deprecated-declarations -Wno-pointer-sign -Wno-implicit-int-float-conversion -Wno-switch -Wno-parentheses -Qunused-arguments
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lpostproc -lm -lx264 -pthread
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lpostproc -lm -lx264
fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c
-o wasm/dist/ffmpeg-core.js
-s USE_SDL=2 # use SDL2
-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 EXIT_RUNTIME=1 # exit runtime after execution
-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 EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS" # export main and proxy_main funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" # export preamble funcs
-s INITIAL_MEMORY=1073741824 # 1073741824 bytes = 1 GB
--post-js wasm/src/post.js
--pre-js wasm/src/pre.js
$OPTIM_FLAGS
${EXTRA_FLAGS[@]}
)
echo "FFMPEG_EM_FLAGS=${FLAGS[@]}"
emmake make -j
emcc "${FLAGS[@]}"
44 changes: 44 additions & 0 deletions wasm/build-scripts/build-ffmpeg.png-to-mp4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -eo pipefail
source $(dirname $0)/var.sh

if [[ "$FFMPEG_ST" != "yes" ]]; then
mkdir -p wasm/packages/core.png-to-mp4/dist
EXPORTED_FUNCTIONS="[_main, _proxy_main]"
EXTRA_FLAGS=(
-pthread
-s USE_PTHREADS=1 # enable pthreads support
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
-o wasm/packages/core.png-to-mp4/dist/ffmpeg-core.js
)
else
mkdir -p wasm/packages/core-st.png-to-mp4/dist
EXPORTED_FUNCTIONS="[_main]"
EXTRA_FLAGS=(
-o wasm/packages/core-st.png-to-mp4/dist/ffmpeg-core.js
)
fi

FLAGS=(
-I. -I./fftools -I$BUILD_DIR/include
-Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -L$BUILD_DIR/lib
-Wno-deprecated-declarations -Wno-pointer-sign -Wno-implicit-int-float-conversion -Wno-switch -Wno-parentheses -Qunused-arguments
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lpostproc -lm -lx264 -lz
fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c
-s USE_SDL=2 # use SDL2
-s INVOKE_RUN=0 # not to run the main() in the beginning
-s EXIT_RUNTIME=1 # exit runtime after execution
-s MODULARIZE=1 # use modularized version to be more flexible
-s EXPORT_NAME="createFFmpegCore" # assign export name for browser
-s EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS" # export main and proxy_main funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" # export preamble funcs
-s INITIAL_MEMORY=1073741824 # 1073741824 bytes = 1 GB
--post-js wasm/src/post.js
--pre-js wasm/src/pre.js
$OPTIM_FLAGS
${EXTRA_FLAGS[@]}
)
echo "FFMPEG_EM_FLAGS=${FLAGS[@]}"
emmake make -j
emcc "${FLAGS[@]}"
27 changes: 27 additions & 0 deletions wasm/build-scripts/configure-ffmpeg.png-to-mp4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -euo pipefail
source $(dirname $0)/var.sh

FLAGS=(
"${FFMPEG_CONFIG_FLAGS_BASE[@]}"
--disable-all
--enable-gpl # required by x264
--enable-libx264 # enable x264
--enable-zlib # enable zlib
--enable-avcodec
--enable-avformat
--enable-avfilter
--enable-swresample
--enable-swscale
--enable-decoder=png
--enable-encoder=png,libx264
--enable-parser=h264,png
--enable-protocol=file
--enable-demuxer=image2
--enable-muxer=mp4
--enable-filter=scale,format,null

)
echo "FFMPEG_CONFIG_FLAGS=${FLAGS[@]}"
emconfigure ./configure "${FLAGS[@]}"
2 changes: 1 addition & 1 deletion wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "serve .",
"test": "node --experimental-wasm-threads --experimental-wasm-bulk-memory ./node_modules/.bin/jest"
"test": "node --experimental-wasm-threads --experimental-wasm-bulk-memory ./node_modules/.bin/jest --verbose"
},
"author": "jeromewus@gmail.com",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions wasm/packages/core-st.mp4-scale/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
2 changes: 2 additions & 0 deletions wasm/packages/core-st.png-to-mp4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
2 changes: 2 additions & 0 deletions wasm/packages/core.mp4-scale/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
2 changes: 2 additions & 0 deletions wasm/packages/core.png-to-mp4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
22 changes: 22 additions & 0 deletions wasm/tests/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs');
const path = require('path');
const base = path.join(__dirname, 'data');
const base2 = path.join(__dirname, 'data2');
const avi = Uint8Array.from(fs.readFileSync(path.join(base, 'video-1s.avi')));
const mp4 = Uint8Array.from(fs.readFileSync(path.join(base2, 'video-1s.mp4')));
const wav = Uint8Array.from(fs.readFileSync(path.join(base, 'audio-1s.wav')));
const arial = Uint8Array.from(fs.readFileSync(path.join(base, 'arial.ttf')));
const png = Uint8Array.from(fs.readFileSync(path.join(base, 'image.png')));
Expand Down Expand Up @@ -229,6 +231,26 @@ const CASES = [
],
st: false,
},
{
name: 'mp4 scale',
args: ['-i', 'video.mp4', '-vf', 'scale=128:-1', 'video-scaled.mp4'],
input: [
{ name: 'video.mp4', data: mp4 },
],
output: [
{ name: 'video-scaled.mp4', type: 'video/mp4' },
]
},
{
name: 'png to mp4',
args: ['-i', 'image.png', 'video.mp4'],
input: [
{ name: 'image.png', data: png },
],
output: [
{ name: 'video.mp4', type: 'video/mp4' },
]
},
];

module.exports = {
Expand Down
Binary file added wasm/tests/data2/video-15s.mp4
Binary file not shown.
Binary file added wasm/tests/data2/video-1s.mp4
Binary file not shown.
Binary file added wasm/tests/data2/video-3s.mp4
Binary file not shown.
7 changes: 4 additions & 3 deletions wasm/tests/transcode-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { CASES, TIMEOUT } = require('./config');

require('events').EventEmitter.defaultMaxListeners = 64;

module.exports = (mode) => {
module.exports = (mode, corename, caseFilter = (name) => true) => {
const { getCore, ffmpeg } = require('./utils')(mode);
CASES.forEach(({
name,
Expand All @@ -14,8 +14,9 @@ module.exports = (mode) => {
st,
}) => {
if (mode === 'st' && st === false) { return; }
test(`[${mode}] ${name}`, async () => {
const core = await getCore();
if (!caseFilter(name)){ return; }
test(`[${mode}][${corename}] ${name}`, async () => {
const core = await getCore(corename);
for (let i = 0; i < dirs.length; i++) {
await core.FS.mkdir(dirs[i]);
}
Expand Down
5 changes: 4 additions & 1 deletion wasm/tests/transcode-mt.test.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
require('./transcode-base')('mt');
const base = require('./transcode-base');
base('mt','core');
base('mt','core.mp4-scale',(name) => name == 'mp4 scale');
base('mt','core.png-to-mp4',(name) => name == 'png to mp4');
5 changes: 4 additions & 1 deletion wasm/tests/transcode-st.test.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
require('./transcode-base')('st');
const base = require('./transcode-base');
base('st','core-st');
base('st','core-st.mp4-scale',(name) => name == 'mp4 scale');
base('st','core-st.png-to-mp4',(name) => name == 'png to mp4');
4 changes: 2 additions & 2 deletions wasm/tests/utils/mt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const ffmpeg = ({ core, args }) => new Promise((_resolve) => {

});

const getCore = () => (
require('../../../packages/core/dist/ffmpeg-core')({
const getCore = (corename) => (
require(`../../../packages/${corename}/dist/ffmpeg-core`)({
printErr: () => {},
print: (m) => {
if (m.startsWith('FFMPEG_END')) {
Expand Down
3 changes: 2 additions & 1 deletion wasm/tests/utils/st/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Worker } = require('worker_threads');
const path = require('path');

const getCore = async () => {
const getCore = async (corename) => {
const resolves = {};
let resolveExit = null;
let _id = 0;
Expand All @@ -21,6 +21,7 @@ const getCore = async () => {
resolveExit();
});

await getHandler({ type: 'LOAD', corename });
await getHandler({ type: 'INIT' });

return {
Expand Down
9 changes: 6 additions & 3 deletions wasm/tests/utils/st/worker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { parentPort } = require('worker_threads');
const createFFmpegCore = require('../../../packages/core-st/dist/ffmpeg-core');
let createFFmpegCore = null;
const parseArgs = require('../parseArgs');
let core = null;

parentPort.on('message', async ({ id, type, cmd, args }) => {
parentPort.on('message', async ({ id, type, cmd, args, corename }) => {
switch(type) {
case 'LOAD':
createFFmpegCore = require(`../../../packages/${corename}/dist/ffmpeg-core`);
parentPort.postMessage({ id, type: 'LOAD' });
break;
case 'INIT':
core = await createFFmpegCore({
printErr: () => {},
Expand Down

0 comments on commit 9e96b1c

Please sign in to comment.