Audio Video Out of Sync (Some Videos) on exporting through AVAssetExportSession

Issue Scope:

We are using AVMutableComposition and AVAssetExportSession to export the composed video to the server. In recent times, we are facing Video and Audio getting completely out of sync for some video files and the sync issue is very noticeable on exported video than the compiled preview through AVPlayer.

Note that this issue is happening on specific videos in recent times and the framework works well for other videos (recorded and imported)

AVMutableComposition:

We are using the below method for adding composition track for both audio and video tracks in assets with a preferred time scale - 600

Code Block
func addCompositionTrackOfType(_ mediaType: String, withMediaItems mediaItems: [MediaItem]) {
        if mediaItems.count > 0 {
            let trackID: CMPersistentTrackID = kCMPersistentTrackID_Invalid
            let compositionTrack: AVMutableCompositionTrack = self.composition.addMutableTrack(withMediaType: convertToAVMediaType(mediaType), preferredTrackID: trackID)!
            // Set insert cursor to 0
            self.cursorTime = CMTime(seconds: 0, preferredTimescale: 600)
            for  mediaItem in mediaItems {
                if mediaItem.startTimeInTimeline.isValid {
                    self.cursorTime = mediaItem.startTimeInTimeline
                }
                if let assetTrack = mediaItem.asset.tracks(withMediaType: convertToAVMediaType(mediaType)).first {
                    try? compositionTrack.insertTimeRange(mediaItem.timeRange, of: assetTrack, at: self.cursorTime)
                }
                self.cursorTime = CMTimeAdd(self.cursorTime, mediaItem.timeRange.duration)
            }
        }
    }


AVAssetExportSession:

Code Block
func makeExportable() -> AVAssetExportSession? {
        let supportedPresets: [String] = AVAssetExportSession.exportPresets(compatibleWith: self.composition)
        var presetName: String?
        switch UserDefaults.standard.videoQuality {
            case .low:
                let presets: [String] = [AVAssetExportPreset640x480, AVAssetExportPresetLowQuality]
                for preset: String in presets {
                    if supportedPresets.contains(preset) {
                        presetName = preset
                    }
                }
            case .medium:
                let presets: [String] = [AVAssetExportPreset960x540, AVAssetExportPreset640x480, AVAssetExportPresetMediumQuality, AVAssetExportPresetLowQuality]
                for preset: String in presets {
                    if supportedPresets.contains(preset) {
                        presetName = preset
                    }
                }
            case .high:
                let presets: [String] = [AVAssetExportPresetHighestQuality, AVAssetExportPreset1280x720, AVAssetExportPreset960x540, AVAssetExportPreset640x480, AVAssetExportPresetMediumQuality, AVAssetExportPresetLowQuality]
                for preset: String in presets {
                    if supportedPresets.contains(preset) {
                        presetName = preset
                    }
                }
        }
        if presetName == nil {
            DDLogError("Failed to find an export preset")
            return nil
        }
        DDLogInfo("export preset: \(presetName ?? "")")
        let session = AVAssetExportSession(asset: self.composition, presetName: presetName!)
        session?.videoComposition = self.preparedVideoComposition()
        session?.audioMix = self.audioMix
        return session
    }


Methods tried till now:
  • Modified Preferred Scale to different range (1 - 60,000).

  • Infusing encoder delay on Mon audio channels (not successful and impacting other videos).

  • Rewriting the imported videos with Asset Writer and then used it for composition.


Kindly provide and guide us to resolve this with any workaround or solution as early as possible.

Answered by Media Engineer in 671371022
Please file a report using the feedback assistant.
Accepted Answer
Please file a report using the feedback assistant.
Reported to Feedback assistance with ID - FB9083872

Thanks. Looks like this will be fixed in an upcoming software update.
Hi team, can you please confirm that if this issue is fixed in iOS - 14.5
Audio Video Out of Sync (Some Videos) on exporting through AVAssetExportSession
 
 
Q