ffmpegwasm / ffmpeg.wasm

FFmpeg for browser, powered by WebAssembly
https://ffmpegwasm.netlify.app
MIT License
14.4k stars 845 forks source link

FFmpeg.wasm exits early when attempting to concatenate frames into video #445

Open viberfoner opened 1 year ago

viberfoner commented 1 year ago

Describe the bug I am creating a tool that captures frames from an HTML canvas and uses ffmpeg.wasm to make a video out of the frames. The way it works is it saves each frame as a PNG with ffmpeg.FS(), then uses a glob pattern in ffmpeg.run() to collect all of the frames and concatenate them into a video.

The issue is that FFmpeg is exiting early (without error), causing the video to be cut short. It seems to always stop at 180 frames in both Firefox and Chrome. I have created a simpler version of the code that just uses one image for all of the frames (but still duplicates it multiple times in the FFmpeg FS) so that the code for capturing the canvas can be omitted.

To Reproduce

  1. Clone the repo (https://github.com/viberfoner/ffmpeg-wasm-frame-limit-bug), install dependencies, and run the server locally.

      git clone https://github.com/viberfoner/ffmpeg-wasm-frame-limit-bug.git
      cd ffmpeg-wasm-frame-limit-bug
      npm install
      node server
  2. Open http://localhost:8080 in your browser. Open dev tools and note that the frames stop encoding at 180, instead of finishing at 300.

Expected behavior FFmpeg should produce a 5 second long video with 300 frames. Instead, it stops at 180 frames.

Desktop:

Additional context This is my first time submitting an issue on GitHub, so please let me know if I can improve it!

viberfoner commented 1 year ago

Update: I edited the issue to include a repo for easier reproduction of the bug.

benz2012 commented 1 year ago

It appears the FFmpeg command you wrote is the culprit.

From the docs

-t duration (input/output)

And your command was

-r 60 -pattern_type glob -i *.png -c:v libx264 -t 3 -pix_fmt yuv420p -vf scale=400:400 concat.mp4

where -t 3 was limiting the encoder to 3 seconds

removing this argument, I was able to get the full 300 frames to encode