Skip to content

fix: re-encode MP3 and multichannel Opus audio when converting - #147

Merged
itsjoshpark merged 1 commit into
mainfrom
fix/copyable-audio-mp3-opus
Aug 26, 2026
Merged

fix: re-encode MP3 and multichannel Opus audio when converting#147
itsjoshpark merged 1 commit into
mainfrom
fix/copyable-audio-mp3-opus

Conversation

@itsjoshpark

Copy link
Copy Markdown
Owner

The copyable audio table lists codecs AVFoundation is known to decode from an MP4, but two entries do not hold up against the real thing. Verified on macOS 15.7.9 / ffmpeg 9.0.1 by muxing each codec with the app's own arguments (-c copy -tag:v avc1 -f mp4) and decoding the result through AVAssetReader.

MP3 — ffmpeg can only mux it under the mp4a tag, and AVFoundation drops that track on open, so loadTracks(withMediaType: .audio) comes back empty. The converted file plays video and is silent. There is no tag that rescues it: ffmpeg's MP4 muxer rejects -tag:a ".mp3" outright.

Opus above 2 channels — 5.1 and 7.1 need channel mapping family 1 in the dOps box, which AVFoundation won't load. This one is worse than silence: assetProperty_Tracks fails with -11800 / 'what' and the whole file refuses to open. Mono and stereo decode fine.

Both are re-encoded to AAC now. Opus is capped by channel count rather than dropped from the table, because stereo Opus is the common case in Matroska and a blanket re-encode would cost it both a lossy generation and the fast copy path. The cap lives in a new isCopyable(audio:), mirroring the existing isCopyable(_:canDecodeAV1:) that already gates video on more than the codec name.

After the fix, both cases produce an MP4 with a decodable AAC track where a straight copy previously produced no track and an unopenable file.

Also checked and left alone: DTS and TrueHD genuinely fail, PCM decodes but stays re-encoded for size, and FLAC decodes at 2/5.1/7.1 channels and 24-bit/96k.

Docs

README line 53 still listed FLAC and Opus as codecs that get re-encoded — untrue since #122 made them copyable. A ConversionUITests comment carried the same stale fact. Both corrected, plus a release note.

The copyable audio table lists codecs AVFoundation is known to decode from an
MP4, but two entries do not hold up against the real thing.

ffmpeg can only mux MP3 under the `mp4a` tag, and AVFoundation drops that track
on open: the converted file plays video and is silent. Opus decodes at stereo
and below, but above two channels it carries a channel mapping family
AVFoundation will not load at all, so the whole MP4 fails to open rather than
merely losing its audio.

Re-encode both to AAC instead. Stereo Opus still copies untouched, so the common
case keeps its original audio.

The README also still named FLAC and Opus as codecs that get re-encoded, which
stopped being true when they were made copyable.
@itsjoshpark
itsjoshpark merged commit fd3f7a8 into main Aug 26, 2026
2 checks passed
@itsjoshpark
itsjoshpark deleted the fix/copyable-audio-mp3-opus branch August 26, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant