我正在尝试将H.264转换为m4v格式的HEVC文件,并且在获取AVAssetExportSession来保留原始文件的CreateDate时遇到问题。我尝试使用元数据值通过应用原始日期来强制使用该值,但这只会导致创建新的QuickTime标记DateTimeOriginal。 CreateDate,TrackCreateDate和MediaCreateDate都采用当前日期和时间。
这是我正在使用的代码(在macOS游乐场中)。
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.")
}
时间通常如下所示(七月是原始日期,而九月是当前日期):
---- 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