Saving audiobook removes chapters information

Hi

I'm wondering what the correct way to save an m4b file is. Even if I just open a file and then immediately save it, the chapters information gets lost, like in this straightforward function:

  func openSave(fileName: String, newFileName: String) {
    let fileUrl = URL(fileURLWithPath: fileName)
    let asset = AVURLAsset(url: fileUrl)
    let metadata = asset.metadata
    let exporter = AVAssetExportSession(asset: asset, 
      presetName: AVAssetExportPresetPassthrough)
    exporter?.outputURL = URL(fileURLWithPath: newFileName)
    exporter?.outputFileType = AVFileType.m4a
    exporter?.metadata = metadata
    exporter?.exportAsynchronously {
      if exporter?.status == .completed {
        print("completed")
      } else if exporter?.status == .failed {
        print("M4bFile.write error \(newFileName)")
      }
    }
  }

So what am I doing wrong? If I open a new file, all other tags are still there, but the chapter list is not, or rather asset.availableChapterLocales is empty.

Alex

Have you tried leaving the export session's metadata property nil rather than transferring the asset metadata yourself?

/* Specifies an NSArray of AVMetadataItems that are to be written to the output file by the export session.

   If the value of this key is nil, any existing metadata in the exported asset will be translated as accurately as possible into

   the appropriate metadata keyspace for the output file and written to the output. */

@property (nonatomic, copy, nullable) NSArray<AVMetadataItem *> *metadata;

Nothing changes, both if I just comment the line

exporter?.metadata = metadata

or overwrite it with

exporter?.metadata = nil

It's possible then that the chapter metadata is not supported by the destination file type. If you change it to .mov, do you get a better result?

Yes, it works. Thanks a lot for a help

Saving audiobook removes chapters information
 
 
Q