Reputation: 274
I'm trying to add chapters to an AVMutableComposition
. The video is created as expected and sent to the AVPlayer
. How ever I would like to include chapter marks (to use the Chapter marker option in AVplayer
).
I have tried the following to create the timed metadata... but than what to do??
When trying to add metadata to the AVMutableComposition
all items for metadata are get only.
let someAsset:AVURLAsset = AVURLAsset(url:URL(fileURLWithPath: "/tmp/temp.mov"))
let mutableComposition:AVMutableComposition = AVMutableComposition()
let start: Int64 = 0
let duration: Int64 = 500
let timeRange:CMTimeRange = CMTimeRange(start: CMTime(value: CMTimeValue(start), timescale: 1), end: CMTime(value: CMTimeValue(duration), timescale: 1));
var timedMetadataItems: [AVTimedMetadataGroup] = []
do {
try mutableComposition.insertTimeRange(timeRange, of: someAsset, at: mutableComposition.duration)
let metadata = AVMutableMetadataItem()
metadata.key = AVMetadataQuickTimeUserDataKeyChapter as NSCopying & NSObjectProtocol
metadata.value = "Test" as NSCopying & NSObjectProtocol
let timedMetadata = AVTimedMetadataGroup(items: [metadata], timeRange: timeRange)
timedMetadataItems.append(timedMetadata)
} catch let error as NSError {
Swift.print(error.localizedDescription)
}
return AVPlayerItem(asset: mutableComposition)
If doing this without writing to disk is really impossible (what feels strange) any help with saving the video with the chapters would be great as well. As I can only save normal meta data.
With AVAssetExportSession
it is possible to set the metadata and write the new composition to a file, but it’s not possible to write a AVTimedMetadataGroup
?
let export = AVAssetExportSession(asset: mutableComposition, presetName: AVAssetExportPresetPassthrough)
Upvotes: 2
Views: 1419
Reputation: 166
I do not think it is possible to add timed metadata to a composition. You will just have to work with the metadata in memory until you write it to disk.
The only way to write timed metadata to a file is to use an AVAssetWriter
.
To do this you need to set up the asset writer as usual, but then add a metadata track:
let formatDesc = aMetadataGroup.copyFormatDescription()
let metadataInput = AVAssetWriterInput(mediaType: .metadata, outputSettings: nil, sourceFormatHint: formatDesc)
metadataInput.expectsMediaDataInRealTime = false
writer?.add(metadataInput)
Then you set up a metadataAdaptor and use that to append the metadata groups whenever the metadataInput is ready.
let metadataAdaptor = AVAssetWriterInputMetadataAdaptor(assetWriterInput: metadataInput)
metadataInput.requestMediaDataWhenReady(on: metadataQueue, using: {
metadataAdaptor.append(metadataGroup)
})
You can retrieve the metadata from this file again by using a AVAssetReaderOutputMetadataAdaptor
with the metadata track.
Upvotes: 1
Reputation: 4134
It does not seem possible to add AVTimedMetadataGroup
directly to an AVComposition, however any timed metadata present in the videos you are combining will pass through.
I have verified that this works (although I haven't tried chapter markers specifically) for compositions exported with AVAssetExportSession
and for playback with AVPlayerItemMetadataOutputPushDelegate
This is covered briefly in the WWDC 2014 Session 505 "Harnessing Metadata in Audiovisual Media" video at 35:50
Upvotes: 0