AVAssetExportSession exporting file with shorter duration

I've been using AVAssetExportSession to trim audio files for the past 2 years, and suddenly it stopped working properly. It still works fine when I run my app on a phone running iOS 16, but on my iOS 17 phone it exports an incorrect duration (ex. I'll provide a file with 2 seconds duration, ask it to trim it to 0 - 1.7s, it'll return the file overtrimmed at 1.58s or something like that). The AVURLAsset is returning the correct duration, I've already tried using the AVURLAssetPreferPreciseDurationAndTimingKey, it's useless to me, as the error happens somewhere during the export.

guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
            completion(false, nil)
            return
        }
            
        let startTime = CMTimeMakeWithSeconds(floor(startPoint * 100) / 100.0, preferredTimescale: 44100)
        let stopTime = CMTimeMakeWithSeconds(ceil(endPoint * 100) / 100.0, preferredTimescale: 44100)
        let exportTimeRange = CMTimeRange(start: startTime, end: stopTime)
        
        exportSession.timeRange = exportTimeRange
        exportSession.outputFileType = .m4a
        exportSession.outputURL = targetURL
        
        AudioHelper.deleteFile(at: exportSession.outputURL)

        exportSession.exportAsynchronously {
            ...
        }

I've managed to somewhat mitigate the damage by adding silence to the file and continuously trimming it until I get it close to my required duration, but it's an extremely ugly hack and it's breaking down the whole functionality of my app.

Replies

Did. you solve this? I solved this by changing preferredTimescale. It helped me.