Skip to content

Instantly share code, notes, and snippets.

@chicken-suop
Last active January 14, 2022 06:56
Show Gist options
  • Save chicken-suop/f1fa1f665d699f0d20880a7ccacd7940 to your computer and use it in GitHub Desktop.
Save chicken-suop/f1fa1f665d699f0d20880a7ccacd7940 to your computer and use it in GitHub Desktop.
# !/bin/bash -x
set -e -o pipefail
# I follow [this](https://emscripten.org/docs/getting_started/downloads.html) to install emscripten manually
# then source the env file
source ../emsdk/emsdk_env.sh
# I manually run these steps to compile x264
# cd ~/ffmpeg/
# mkdir build sources
# cd sources
# git clone https://code.videolan.org/videolan/x264.git
# emconfigure ./configure --host=i686-gnu --disable-cli --disable-asm --enable-static --prefix=~/ffmpeg/build
# emmake make -C ~/ffmpeg/sources/x264 install-lib-static -j
# emmake make -C ~/ffmpeg/sources/x264 clean
# verify Emscripten version
emcc -v
BUILD_DIR=~/ffmpeg/build
CFLAGS="-I$BUILD_DIR/include -O3"
LDFLAGS="$CFLAGS -L$BUILD_DIR/lib"
# configure FFMpeg with Emscripten
CONFIG_ARGS=(
--target-os=none # use none to prevent any os specific configurations
--arch=x86_32 # use x86_32 to achieve minimal architectural optimization
--enable-cross-compile # enable cross compile
--disable-x86asm # disable x86 asm
--disable-inline-asm # disable inline asm
--disable-stripping # disable stripping
--disable-programs # disable programs build (incl. ffplay, ffprobe & ffmpeg)
--disable-doc # disable doc
--nm="llvm-nm"
--ar=emar
--ranlib=emranlib
--cc=emcc
--cxx=em++
--objcc=emcc
--dep-cc=emcc
--extra-cflags="$CFLAGS"
--extra-cxxflags="$CFLAGS"
--extra-ldflags="$LDFLAGS"
--disable-avdevice
--disable-swresample
--disable-postproc
--disable-network
--disable-pthreads
--disable-w32threads
--disable-os2threads
--disable-everything
--enable-asm
--enable-bsf=h264_mp4toannexb
--enable-ffmpeg
--enable-avcodec
--enable-avformat
--enable-avutil
--enable-swresample
--enable-swscale
--enable-avfilter
--enable-libx264
--enable-gpl
--enable-encoder=libx264,mpeg4,mov,gif,h264
--enable-decoder=rawvideo,hevc,h264,mpeg4,gif
--enable-parser=mpeg4video,mpegaudio,gif
--enable-demuxer=h264,mov,gif,concat,image2,image2pipe,mpegps
--enable-muxer=mp4,gif,mov
--enable-protocol=file
--enable-filter=scale,overlay,fps,movie
)
emconfigure ./configure "${CONFIG_ARGS[@]}"
# build dependencies
emmake make -j4
# build ffmpeg.wasm
mkdir -p wasm/dist
ARGS=(
-I. -I./fftools # Add directory to include search path
-Llibavcodec -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample # Add directory to library search path
-Qunused-arguments # Don't emit warning for unused driver arguments.
-o wasm/dist/ffmpeg-core.js fftools/ffmpeg_opt.c fftools/ffmpeg_filter.c fftools/ffmpeg_hw.c fftools/cmdutils.c fftools/ffmpeg.c # output
-lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -lm # library
-s USE_SDL=2 # use SDL2
-s MODULARIZE=1 # use modularized version to be more flexible
-s EXPORT_NAME="createFFmpegCore" # assign export name for browser
-s EXPORTED_FUNCTIONS="[_main]" # export main and proxy_main funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" # export extra runtime methods
-s INITIAL_MEMORY=33554432 # 33554432 bytes = 32MB
-s ALLOW_MEMORY_GROWTH=1 # allows the total amount of memory used to change depending on the demands of the application
--post-js wasm/post-js.js # emits a file after the emitted code. use to expose exit function
-O3 # optimize code and reduce code size
)
emcc "${ARGS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment