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

Commit 5f8c868

Browse files
committed
Add x264 and fix ffmpeg.js
1 parent 487f38a commit 5f8c868

File tree

8 files changed

+53
-17
lines changed

8 files changed

+53
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
/tools/python/__pycache__/
4040
/wasm/cache
4141
/wasm/dist
42+
/build

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "testdata"]
22
path = testdata
33
url = https://github.com/ffmpegwasm/testdata.git
4+
[submodule "third_party/x264"]
5+
path = third_party/x264
6+
url = https://code.videolan.org/videolan/x264.git

build.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
set -eo pipefail
44

5-
ROOT=$(dirname $0)
5+
ROOT=$PWD
6+
BUILD_DIR=$ROOT/build
67

78
# verify Emscripten version
89
emcc -v
10+
# build x264
11+
$ROOT/wasm/build-scripts/build-x264.sh $ROOT/third_party/x264 $BUILD_DIR
912
# configure FFmpeg with Emscripten
10-
$ROOT/wasm/build-scripts/configure.sh
11-
# build dependencies
13+
$ROOT/wasm/build-scripts/configure.sh $BUILD_DIR
14+
# # build dependencies
1215
$ROOT/wasm/build-scripts/make.sh
13-
# build ffmpeg.wasm
14-
$ROOT/wasm/build-scripts/build-ffmpeg.sh
16+
# # build ffmpeg.wasm
17+
$ROOT/wasm/build-scripts/build-ffmpeg.sh $BUILD_DIR

third_party/x264

Submodule x264 added at db0d417

wasm/build-scripts/build-ffmpeg.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
set -eo pipefail
44

5+
BUILD_DIR=$1
6+
57
mkdir -p wasm/dist
68
ARGS=(
7-
-I. -I./fftools
8-
-Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample
9+
-I. -I./fftools -I$BUILD_DIR/include
10+
-Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -L$BUILD_DIR/lib
911
-Qunused-arguments
1012
-o wasm/dist/ffmpeg-core.js fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c
11-
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lm
13+
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lpostproc -lm -lx264 -pthread
1214
-O3 # Optimize code with performance first
1315
-s USE_SDL=2 # use SDL2
1416
-s USE_PTHREADS=1 # enable pthreads support
1517
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
1618
-s INVOKE_RUN=0 # not to run the main() in the beginning
1719
-s EXPORTED_FUNCTIONS="[_main, _proxy_main]" # export main and proxy_main funcs
1820
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, setValue, writeAsciiToMemory]" # export preamble funcs
19-
-s INITIAL_MEMORY=33554432 # 33554432 bytes = 32 MB
21+
-s INITIAL_MEMORY=268435456 # 268435456 bytes = 256 MB
2022
)
2123
emcc "${ARGS[@]}"

wasm/build-scripts/build-x264.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash -x
2+
3+
set -eo pipefail
4+
5+
ROOT=$1
6+
BUILD_DIR=$2
7+
8+
cd $ROOT
9+
ARGS=(
10+
--prefix=$BUILD_DIR # install library in a build directory for FFmpeg to include
11+
--host=i686-gnu # use i686 linux
12+
--enable-static # enable building static library
13+
--disable-cli # disable cli tools
14+
--disable-asm # disable asm optimization
15+
--extra-cflags="-s USE_PTHREADS=1" # pass this flags for using pthreads
16+
)
17+
emconfigure ./configure "${ARGS[@]}"
18+
emmake make install-lib-static -j4
19+
cd -

wasm/build-scripts/configure.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
set -eo pipefail
44

5-
CFLAGS="-s USE_PTHREADS -O3"
6-
LDFLAGS="$CFLAGS -s INITIAL_MEMORY=33554432" # 33554432 bytes = 32 MB
5+
BUILD_DIR=$1
6+
CFLAGS="-s USE_PTHREADS -O3 -I$BUILD_DIR/include"
7+
LDFLAGS="$CFLAGS -L$BUILD_DIR/lib -s INITIAL_MEMORY=33554432" # 33554432 bytes = 32 MB
78
ARGS=(
89
--target-os=none # use none to prevent any os specific configurations
910
--arch=x86_32 # use x86_32 to achieve minimal architectural optimization
@@ -13,6 +14,8 @@ ARGS=(
1314
--disable-stripping # disable stripping
1415
--disable-programs # disable programs build (incl. ffplay, ffprobe & ffmpeg)
1516
--disable-doc # disable doc
17+
--enable-gpl # required by x264
18+
--enable-libx264 # enable x264
1619
--extra-cflags="$CFLAGS"
1720
--extra-cxxflags="$CFLAGS"
1821
--extra-ldflags="$LDFLAGS"

wasm/ffmpeg.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Module.onRuntimeInitialized = () => {
66
Module.FS.writeFile('flame.avi', data);
77

88
const ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
9-
const args = ['ffmpeg', '-hide_banner', '-i', 'flame.avi', 'flame.mp4'];
9+
const args = ['ffmpeg', '-hide_banner', '-report', '-i', 'flame.avi', 'flame.mp4'];
1010
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
1111
args.forEach((s, idx) => {
1212
const buf = Module._malloc(s.length + 1);
@@ -17,13 +17,17 @@ Module.onRuntimeInitialized = () => {
1717

1818
/*
1919
* The execution of ffmpeg is not synchronized,
20-
* so we need to set a timer to wait for it completes.
20+
* so we need to parse the log file to check if completed.
2121
*/
2222
const timer = setInterval(() => {
23-
if (Module.FS.readdir('.').find(f => f === 'flame.mp4') !== -1) {
24-
clearInterval(timer);
25-
const output = Module.FS.readFile('flame.mp4');
26-
fs.writeFileSync('flame.mp4', output);
23+
const logFileName = Module.FS.readdir('.').find(name => name.endsWith('.log'));
24+
if (typeof logFileName !== 'undefined') {
25+
const log = String.fromCharCode.apply(null, Module.FS.readFile(logFileName));
26+
if (log.includes("frames successfully decoded")) {
27+
clearInterval(timer);
28+
const output = Module.FS.readFile('flame.mp4');
29+
fs.writeFileSync('flame.mp4', output);
30+
}
2731
}
2832
}, 500);
2933
};

0 commit comments

Comments
 (0)