Reputation: 31
I am creating an application that allows you to record a video and overlay a background and subtitles on it. Everything is working fine until I try to export the video with overlaid subtitles. If a video contains many subtitles, the RAM consumption increases dramatically and the application may crash. This happens more often on older devices as they have less RAM. This does not happen when playing videos with subtitles inside the app.
It seems to me that all CALayer remain in memory until the end of the method exportAsynchronously(completionHandler:
and are released only after it is completed.
Here is an example code that exports a video:
mainVideoComposition.animationTool = AVVideoCompositionCoreAnimationTool(
postProcessingAsVideoLayer: videoLayer,
in: outputLayer)
guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else {
errorCallback?(VLError.toError(title: "Could not create exporter"))
return
}
self.export = exporter
exporter.outputURL = exportURL
exporter.outputFileType = .mp4
exporter.videoComposition = mainVideoComposition
exporter.shouldOptimizeForNetworkUse = true
exporter.exportAsynchronously(completionHandler: { [weak self] in
guard let self = self else { return }
if let anError = exporter.error {
self.errorCallback?(anError)
} else if exporter.status == AVAssetExportSession.Status.completed {
self.completionCallback?(AVURLAsset(url: exportURL))
}
})
Is there a way to avoid the out of memory error?
I will appreciate any help, thanks a lot!
Upvotes: 1
Views: 142