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

Commit fe28bd1

Browse files
committed
Update build scripts and add ffmpeg.js
1 parent 359abee commit fe28bd1

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Pt.3 FFmpeg with x264
1+
name: Pt.3 FFmpeg v0.1
22
# Controls when the action will run. Triggers the workflow on push or pull request
33
# events but only for the master branch
44
on:
@@ -22,7 +22,7 @@ jobs:
2222
bash build-with-docker.sh
2323
- uses: actions/upload-artifact@master
2424
with:
25-
name: ffmpeg-linux
25+
name: ffmpeg-core-linux
2626
path: wasm/dist
2727
macos-build:
2828
runs-on: macos-latest
@@ -45,7 +45,7 @@ jobs:
4545
bash build.sh
4646
- uses: actions/upload-artifact@master
4747
with:
48-
name: ffmpeg-macos
48+
name: ffmpeg-core-macos
4949
path: wasm/dist
5050
# Not working with error messsage:
5151
# C:\ProgramData\Chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\bin\ar.exe: libavfilter/vsink_null: No such file or directory

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "testdata"]
2+
path = testdata
3+
url = https://github.com/ffmpegwasm/testdata.git

testdata

Submodule testdata added at de37794

wasm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.mp4

wasm/build-scripts/build-ffmpeg.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ ARGS=(
77
-I. -I./fftools
88
-Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample
99
-Qunused-arguments
10-
-o wasm/dist/ffmpeg.js fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c
10+
-o wasm/dist/ffmpeg-core.js fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c
1111
-lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lm
12-
-s USE_SDL=2 # use SDL2
13-
-s USE_PTHREADS=1 # enable pthreads support
14-
-s INITIAL_MEMORY=33554432 # 33554432 bytes = 32 MB
12+
-O3 # Optimize code with performance first
13+
-s USE_SDL=2 # use SDL2
14+
-s USE_PTHREADS=1 # enable pthreads support
15+
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
16+
-s INVOKE_RUN=0 # not to run the main() in the beginning
17+
-s EXPORTED_FUNCTIONS="[_main, _proxy_main]" # export main and proxy_main funcs
18+
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, setValue, writeAsciiToMemory]" # export preamble funcs
19+
-s INITIAL_MEMORY=33554432 # 33554432 bytes = 32 MB
1520
)
1621
emcc "${ARGS[@]}"

wasm/build-scripts/configure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -eo pipefail
44

5-
CFLAGS="-s USE_PTHREADS"
5+
CFLAGS="-s USE_PTHREADS -O3"
66
LDFLAGS="$CFLAGS -s INITIAL_MEMORY=33554432" # 33554432 bytes = 32 MB
77
ARGS=(
88
--target-os=none # use none to prevent any os specific configurations

wasm/ffmpeg.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fs = require('fs');
2+
const Module = require('./dist/ffmpeg-core');
3+
4+
Module.onRuntimeInitialized = () => {
5+
const data = Uint8Array.from(fs.readFileSync('../testdata/flame.avi'));
6+
Module.FS.writeFile('flame.avi', data);
7+
8+
const ffmpeg = Module.cwrap('proxy_main', 'number', ['number', 'number']);
9+
const args = ['ffmpeg', '-hide_banner', '-i', 'flame.avi', 'flame.mp4'];
10+
const argsPtr = Module._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
11+
args.forEach((s, idx) => {
12+
const buf = Module._malloc(s.length + 1);
13+
Module.writeAsciiToMemory(s, buf);
14+
Module.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32');
15+
});
16+
ffmpeg(args.length, argsPtr);
17+
18+
/*
19+
* The execution of ffmpeg is not synchronized,
20+
* so we need to set a timer to wait for it completes.
21+
*/
22+
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);
27+
}
28+
}, 500);
29+
};

0 commit comments

Comments
 (0)