Adding captions to a video stream

I am trying to add accessibility features in our AVPlayer to our apple TV app. (swift application)


Our video content is hosted on a CDN. This CDN returns us a list of streams (mp4, flv, m3u8, etc) and a list of tracks for captions in a DFXP format.


From the documentation that I am consulting, it seems that I'd need a master m3u8 that would host the captions file in order to display them, but our CDN doesn't seem to provide that option. Is there a way to add a captions file to an AVPlayerItem dynamically?

Answered by Technology Evangelist in 119663022

There isn't a way to add them client-side in a direct sense, but you could download and modify the master playlist to point to the appropriate WebVTT file (with the requisite tags) before initializing the AVAsset with it. You could also leverage a custom URL scheme and AVAssetResourceLoader to handle the request for the initial m3u8 as well - both paths would allow you to modify the master playlist before handing it off to AVFoundation.

Accepted Answer

There isn't a way to add them client-side in a direct sense, but you could download and modify the master playlist to point to the appropriate WebVTT file (with the requisite tags) before initializing the AVAsset with it. You could also leverage a custom URL scheme and AVAssetResourceLoader to handle the request for the initial m3u8 as well - both paths would allow you to modify the master playlist before handing it off to AVFoundation.

So essentially, download the m3u8 file locally, edit it by adding the vtt url to it, and giving that local file url to the AVAsset?

I got this working. It was a horrible experience but I did succeed. My full explanation is too long to post here so I've stuck my long explanation and sample code on Github: https://github.com/kanderson-wellbeats/sideloadWebVttToAVPlayer . I dearly hope my link doesn't die. Apple doesn't want us posting long answers to questions but they're just fine forcing us to do weeks of ridiculous work for something that on another platform is an hour or less.
Adding captions to a video stream
 
 
Q