Skip to content

libobs: Keep audio frames sample accurate when remuxing - #360

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13249-remux-audio
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/13249-remux-audio

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Remuxing an MKV recording to MP4 or MOV now keeps the audio sample accurate, so the remuxed file no longer crackles in Final Cut Pro or QuickTime.

  • Matroska stores timestamps in milliseconds: AAC frames of 1024 samples at 48 kHz (21.333 ms) are stored as 0, 21, 43, 64, ... ms.
  • media_remux's process_packet() rescaled every packet's timestamps on their own. MP4/MOV use the sample rate as the audio time base and take each frame's length from the gap to the next timestamp, so the remuxed frames were 1008 or 1056 samples long instead of 1024 (1014/1015/1058 at 44.1 kHz). Players that honour those lengths stretch or overlap the audio by 16 to 32 samples at every frame boundary; VLC ignores them, which is why it sounded player-specific.
  • ffmpeg -c copy rescales audio timestamps of a stream copy with av_rescale_delta(), which keeps frames back to back as long as that fits the input's rounding. The fix does the same for audio packets, with the frame length from av_get_audio_frame_duration2() or the stream's frame size. A real gap in the recording (the input timestamp jumps) is followed, and a trimmed last frame keeps its shorter length. Video, other streams and audio without a known frame size are rescaled as before.
  1. test/cmocka: Add failing test for remuxed MKV audio timestamps: test_remux_audio_timestamps encodes a tone with FFmpeg's AAC encoder into an MKV, remuxes it with media_remux_job_create()/media_remux_job_process() and reads the result back. It needs no display. The cases are:

    • 48 kHz MKV to MP4: every frame starts 1024 samples after the previous one, and the last frame is 1024 samples long;
    • 44.1 kHz MKV to MP4 with a shorter last frame: the same, and the last frame keeps its length to the millisecond;
    • 48 kHz MKV to MOV;
    • 48 kHz MKV with 10 frames left out: the gap survives the remux (a fix that only counted samples would fail here).

    Today 200 of 200 frame starts are off the grid (186 of 190 in the gap case). 4 of 4 fail.

  2. libobs: Keep audio frames sample accurate when remuxing: rescales audio packets with av_rescale_delta(). 4 of 4 pass.

Motivation and Context

Fixes #359 (upstream obsproject/obs-studio#13249, reported by angeloruggieridj; benank and lucgrefte see the same). It is also the 2021 report obsproject/obs-studio#4740 (QuickTime Player and Final Cut Pro), where PatTheMav reproduced it, showed that updating FFmpeg did not help and concluded it was in how OBS uses FFmpeg for MKV input. Piipperi and DaiAoMori hit it in Final Cut Pro there too.

There is no upstream PR for this. obsproject/obs-studio#12224 (remuxed MP4s stutter in Premiere at 2x/4x) may be related but is about video and is not addressed here.

Why this one: MKV is the format OBS recommends for recording because it survives crashes, and remuxing to MP4 is the advertised way to get an editable file. Every MKV recording with AAC audio is affected, and the reports go back to 2021.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_remux_audio_timestamps: 4 of 4 cases fail at the RED commit and 4 of 4 pass at GREEN. The test is deterministic (no timing). ctest -V shows every case run, none skipped, both without a display and without xvfb, and with the installed system OpenGL module masked.
  • Full ctest at GREEN: 117 of 117 in each lane (rebased on current master).
  • An MKV with H.264 and AAC remuxed to MP4 by the patched libobs has the same audio timestamps and durations as ffmpeg -c copy gives (all frames 1024 samples except one 1016 where the encoder delay settles, and the same 624-sample last frame), the same audio payload, and the same video packets as before the change.
  • clang-format, gersemi and ci/test_registration.py are clean. The test links libobs, libavcodec, libavformat and libavutil (already required in test/cmocka), is registered on every platform and skips its cases if FFmpeg has no AAC encoder or Matroska muxer.

Not tested:

  • Listening in Final Cut Pro or QuickTime (no macOS here). The test checks the frame lengths those players read from the file.
  • The Remux dialog itself: it calls the same media_remux_job_* functions the test calls.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 10:42
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Checked locally in the Tier 2 Linux image (Ubuntu, FFmpeg/libavformat 8.0.1):

  • RED 92ca451: test_remux_audio_timestamps fails all 4 cases ✔
  • GREEN e60c861: 3/4 pass, but mkv_44k_to_mp4 still fails on the last-frame check:
