AVAssetExportSession doesn't preserve the original date and time (macOS)

I'm trying to transcode an H.264 to HEVC file in m4v format, and I'm having problems getting AVAssetExportSession to preserve the CreateDate of the original file. I have tried forcing the value by applying the original date using metadata values, but this only results in a new QuickTime tag, DateTimeOriginal being created. The CreateDate, TrackCreateDate, and MediaCreateDate, all take the current date and time.


This is the code I'm using (in a macOS playground).


import Cocoa
import AVFoundation


let videoAsset = AVURLAsset(url: URL(fileURLWithPath: "input.mp4"))


AVAssetExportSession.determineCompatibility(ofExportPreset: AVAssetExportPresetHEVCHighestQuality, with: videoAsset, outputFileType: AVFileType.m4v, completionHandler: { (isCompatible) in
    if !isCompatible {
        print("Format not compatible.")
        return
    }})


if (AVAssetExportSession.exportPresets(compatibleWith: videoAsset).contains(AVAssetExportPresetHEVCHighestQuality)) {
    let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHEVCHighestQuality)


    exporter?.outputURL = URL(fileURLWithPath: "output.mp4")
    exporter?.outputFileType = AVFileType.m4v
    exporter?.shouldOptimizeForNetworkUse = true
    exporter?.canPerformMultiplePassesOverSourceMediaData = true


    let creationDate = AVMutableMetadataItem()
    creationDate.keySpace = videoAsset.creationDate?.keySpace
    creationDate.key = videoAsset.creationDate?.key
    creationDate.value = videoAsset.creationDate?.value
    print(creationDate)


    var newMetaData = videoAsset.metadata
    newMetaData.append(creationDate)
    print(newMetaData)


    exporter?.metadata = newMetaData


    exporter?.exportAsynchronously{() -> Void in
        switch exporter!.status {
        case .failed:
            print("Export failed: \(String(describing: exporter?.error?.localizedDescription))")
        case .cancelled:
            print("Export canceled.")
        case .completed:
            print("Successful!")
        default:
            break
        }
    }
}
else {
    print("Preset not compatible.")
}


The times generally look like this (where the July date is original, and the September date is current):


---- File ----
FileModifyDate: 2018:09:24 23:51:12+08:00
FileAccessDate: 2018:09:24 23:53:22+08:00
FileInodeChangeDate: 2018:09:24 23:51:12+08:00
---- QuickTime ----
CreateDate: 2018:09:24 15:49:50
ModifyDate: 2018:09:24 15:51:12
TrackCreateDate: 2018:09:24 15:49:50
TrackModifyDate: 2018:09:24 15:51:12
MediaCreateDate: 2018:09:24 15:49:50
MediaModifyDate: 2018:09:24 15:51:12
DateTimeOriginal: 2018:07:16 19:59:03Z


Any ideas?

Replies

Adding more information to my own question...


I've created my own AVAssetExportSession with AVAssetWriter and it seems that the metadata is handled at that level. Even I set it, or clear it, the current date and time get set as the created date. I am able to add the DateTimeOriginal only as an additional attribute that was not there originally (oddly enough). It would be good if AVAssetWriter had an option to force a specific create data rather than taking the current one. This is important in situations where you are transcoding and not creating new content.