bgrins / videoconverter.js

[UNMAINTAINED] Convert videos in your browser
http://bgrins.github.io/videoconverter.js/
Other
2.34k stars 300 forks source link

Compiling into WebAssembly #43

Open FluorescentHallucinogen opened 8 years ago

FluorescentHallucinogen commented 8 years ago

What about compiling videoconverter.js to WebAssembly?

There are experimental native WebAssembly implementations for Google Chrome, Mozilla Firefox, Microsoft Edge and Apple Safari.

amilajack commented 7 years ago

@bgrins this would definitely be a welcome feature! It would allow for significant performance gains.

jonasof commented 7 years ago

Hello @FluorescentHallucinogen and @amilajack,

I have compiled videoconverter.js to WebAssembly, see pull request #63.

How

Just change the bellow emcc line from "build_lgpl.sh" and all runs ok. Emscripten does the job.

Before: emcc -s OUTLINING_LIMIT=100000 -v -s TOTAL_MEMORY=33554432 -O2 ffmpeg.bc -o ../ffmpeg.js --pre-js ../ffmpeg_pre.js --post-js ../ffmpeg_post.js

After: emcc -v -s TOTAL_MEMORY=33554432 -Os ffmpeg.bc -o ../ffmpeg.js --pre-js ../ffmpeg_pre.js --post-js ../ffmpeg_post.js -s WASM=1 -s "BINARYEN_METHOD='native-wasm'" -s ALLOW_MEMORY_GROWTH=1

Add also "cp ffmpeg.wasm* ../demo"

I removed OUTLINING_LIMIT and add ALLOW_MEMORY_GROWTH=1 to avoid memory table errors.

Demo

See https://jonasof.github.io/videoconverter.js-wasm-demo/ - in WebAssembly version remember to wait disappear the spinner loading icon on right of "run command" button before run any command.

Results

The filesizes are:

ASM.js WebAssembly
ffmpeg.js 20.8 MB 0.2MB
ff.js.mem 3MB
ffmpeg.wasm 12MB
total 23.8MB 12.2MB

I'm using Firefox 52.1.0 ESR.

The small tasks like image manipulation and the benchmark options gave me poor results because in each iteration (there are around 5 for each benchmark) the browser download and parse the full ffmpeg.wasm file (12 MB), while in asm.js the browser only downloads the ffmpeg.js.mem (3MB). This clearly give more advantage to asm.js.

But when running a longer single test - "Video to mp4", WebAssembly performs 3 times more faster than asm.js:

ASM.js WebAssembly
"Video to mp4" 118264ms 25171ms
timaschew commented 5 years ago

@jonasof hey do you know how I can use the libx264 codec?

Currently if I try to do that I'm getting this error (even for all_codec version):

Worker has received command
Received command: -t 3 -i input.webm -vf showinfo -strict -2 -c:v libx264 output.mp4.  Processing with 268435456 bits.
ffmpeg version 3.1.3 Copyright (c) 2000-2016 the FFmpeg developers
  built with emcc (Emscripten gcc/clang-like replacement) 1.37.10 (commit 79697257a5378666a9fa0e7215de1e61673dfe8d)
  configuration: --cc=emcc --prefix=/projetos/videoconverter.js/build/ffmpeg/../dist --extra-cflags='-I/projetos/videoconverter.js/build/ffmpeg/../dist/include -v' --enable-cross-compile --target-os=none --arch=x86_32 --cpu=generic --disable-ffplay --disable-ffprobe --disable-ffserver --disable-asm --disable-doc --disable-devices --disable-pthreads --disable-w32threads --disable-network --disable-hwaccels --disable-parsers --disable-bsfs --disable-debug --disable-protocols --disable-indevs --disable-outdevs --enable-protocol=file --enable-libvpx --enable-gpl --extra-libs='/projetos/videoconverter.js/build/ffmpeg/../dist/lib/libx264.a /projetos/videoconverter.js/build/ffmpeg/../dist/lib/libvpx.a'
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
[h264 @ 0xf88d90] Warning: not compiled with thread support, using thread emulation
[aac @ 0xf8c800] Warning: not compiled with thread support, using thread emulation
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.webm':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.83.100
  Duration: 00:00:53.68, start: 0.000000, bitrate: 826 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 691 kb/s, 25.04 fps, 25 tbr, 90k tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Unknown encoder 'libx264'
Finished processing (took 6160ms)

Your version of asm.js is also not working, but the original page on http://bgrins.github.io/videoconverter.js/demo/ works with that codec.

jonasof commented 5 years ago

No @timaschew, I have tried to resolve that issue months ago but I had no success - https://github.com/jonasof/videoconverter.js-wasm-demo/issues/2. Since I was more interested in testing webassembly than the usage of ffmpeg specifically I haven't put much efforts in resolving that.