LaunchReel 兩個 FFmpeg 旗標令瀏覽器影片匯出慢 7 倍並出現凍結畫格
Two ffmpeg flags that made our in-browser video export 7x slower (and gave us frozen frames)
LaunchReel 團隊指出,瀏覽器透過 ffmpeg.wasm 匯出影片時,兩種 FFmpeg 旗標設定會令匯出慢 7 倍,並造成片段交界凍結或重複畫格。團隊建議統一影片輸入及輸出的影格率,並將字幕 PNG 作為單一靜態畫格輸入,讓 overlay 保留畫格並按時間控制顯示。其 90 秒匯出曾由約 8 分鐘增至約 1 小時,批次速度約為 0.05x。
We build LaunchReel, a tool that makes short vertical video ads. The editor exports the final MP4 in the browser with ffmpeg.wasm: trim each clip, concatenate, then burn captions on top. Two small flag choices cost us a lot of debugging. Both are easy to repeat, so here they are.
1. Pin every video input to one frame rate
Our segments come from different sources: AI-generated clips, user uploads from phones, still images turned into short clips. Their native frame rates differ (24, 25, 30, 29.97, variable).
If you trim each one with its own rate and then concat, you get frozen or duplicated frames at the segment boundaries, and anything timed against the output (like a caption highlighting the current word) can briefly show the previous state at fast cuts.
The fix is boring: choose one export rate and force it on every input and output that touches video.
# video segment
ffmpeg -ss 2.0 -t 3.5 -i clip.mp4 -r 30 -c:v libx264 -pix_fmt yuv420p seg1.mp4
# still image held as a segment
ffmpeg -loop 1 -framerate 30 -t 2.0 -i photo.png -r 30 -c:v libx264 -pix_fmt yuv420p seg2.mp4
We keep a test that asserts -r 30 is on every trim command. If it fails, we fix the pipeline, not the assertion.
2. Don't loop a full-frame overlay image with an input frame rate
Captions are rendered as transparent PNGs the size of the video (1080×1920) and composited with overlay, about 30 per filtergraph, each enabled only during its time window.
This version looks harmless:
-loop 1 -framerate 30 -i ovl0.png # repeated for each caption
...
[0:v][1:v]overlay=0:0:enable='between(t,1.2,1.8)'[v1]; ...
-loop 1 with an explicit input rate turns each PNG into a 30 fps stream for the whole video length. ffmpeg then decodes and scales a full ~8 MB RGBA frame 30 times a second per caption input. With 30 inputs chained, that's hundreds of full-frame rescales per output frame. On the single-threaded wasm build, a 90-second export went from about 8 minutes to about an hour, with batches running at ~0.05x.
You won't see it in a small test with one tiny PNG. It needs all four together: input rate, full-frame image, loop, deep chain.
What works: feed each PNG as one still frame and let overlay hold it.
-framerate 30 -i ovl0.png # no -loop, no -t
...
[0:v][1:v]overlay=0:0:enable='gte(t,1.2)*lt(t,1.8)'[v1]; ...
overlay keeps the last frame of a finished input by default (eof_action=repeat), so the single frame stays available for the whole timeline and enable decides when it's drawn. Two things to avoid:
-
shortest=1ends the output when the one-frame input ends. -
eof_action=passstops drawing the caption after its first frame, so it never shows.
Bounding the loop with -t or -itsoffset instead also looked tempting. In our case it deadlocked ffmpeg's frame sync whenever the first caption in a batch started well after t=0.
Takeaways
- One output frame rate, set explicitly everywhere, beats trusting each source.
- A large image input should be one held frame plus
enable, not a looped stream. - Write tests that assert the flags. These bugs return quietly when someone "cleans up" a command.
If you're exporting for TikTok, Reels or Shorts, the target is 1080×1920 (9:16), H.264 MP4, with captions burned in because feeds autoplay muted. We keep a free short-form video ad specs lookup with lengths and caption guidance per platform.
Disclosure: I'm the founder of LaunchReel.
來源:DEV Community · dev.to