You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to run ffmpeg wasm on a cloudflare worker with a custom build. But since I can't start a web worker from within a CF worker I get the error
Caution
Worker is not defined
Is there a way to run ffmpeg.wasm on the main thread (in my case directly in the CF worker)?
Running ./build-with-docker.sh to build a very small version of ffmpeg.wasm. By following this blog post - link. The final wasm file is only 8.4 mb (2.8 mb gzipped). CF Worker can support upto 10 MB compressed.
Here is the code for the worker
import{FFmpeg}from'@ffmpeg/ffmpeg';import{fetchFile}from'@ffmpeg/util';exportinterfaceEnv{// Add any environment variables here}exportdefault{asyncfetch(request: Request,env: Env,ctx: ExecutionContext): Promise<Response>{if(request.method!== 'POST'){returnnewResponse('Send a POST request with a video file',{status: 400});}constformData=awaitrequest.formData();constvideoFile=formData.get('video')asFile|null;if(!videoFile){returnnewResponse('No video file provided',{status: 400});}try{const trimmedVideo =awaittrimVideo(videoFile);if(!trimmedVideo){returnnewResponse(`Failed to trim video`,{status: 500});}returnnewResponse(trimmedVideo,{headers: {'Content-Type': 'video/mp4','Content-Disposition': `attachment; filename="trimmed_${videoFile.name}"`, }, }); } catch (error) { // @ts-ignore return new Response(`Errorprocessingvideo: ${error.message}`,{status: 500});}},};asyncfunctiontrimVideo(videoFile: File): Promise<Uint8Array|null>{constffmpeg=newFFmpeg();try{awaitffmpeg.load({coreURL: 'http://localhost:8787/ffmpeg-core.js',wasmURL: 'http://localhost:8787/ffmpeg-core.wasm',});}catch(error){// @ts-ignoreconsole.error(error.message);returnnull;}constinputFileName='input.mp4';constoutputFileName='output.mp4';ffmpeg.writeFile(inputFileName,awaitfetchFile(videoFile));awaitffmpeg.exec(['-i',inputFileName,'-ss','1',// Start at 1 second'-to','-1',// End 1 second before the end'-c','copy',// Use the same codec (faster)outputFileName,]);constdata=awaitffmpeg.readFile(outputFileName);awaitffmpeg.deleteFile(inputFileName);awaitffmpeg.deleteFile(outputFileName);returnnewUint8Array(dataasArrayBuffer);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to run ffmpeg wasm on a cloudflare worker with a custom build. But since I can't start a web worker from within a CF worker I get the error
Caution
Worker is not defined
Is there a way to run ffmpeg.wasm on the main thread (in my case directly in the CF worker)?
Running
./build-with-docker.sh
to build a very small version of ffmpeg.wasm. By following this blog post - link. The finalwasm
file is only 8.4 mb (2.8 mb gzipped). CF Worker can support upto 10 MB compressed.Here is the code for the worker
Beta Was this translation helpful? Give feedback.
All reactions