Out of sync between video and audio of video exported by AVAssetExportSession

I am using AVAssetExportSession for encoding video. For some source files(not all files), there is out of sync between video and audio after exporting by AVAssetExportSession without editing.

This issue causes when I put AVMutableComposition to asset parameter in initialization of AVAssetExportSession.(There is no out of sync when I put source asset directly to asset parameter.) I just created new AVMutableComposition. And I inserted video track and audio track of source asset to AVMutableComposition. But, the result of export session has out of sync. The audio of exported video is faster by about 0.05 seconds than the original one.

Through some searches I doubt this problem is related to Encoder delay(Encoder delay). Because 0.05 seconds with 44100Hz is about 2112 samples. But I am not sure. Can it resolve with AVAssetExportSession? Or Should I use AVAssetReader/AVAssetWriter or others which is more controllable than AVAssetExportSession? Are there any solutions?

Can you share the code you use to construct the AVMutableComposition (including the insertion of the video and audio tracks)?
Thank you for the reply. Here is my code I use to construct AVMutableComposition.

Code Block
let composition = AVMutableComposition()
let videoTrack = composition.addMutableTrack(
  withMediaType: AVMediaType.video,
  preferredTrackID: kCMPersistentTrackID_Invalid
)
let audioTrack = composition.addMutableTrack(
  withMediaType: AVMediaType.audio,
  preferredTrackID: kCMPersistentTrackID_Invalid
)
let assetVideoTrack = asset.tracks(withMediaType: AVMediaType.video).first
let assetAudioTrack = asset.tracks(withMediaType: AVMediaType.audio).first
let assetDuration = assetVideoTrack?.timeRange.duration
  ?? assetAudioTrack?.timeRange.duration
  ?? CMTime.zero
let timeRange = CMTimeRangeMake(start: CMTime.zero, duration: assetDuration)
let atTime = CMTime.zero
do {
  if let assetVideoTrack = assetVideoTrack {
   try videoTrack?.insertTimeRange(
    timeRange,
    of: assetVideoTrack,
    at: atTime
   )
  }
  if let assetAudioTrack = assetAudioTrack {
   try audioTrack?.insertTimeRange(
    timeRange,
    of: assetAudioTrack,
    at: atTime
   )
  }
} catch {
}


Have you tried using the value of AVAsset.duration instead of AVAssetTrack.timeRange to calculate the time range of the source tracks?
Yes, but it has still a sync problem. Also, AVAsset.duration and AVAssetTrack.timeRange.duration is actually the same in the log.
FYI, here is the stackoverflow link of my question. It has an image about audio tracks between source and exported video.
Thanks for the extra detail. I think at this point we would need you to file a bug report via the feedback assistant to dig in further. Most helpful would be to attach an example of an output movie file that is out of sync, plus the source movies. Including a project we can compile to repro would be ideal as well.
I uploaded same question to Feedback Assistant. I hope this problem will be resolved well and I will cooperate actively.
Thanks. For reference, can you share the feedback ID?
The feedback ID is FB8811460. Thanks!
Any updates about this issue? I have the same problem when compose video of 30s video with audio, tracks has same duration but when AVAssetExportSession finish, playing audio ends before video and have .5s os silence at end of videoclip
It looks like this will be fixed in an upcoming software update.
Do you know in which version will be resolved this issue?
Hi Team, We are also facing this issue as mentioned by Youngsoo... We are facing this more for Video with Mono - Audio Channel. Could you please let us know when this fix will be in place? We have live customers facing this issue in our app and if there's any workaround also... Please let me know

The resolution status of the issue I registered through Feedback Assistant is set to "Potential fix identified - In iOS 14.5" now. Also, when I tried some unsync cases in iOS 14.6, the problem is not reproduced.

Out of sync between video and audio of video exported by AVAssetExportSession
 
 
Q