Repository navigation
Conversation
dallo28
force-pushed
the
fix/video-apac-audio-track
branch
from
October 24, 2026 21:29
46c0a7e to
64084a7
Compare
Videos recorded with Spatial Audio carry an APAC (ambisonic) track next to an AAC stereo track, and trimming one in Photos puts the APAC track first. Both the legacy converter and MediaEditorVideoExport took the first audio track and fed it, decoded as is, to the AAC encoder, which refuses any channel count other than its own (AVFoundationErrorDomain -11800, OSStatus -12780). Sending failed at once: the message got a red error mark while the "Processing" ring kept spinning. The same failure hit PCM audio (ProRes recordings, including stereo PCM when the preset encodes mono), 5.1 and other multichannel tracks. - Take an AAC track when the file has one, which skips rendering the APAC track and is what the camera mixed for stereo playback. - Decode audio straight to the encoder's channel count and layout, so Core Audio downmixes or renders whatever track is used: APAC and PCM ambisonics (ACN SN3D/N3D, B-format), 5.1, quad, stereo PCM. - Log conversion errors in FetchVideoMediaResource, and report the writer's error when the reader has none. Fixes https://bugs.telegram.org/c/67051 and https://bugs.telegram.org/c/67049
dallo28
force-pushed
the
fix/video-apac-audio-track
branch
from
October 24, 2026 22:22
64084a7 to
98ac708
Compare
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.
Fixes https://bugs.telegram.org/c/67051 and https://bugs.telegram.org/c/67049: some iPhone videos fail to send from the attachment menu. The message gets a red error mark at once while the "Processing" ring keeps spinning.
Cause
With "Record Sound: Spatial Audio", the Camera app writes an APAC track (Apple Positional Audio, 4-channel first-order ambisonics) next to an AAC stereo track. In the original file the AAC track comes first. Trimming the video in Photos rewrites it with the APAC track first and enabled, and the AAC track disabled:
TGMediaVideoConverter(unedited sends) andMediaEditorVideoExport(edited sends, stories) both take the first audio track and decode it with no channel constraint. The AAC writer input refuses the first buffer whose channel count differs from its own (AVFoundationErrorDomain -11800, OSStatus-12780), so the conversion fails before any upload starts. HDR isn't involved: the video track is the same HLG HEVC in both files.The same failure hits more than Spatial Audio. Testing every layout in the app showed it for 4- and 9-channel ambisonic PCM (ProRes Spatial Audio), B-format, quad and 5.1 tracks, and for plain stereo PCM whenever the quality preset encodes mono audio (AAC sources happen to be downmixed by the decoder, PCM ones are not).
Change
_preferredAudioTrackForAVAsset:inTGMediaVideoConverter,preferredAudioTrack(in:)inMediaEditorUtils.swift(the main video and collage/dual-video inputs; the added-music track is unchanged).audioSettingsForPreset:/configuration.audioSettings), so Core Audio downmixes or renders whatever track is used: APAC and PCM ambisonics (ACN SN3D/N3D, B-format) are rendered as a sound field (a source on the left stays on the left), 5.1 and quad are downmixed, stereo is folded to mono when the preset is mono. Mono and stereo AAC sources come out identical to before (same channels, length and level). The channel count stays the preset's, as for every other video: stereo at Medium and above (so a camera video's APAC is rendered to stereo), mono at Low and Very Low, none for animations and profile videos.FetchVideoMediaResourcelogs conversion and export errors (library and local-file paths, both pipelines) underFetchVideoResource; the legacy converter reports the writer's error when the reader has none (it reportednilbefore) and logs an exception raised byappendSampleBuffer."Send as file" still uploads the original file untouched, including the APAC track.
Logic
Both converters now handle audio the same way:
Pick the track. The first AAC track in the file, even if it is disabled (Photos disables it in trimmed Spatial Audio videos). With no AAC track, the first audio track, whatever its format.
Decode to the encoder's format. The reader outputs linear PCM with the channel count and layout of the AAC encoder, which come from the quality preset:
Encode with the same AAC settings as before. Sample-rate conversion stays with the encoder, as before.
In
MediaEditorVideoExportthe same settings go to theAVAssetReaderAudioMixOutput, so the editor's volume mix and an added music track are mixed into that same layout. When the encoder settings have no channel count (stickers, avatars), the reader settings staynil, as before.Testing
Built with
Make.py build --configuration=debug_sim_arm64and run in the iOS 27.0 simulator (iPhone 17e), logged in, sending each file on its own to Saved Messages from the attachment menu, then checking the app log and the converted file in the media cache:Edited sends (trimmed in Telegram's editor,
MediaEditorVideoExport): the reporter's trimmed file (AAC track used, L/R correlation 0.99 like the camera's AAC) and t10 (left-panned tone rendered left: L −10.0 dBFS, R −27.3 dBFS) both sent.Which track was used was checked on the output: the reporter's file now matches its AAC track (L/R correlation 0.99; the rendered APAC is 0.95), and t14, whose AAC track carries the tone in the left channel only, comes out like that AAC track rather than like the rendered APAC. Every output keeps the source length (minus the 0.1 s the legacy converter always trims) and level.
Not tested on a device. Please send a Photos-trimmed Spatial Audio video from the attachment menu on an iPhone 16 or 17.
Limitations
FetchManagerImplhas no error handler); an album with one failing item waits for the rest, then sends without it. The new log lines at least show why.