Repository navigation
libobs: Keep audio frames sample accurate when remuxing - #360
Conversation
|
Checked locally in the Tier 2 Linux image (Ubuntu, FFmpeg/libavformat 8.0.1):
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. |
|
Follow-up on the
Suggestion: in |
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]>
e60c861 to
83bd157
Compare
|
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): |
|
I pushed a test-only commit on top of your branch: when the MKV itself stores a full-length last frame,
|
|
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"}
83bd157 to
a9187e6
Compare
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.
media_remux'sprocess_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 copyrescales audio timestamps of a stream copy withav_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 fromav_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.test/cmocka: Add failing test for remuxed MKV audio timestamps:test_remux_audio_timestampsencodes a tone with FFmpeg's AAC encoder into an MKV, remuxes it withmedia_remux_job_create()/media_remux_job_process()and reads the result back. It needs no display. The cases are:Today 200 of 200 frame starts are off the grid (186 of 190 in the gap case). 4 of 4 fail.
libobs: Keep audio frames sample accurate when remuxing: rescales audio packets withav_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_LIBOBSOFF 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 -Vshows every case run, none skipped, both without a display and without xvfb, and with the installed system OpenGL module masked.ffmpeg -c copygives (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.ci/test_registration.pyare clean. The test links libobs, libavcodec, libavformat and libavutil (already required intest/cmocka), is registered on every platform and skips its cases if FFmpeg has no AAC encoder or Matroska muxer.Not tested:
media_remux_job_*functions the test calls.Types of changes
Checklist:
🤖 Generated with Claude Code