Subtitles for AVPlayer/MPMoviePlayerController

Vasu N picture Vasu N · Jul 17, 2012 · Viewed 17.1k times · Source

I am using m3u8 video format for streaming the video and now I need to display subtitles for the same.

I searched in Apple Documentation and found that I can achieve this by using the closedCaptionDisplayEnabled property of AVPlayer.

I am interested to know what should be the format of subtitles? Will the .srt format do?

Also can I achieve the same using MPMoviePlayerController?

Any help is appreciated.

Answer

Johannes Fahrenkrug picture Johannes Fahrenkrug · Jun 21, 2016

Update 03/06/2020: On GitHub, jbweimar has created a sample project that uses the AVAssetResourceLoaderDelegate approach that looks very promising: https://github.com/jbweimar/external-webvtt-example


Update 10/30/2018: It's worth checking this answer by an Apple engineer (Thanks to @allenlini for pointing it out). He suggests a solution involving AVAssetResourceLoaderDelegate. I haven't tried it myself, but it might be a better solution than mine below.


Original Answer:

It seems as if referencing your WebVTT files in your m3u8 streaming description is the officially supported way. Adding them "after the fact" seems to not be officially supported (See this statement by an Apple engineer (bottom of the page)).

That - however - does not mean that you can't get it to work ;-). With the help of this great presentation and sample project (ZIP) by Chris Adamson, this post on the Apple Developer Forums and this Ray Wenderlich tutorial by Abdul Azeem, I was able to get it to work. This is a modified version of Abdul Azeem's sample code and Chris' sample project.

Note how you need to use AVMediaTypeText instead of AVMediaTypeSubtitle. This seems to be a bug in iOS.

// 1 - Load video asset
AVAsset *videoAsset = [AVURLAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"]];

// 2 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

// 3 - Video track
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                    ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                     atTime:kCMTimeZero error:nil];

// 4 - Subtitle track
AVURLAsset *subtitleAsset = [AVURLAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"subtitles" withExtension:@"vtt"]];

AVMutableCompositionTrack *subtitleTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeText
                                                                       preferredTrackID:kCMPersistentTrackID_Invalid];

[subtitleTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                       ofTrack:[[subtitleAsset tracksWithMediaType:AVMediaTypeText] objectAtIndex:0]
                        atTime:kCMTimeZero error:nil];

// 5 - Set up player
AVPlayer *player = [AVPlayer playerWithPlayerItem: [AVPlayerItem playerItemWithAsset:mixComposition]];