Skip to content

bug: obs-ffmpeg: stopping a Custom FFmpeg Output loses the last ~0.8 s of video (the encoders are never drained) #1124

Closed

Description

@rdoupe

Found by our FFmpeg 8.1.3 A/B release check (check 67, Custom FFmpeg Output recordings); not a user report. A recording through the Custom FFmpeg Output (Settings > Output > Recording, Type "Custom Output (FFmpeg)", to a file or a URL) ends early: with libx264, libvpx-vp9 or FLV/H.264 the video stops about 0.8 s before the recording does, with SVT-AV1 about 1.3 s before, and the AAC track loses its last frame. Encoders without delay (FFV1, MJPEG, ProRes, PCM) are complete. It behaves the same with FFmpeg 8.1.2 and 8.1.3. It is not a regression from our #355 (the stop-race fix): the encoders were never drained before that either, and upstream's obs-ffmpeg-output.c has the same stop path.

Location

plugins/obs-ffmpeg/obs-ffmpeg-output.c:

  • encode_video() (line 724) and encode_audio() send one frame and take at most one packet (avcodec_receive_packet(), lines 761 and 841). Encoders with B-frames or lookahead hold frames back and give them out only after avcodec_send_frame(ctx, NULL), which nothing ever sends.
  • ffmpeg_deactivate() (line 1222) frees the packets the write thread had not written yet (line 1239). The write thread leaves as soon as the stop event is set (line 1000). Then ffmpeg_data_free() (line 1244) writes the trailer and frees the encoders with their frames still in them.

Class

Data loss at the end of every such recording, not subjective. The length lost is the encoder's delay: libx264 lookahead, libvpx lag_in_frames, SVT-AV1 lookahead, B-frames.

How it is reached

Settings > Output > Recording > Type "Custom Output (FFmpeg)", any video encoder with delay (the default libx264, libvpx-vp9, libsvtav1, libaom-av1, mpeg4 with B-frames) or audio encoder with delay (aac, libopus), then Stop Recording. The same applies to streams sent to a URL through this output.

Reproduced in 0.1.0-beta.20261014

The released AppImage, headless (private Xvfb and D-Bus, fresh profile, loopback only), check 67: Advanced output, Custom Output (FFmpeg) to a file, about 4.2 s per recording, then each file decoded:

Format, encoders File duration Video ends at Lost
Matroska, libx264 + aac 4.14 s 3.40 s ~0.8 s of video
WebM, libvpx-vp9 + libopus 4.20 s 3.40 s ~0.8 s
FLV, libx264 + aac 4.23 s 3.40 s ~0.8 s
Matroska, libsvtav1 + libopus 4.12 s 2.87 s ~1.3 s
NUT, ffv1 + flac 4.23 s 4.23 s none
AVI, mjpeg + pcm_s16le 4.27 s 4.23 s none
MPEG-TS, mpeg2video + mp2 4.24 s 4.24 s none

The same check in the A/B builds (FFmpeg 8.1.2 and 8.1.3) gave the same values, within 0.05 s.

Proposed fix

On a stop (not on a write failure), ffmpeg_deactivate() first writes the packets that are still queued. It then drains each encoder: avcodec_send_frame(ctx, NULL), then every packet avcodec_receive_packet() returns, written in order. After that it writes the trailer. Packets from after the stop time are left out, as now.

RED test (cmocka, test_ffmpeg_output_drain, Linux, under xvfb-run; it skips without a display). It builds the real obs-ffmpeg-output.c into the test and wraps avcodec_send_frame() (-Wl,--wrap) to count the frames given to the encoder. It records for 1.5 s and stops. Every frame encoded before obs_output_stop() was called must be in the file. Cases:

  • MPEG-4 with 3 B-frames (built into FFmpeg);
  • libx264 (where FFmpeg has it);
  • AAC in ADTS (built in);
  • a guard: FFV1, which has no delay.

3 of 4 fail today (41 of 44 frames, 27 of 45, 68 of 69 audio frames). The guard passes.

Activity

  1. rdoupe commented on Oct 16, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: this issue.

    1. RED: test_ffmpeg_output_drain builds the real obs-ffmpeg-output.c into the test and counts the frames given to the encoder (--wrap on avcodec_send_frame()). It records for 1.5 s, stops, and counts the finished file's packets. Every frame encoded before the stop call must be in the file. Today MPEG-4 with B-frames, libx264 and AAC lose 3, 18 and 1 frames. FFV1 (no delay) is a guard and passes. Reproduced in 0.1.0-beta.20261014: Custom Output recordings with libx264, libvpx-vp9 and SVT-AV1 end 0.8-1.3 s early.
    2. GREEN: on a stop, ffmpeg_deactivate() writes the packets still queued, then drains each encoder (avcodec_send_frame(ctx, NULL)) and writes what it returns, before the trailer. The stop time still applies, and a write failure still drains nothing.
    3. RED and GREEN in both lanes, full ctest in both lanes, then the release's Custom Output check on the fixed build.
  2. added
    claimed
    on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions