fix: re-encode MP3 and multichannel Opus audio when converting - #147
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 throughAVAssetReader.MP3 — ffmpeg can only mux it under the
mp4atag, and AVFoundation drops that track on open, soloadTracks(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
dOpsbox, which AVFoundation won't load. This one is worse than silence:assetProperty_Tracksfails 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 existingisCopyable(_: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
ConversionUITestscomment carried the same stale fact. Both corrected, plus a release note.