Since closed-captions rendered by AVPlayer sometimes overlap with other UI components I would like to render cc in a separate view.
I am able to turn off AVPlayer's cc rendering by setting closedCaptionDisplayEnabled to NO but I did not find a way to extract the closed captions I want to render.
Does anybody know if there's a way to extract CC string from AVPlayer/AVPlayerItem? I'm able to identify the AVAssetTrack with AVMediaTypeClosedCaption but I am not sure how to extract the string for a certain time.
The key steps to "extracting" the caption strings are
let captionOutput = AVPlayerItemLegibleOutput()
.captionOutput.setDelegate(self, queue: DispatchQueue.main)
.player.currentItem?.add(captionOutput)
.extension ViewController: AVPlayerItemLegibleOutputPushDelegate {
func legibleOutput(_ output: AVPlayerItemLegibleOutput,
didOutputAttributedStrings strings: [NSAttributedString],
nativeSampleBuffers nativeSamples: [Any],
forItemTime itemTime: CMTime) {
// Your attributed caption strings get delivered here!
}
}
captionOutput.suppressesPlayerRendering = true
.I've created an example project here: https://github.com/balnaves/AVPlayerItemLegibleOutputTest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With