I'm trying to build ffmpeg.wasm for CF worker. Need help

I am trying to build a custom version of ffmpeg.wasm for CF worker.
4 Replies
cj
cjOP2mo ago
Migrating away from web workers The original ffmpeg.wasm used web workers but since CF workers can't create a web worker using new Worker(..) I've created a single file with the logic from classes.ts and worker.ts ✅ This seems to work. Error with emscripten build But when I tried running my CF worker I get the error
not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)
As I understand, this is an emscripten ENV related issue. My environment should be a SERVICE WORKER for it to support CF workers And so I used claude to update my Dockerfile used to build ffmpeg wasm. I added the below code to Dockerfile
# Build ffmpeg.wasm for Cloudflare Workers
FROM ffmpeg-builder AS ffmpeg-wasm-cloudflare-builder
COPY src/bind /src/src/bind
COPY src/fftools /src/src/fftools
COPY build/ffmpeg-wasm.sh build.sh
# libraries to link
ENV FFMPEG_LIBS \
-lx264 \
-lx265 \
-lvpx \
-lmp3lame \
-logg \
-ltheora \
-lvorbis \
-lvorbisenc \
-lvorbisfile \
-lopus \
-lz \
-lwebpmux \
-lwebp \
-lsharpyuv \
-lfreetype \
-lfribidi \
-lharfbuzz \
-lass \
-lzimg

# Build for Cloudflare Workers
RUN mkdir -p /src/dist/cloudflare && bash -x /src/build.sh \
${FFMPEG_LIBS} \
-s ENVIRONMENT=worker \
-s EXPORTED_RUNTIME_METHODS=['cwrap','setValue','getValue'] \
-s EXPORT_NAME=FFmpeg \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s USE_ES6_IMPORT_META=0 \
-s MALLOC=emmalloc \
-s ALLOW_MEMORY_GROWTH=1 \
-s INITIAL_MEMORY=16777216 \
-s MAXIMUM_MEMORY=2147483648 \
-s WASM_BIGINT=1 \
-s ASSERTIONS=0 \
-s EXIT_RUNTIME=0 \
-s FILESYSTEM=0 \
-s EXPORTED_FUNCTIONS=['_malloc','_free'] \
-o dist/cloudflare/ffmpeg-core.js
# Build ffmpeg.wasm for Cloudflare Workers
FROM ffmpeg-builder AS ffmpeg-wasm-cloudflare-builder
COPY src/bind /src/src/bind
COPY src/fftools /src/src/fftools
COPY build/ffmpeg-wasm.sh build.sh
# libraries to link
ENV FFMPEG_LIBS \
-lx264 \
-lx265 \
-lvpx \
-lmp3lame \
-logg \
-ltheora \
-lvorbis \
-lvorbisenc \
-lvorbisfile \
-lopus \
-lz \
-lwebpmux \
-lwebp \
-lsharpyuv \
-lfreetype \
-lfribidi \
-lharfbuzz \
-lass \
-lzimg

# Build for Cloudflare Workers
RUN mkdir -p /src/dist/cloudflare && bash -x /src/build.sh \
${FFMPEG_LIBS} \
-s ENVIRONMENT=worker \
-s EXPORTED_RUNTIME_METHODS=['cwrap','setValue','getValue'] \
-s EXPORT_NAME=FFmpeg \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s USE_ES6_IMPORT_META=0 \
-s MALLOC=emmalloc \
-s ALLOW_MEMORY_GROWTH=1 \
-s INITIAL_MEMORY=16777216 \
-s MAXIMUM_MEMORY=2147483648 \
-s WASM_BIGINT=1 \
-s ASSERTIONS=0 \
-s EXIT_RUNTIME=0 \
-s FILESYSTEM=0 \
-s EXPORTED_FUNCTIONS=['_malloc','_free'] \
-o dist/cloudflare/ffmpeg-core.js
But resulting ffmpeg-core.js still won't work It did set the ENVIRONMENT to WORKER
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = true;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = true;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
But it fails here because self.location is undefined
if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
scriptDirectory = self.location.href;
}
if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
scriptDirectory = self.location.href;
}
This is the error I see in my console
Failed to load FFmpeg: TypeError: Cannot read properties of undefined (reading 'href')
Does anyone know how to build ffmpeg for CF Worker? Or building a project using emscripten for CF Worker?
Wallacy
Wallacy2mo ago
I may try build a ffmpeg build for workers at some point next weak when i will have more time. But just looking very quickly on you code here i can point few things: MAXIMUM_MEMORY as 2Gb make no sense. Should be 128M; Claude like others AIs appears to just make some renames and references to cloudflare but not make any real customization to the actual environment. When (if) i got to work i will let you know.
cj
cjOP4w ago
Awesome! Thank you very much Hey @Wallacy Where you able to give it a try? (Making ffmepg work on a cf worker)
Wallacy
Wallacy4w ago
Unfortunately no... i didn't get time to do that. Anyway, the only thing that cross my mind whiout look at the script is: "self.location", should no be "globalThis.location"? Self will probably reference the function on this script, i dont know if is ES5/6 but anyway. The loader part is probably confusing a lot of refs. You may actually target nodejs and use CF worker node compat layer also.
Want results from more Discord servers?
Add your server