0 Replies
      Latest reply on Mar 20, 2020 5:52 AM by alabash
      alabash Level 1 Level 1 (0 points)

        Hi there!


        My goal is to trim audio quite precisely. I'm facing some strange issue when exporting it using AVAssetExportSession.

        The code is pretty straightforward.


        import UIKit
        import AVFoundation
        import PlaygroundSupport
        let asset: AVURLAsset = AVURLAsset(url: Bundle.main.url(forResource: "tmp", withExtension: "aac")!)
        let timeRange = CMTimeRange(
          start: CMTime(seconds: 20.0, preferredTimescale: asset.duration.timescale),
          end: CMTime(seconds: 25.0, preferredTimescale: asset.duration.timescale)
        let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)!
        exportSession.outputFileType = .m4a
        let fm = FileManager.default
        let tmpDirURL = FileManager.default.temporaryDirectory.appendingPathComponent("cut.m4a")
        try? fm.removeItem(at: tmpDirURL)
        exportSession.outputURL = tmpDirURL
        exportSession.timeRange = timeRange
        exportSession.exportAsynchronously {
          switch exportSession.status {
          case .completed:
          print("exportSession: \(exportSession.error?.localizedDescription ?? "error")")


        When I started analyzing results in the Audacity, I see that file is trimmed with some error which is very critical.



        If I align it by peaks (on the eye) I see ~500 ms error in this particular case. Error varies and repeats for different files I've tried.



        I've tried it with AVMutableComposition. Same result.

        Maybe I'm doing something wrong? Or am I missing something? I want files to be cut exactly by the time I set timeRange property of AVAssetExportSession.