AVCaptureSession采集音频转MP4失真求助:LBMediaToolkit转换问题排查
Hey there, let's break down what might be causing that audio distortion you're seeing on iOS and QuickTime (but not browsers)—it's likely a format flag mismatch or configuration issue in your AudioStreamBasicDescription (ASBD) or conversion pipeline. Here are the key things to check:
1. Fix Your ASBD's Format Flags
The biggest red flag here is how you're setting mFormatFlags for your AAC LC target. Using MPEG4ObjectID.AAC_LC.rawValue directly is incorrect for the kAudioFormatMPEG4AAC format ID. The format flags for MPEG-4 AAC need to follow specific bitmask conventions, not just the raw object ID.
Replace your ASBD setup with this corrected version:
let asbd = AudioStreamBasicDescription( mSampleRate: 44_100, mFormatID: kAudioFormatMPEG4AAC, mFormatFlags: kAudioFormatFlags_MPEG4_AAC_LC, // Correct flag for AAC LC mBytesPerPacket: 0, mFramesPerPacket: 1024, mBytesPerFrame: 0, mChannelsPerFrame: 2, mBitsPerChannel: 0, mReserved: 0 ) return LBAudioConverter(convertingTo: asbd)
kAudioFormatFlags_MPEG4_AAC_LC is a pre-defined constant that correctly encodes the AAC LC object ID along with required flags for packed frames, which aligns with what iOS and QuickTime expect. Browsers tend to be more lenient with non-standard flag combinations, which explains why they play the audio fine.
2. Verify Input Audio Format Alignment
Make sure the original audio from your AVCaptureSession matches the expectations of your converter:
- Check if the input is stereo (2 channels) like your target ASBD. If the capture is mono, you'll need to handle upmixing properly (some converters do this automatically, but not all).
- Confirm the input sample rate is 44.1kHz. If it's a different rate (like 48kHz), the converter might be doing a sloppy sample rate conversion that causes distortion. You can check the input format using
CMSampleBufferGetFormatDescription()on the incoming buffers.
3. Validate Sample Timestamps
Distortion can also happen if the converted CMSampleBuffers have incorrect timestamps. iOS and QuickTime are strict about presentation timestamps (PTS) matching the actual audio duration, while browsers often ignore or correct small mismatches.
Double-check that your conversion pipeline preserves the original presentationTimeStamp from the capture buffers. If timestamps are being truncated, stretched, or reset, the player will play the audio at the wrong speed, leading to distortion.
4. Check Encoder Bitrate Settings
LBMediaToolkit might be using a default bitrate that's too low for stereo AAC LC. For good quality, aim for 128–256 kbps (stereo). If the converter allows customizing bitrate, try setting it explicitly—low bitrates can cause compression artifacts that are more noticeable in native players than browsers.
5. Isolate the Problem: Conversion vs. Writing
To narrow down whether the issue is in conversion or MP4 writing:
- Save the converted
CMSampleBuffers to a raw AAC file first, then play it in QuickTime/iOS. If it's still distorted, the problem is in the conversion step. - If the raw AAC is fine, then the issue is how you're muxing the audio into the MP4 container. Check that you're setting up the audio track correctly in your MP4 writer (e.g., correct channel layout, sample rate, and codec parameters).
Give these fixes a try—most likely the ASBD flag mismatch is the root cause. Let me know if you still run into issues after adjusting these settings!
内容的提问来源于stack exchange,提问作者Grzegorz Aperliński