44100 Hz MKV -> .mp4: 201 audio frames (expected 201), 0 of 200 frame starts off the 1024-sample grid, last frame 1024 samples long (encoded 624)
test_remux_audio_timestamps.c:299: error: Failure!

So the frame grid is fixed, but the short final frame comes out a full 1024 samples long on this FFmpeg. Which FFmpeg did you test with? Either the remuxer needs to set the last packet's duration from the source here, or the test needs to allow for this muxer behavior. Holding the merge until Tier 2 is green.

@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Follow-up on the mkv_44k_to_mp4 failure. I traced it, and it's the input, not the remuxer:

  • I logged each audio packet in process_audio_packet. On FFmpeg 8.0.1 the last packet of the 44.1 kHz MKV comes back from the demuxer with duration = 23 ms (a full 1024-sample frame) and no AV_PKT_DATA_SKIP_SAMPLES side data.
  • So the generated MKV never stores the 624-sample trim (no shorter BlockDuration, no DiscardPadding), and no remuxer can recover it.
  • I also tried honoring the skip-samples end trim (Matroska DiscardPadding) in process_audio_packet. That's harmless, but it changes nothing here because the side data isn't present.

Suggestion: in remux_and_check, take the expected last-frame length from what the MKV actually contains (demux it and read the last packet's duration and skip side data), not from the encoder's pkt->duration. The frame-grid checks, which are the real bug, already pass on all four clips.

Remuxing an MKV recording to MP4 or MOV made the audio crackle or pop in
Final Cut Pro and QuickTime, while the MKV and an MP4 made from it with
`ffmpeg -c copy` played cleanly.

Matroska stores timestamps in milliseconds, so AAC frames of 1024
samples at 48 kHz (21.333 ms) are stored as 0, 21, 43, 64, ... ms.
media_remux rescales each one on its own to the MP4 audio time base
(the sample rate), and MP4/MOV take each frame's length from the gap to
the next timestamp: the remuxed frames are 1008 or 1056 samples long
instead of 1024, and players that honour that stretch or overlap the
audio at every frame boundary.

The test encodes a tone with FFmpeg's AAC encoder into an MKV, remuxes
it with media_remux_job_create()/media_remux_job_process() and reads the
result back. It needs no display.

Cases:
- 48 kHz MKV to MP4: every frame must start 1024 samples after the
  previous one and the last frame must be 1024 samples long;
- 44.1 kHz MKV to MP4, with a shorter last frame: same, and the last
  frame keeps its length to the millisecond;
- 48 kHz MKV to MOV;
- 48 kHz MKV with 10 frames left out: the gap must survive the remux.

On master all 4 cases fail in both lanes (ENABLE_RUST_LIBOBS OFF and
ON): 200 of 200 frame starts are off the 1024-sample grid (1008/1056
samples at 48 kHz, 1014/1015/1058 at 44.1 kHz; 186 of 190 in the gap
case), and full last frames come out 1008 samples long.

Reported upstream in obsproject/obs-studio#13249 by angeloruggieridj,
also obsproject/obs-studio#4740.

Refs obs-rust#359.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe force-pushed the fix/13249-remux-audio branch from e60c861 to 83bd157 Compare October 12, 2026 10:53
@rdoupe

rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for tracing it: you're right, it's the test's expectation, not the remuxer. I tested with FFmpeg 9.0, whose Matroska muxer stores the trimmed last frame's shorter length; 8.0.1 writes it as a full frame, so there is nothing for the remux to keep.

Pushed 83bd157 (test commit amended, fix unchanged): remux_and_check now demuxes the generated MKV first and expects the last frame the MKV actually stores (exactly one frame if it's stored full, otherwise its length to the millisecond). The macOS and Windows Tier 2 lanes failed on the same case with the same output, so this should clear all three. RED still fails all 4 cases here (frames 1008/1056 samples apart); GREEN passes 4/4 in both lanes.

@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

I pushed a test-only commit on top of your branch: when the MKV itself stores a full-length last frame, remux_and_check now expects a full frame there. The exact-1024 check for full frames is unchanged. Checked on FFmpeg 8.0.1 (Tier 2 image):

  • With the amended test against the old remuxer (92ca451's media-remux.c): all 4 cases fail, so the test is still RED
  • With your fix: Tier 2 passes 118/118
  • Rebased on master, local gate passed; pushed as a0a4ee431.

@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Correction to my last comment: that push was rejected, because you had already pushed an equivalent test fix (83bd157). Nothing of mine is on the branch. Verifying your version now.

Remuxing an MKV recording to MP4 or MOV made the audio crackle or pop in
Final Cut Pro and QuickTime. Matroska stores timestamps in milliseconds,
so AAC frames of 1024 samples at 48 kHz are stored as 0, 21, 43, 64, ...
ms. media_remux rescaled every timestamp on its own, and MP4/MOV, which
take each audio frame's length from the gap to the next timestamp, got
frames of 1008 and 1056 samples. Players that honour those lengths
stretch or overlap the audio at every frame boundary.

For audio packets, place each frame right after the previous one with
av_rescale_delta() as long as that is within the input's rounding of the
packet's timestamp, and follow the input timestamp only when it really
jumps (a gap in the recording), as `ffmpeg -c copy` does. The frame
length comes from av_get_audio_frame_duration2() or the stream's frame
size; a packet whose stored duration is clearly shorter (the trimmed
last frame) keeps it. Video and other streams are rescaled as before,
and audio without a known frame size falls back to that too.

The remuxed audio of an MKV with H.264 and AAC now has the same
timestamps and durations as `ffmpeg -c copy` gives (stts all 1024
except one 1016 where the encoder delay settles), and its video packets
are unchanged.

test_remux_audio_timestamps passes (4 of 4) in both lanes.

Fixes obs-rust#359.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=6a3c50acc8c93a817d9f45f03f397bbf7f7b7406 secs=154 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791803333,"gate":"rust/all/fmt","host":"linux-x86_64","key":"f913e035837b8f61e375b5ecfc88438bb31f56021867a22b1c111b8bb2867f45","lane":"lint","parents":["fca25f0edced838bf39a868b197711ea96f56a49"],"secs":55,"stamp":"c2a511b88ed6c70a9d78c4987d102173","tree":"6a3c50acc8c93a817d9f45f03f397bbf7f7b7406","v":1,"via":"run"}
Ci-Attestation: {"at":1791803333,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"f913e035837b8f61e375b5ecfc88438bb31f56021867a22b1c111b8bb2867f45","lane":"lint","parents":["fca25f0edced838bf39a868b197711ea96f56a49"],"secs":55,"stamp":"6abb923b4a1920e66829d56a10f38ed6","tree":"6a3c50acc8c93a817d9f45f03f397bbf7f7b7406","v":1,"via":"run"}
Ci-Attestation: {"at":1791803333,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"ba225b53ec5240a59b5d65ab83299eaa7518beff17c9bfed0c008a3c6e937092","lane":"rust-test","parents":["fca25f0edced838bf39a868b197711ea96f56a49"],"secs":46,"stamp":"63bc7b6493661057c0e62858f0b88710","tree":"6a3c50acc8c93a817d9f45f03f397bbf7f7b7406","v":1,"via":"run"}
Ci-Attestation: {"at":1791803333,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"208ab29e33cba1418f55524b1f89ad674b20cec33d8d1e3288cbe0552ec2a057","lane":"tier2-linux","parents":["fca25f0edced838bf39a868b197711ea96f56a49"],"secs":52,"stamp":"6d15dc558a7e900583e06847309f5d91","tree":"6a3c50acc8c93a817d9f45f03f397bbf7f7b7406","v":1,"via":"run"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified 83bd157 on FFmpeg 8.0.1 (Tier 2 image):

  • RED: the test with the old media-remux.c fails all 4 cases
  • GREEN: Tier 2 passes 118/118
  • Rebased on master, local gate passed; pushed as a9187e6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs: remuxed MKV audio gets uneven frame lengths in MP4/MOV, crackling in QuickTime and Final Cut (upstream #13249)

2 participants