I'm writing a video that's recorded in realtime while the app is running. The user has the option to insert bookmarks during this time.
I'm trying to write these bookmarks as chapters into the recorded video file, but I'm having some trouble.
ffplay displays the chapters with the right start and end times, but not the title. Also neither QuickTime Player nor AVAsset see the chapters.
This is how I set it up (assetWriter and videoInput not shown):
let metadataItem = AVMutableMetadataItem()
metadataItem.identifier = AVMetadataIdentifier.commonIdentifierTitle
metadataItem.value = "title" as (NSCopying & NSObjectProtocol)
metadataItem.dataType = kCMMetadataBaseDataType_UTF8 as String
metadataItem.extendedLanguageTag = "en"
let dummyGroup = AVTimedMetadataGroup(items: [metadataItem], timeRange: CMTimeRange(start: CMTime.zero, end: CMTime.invalid))
let metaFmt = dummyGroup.copyFormatDescription()
textWriterInput = AVAssetWriterInput(mediaType: .metadata, outputSettings: nil, sourceFormatHint: metaFmt)
textWriterInput.languageCode = "en"
textWriterAdaptor = AVAssetWriterInputMetadataAdaptor(assetWriterInput: textWriterInput)
assetWriter.add(textWriterInput)
videoInput.addTrackAssociation(withTrackOf: textWriterInput, type: AVAssetTrack.AssociationType.chapterList.rawValue)
and this happens when a bookmark is inserted (parameters time and title):
let metadataItem = AVMutableMetadataItem()
metadataItem.identifier = AVMetadataIdentifier.commonIdentifierTitle
metadataItem.value = title as (NSCopying & NSObjectProtocol)
metadataItem.dataType = kCMMetadataBaseDataType_UTF8 as String
metadataItem.extendedLanguageTag = "en"
let group = AVTimedMetadataGroup(items: [metadataItem], timeRange: CMTimeRange(start: time, duration: CMTime.invalid))
if textWriterInput.isReadyForMoreMediaData {
self.textWriterAdaptor.append(group)
}
This is what ffplay has to say about the recording:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/path/to/video.mp4':
Metadata:
major_brand : mp42
minor_version : 1
compatible_brands: isommp41mp42
creation_time : 2024-02-01T16:37:28.000000Z
Duration: 00:00:14.48, start: 0.142993, bitrate: 13226 kb/s
Chapters:
Chapter #0:0: start 6.727000, end 12.020000
Metadata:
title :
Chapter #0:1: start 12.020000, end 14.622000
Metadata:
title :
Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 2060x1080, 13268 kb/s, 57.81 fps, 300 tbr, 600 tbn (default)
Metadata:
creation_time : 2024-02-01T16:37:28.000000Z
handler_name : Core Media Video
vendor_id : [0][0][0][0]
Stream #0:1[0x3](und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 57 kb/s (default)
Metadata:
creation_time : 2024-02-01T16:37:28.000000Z
handler_name : Core Media Audio
vendor_id : [0][0][0][0]
Stream #0:2[0x4](eng): Data: bin_data (mebx / 0x7862656D), 0 kb/s (default)
Metadata:
creation_time : 2024-02-01T16:37:28.000000Z
handler_name : Core Media Metadata
2.05 A-V: -0.001 fd= 24 aq= 35KB vq= 6576KB sq= 0B f=0/0
Anyone got an idea why the titles/chapters won't show up?