Subtitles for AVPlayer/MPMoviePlayerController

19,738

Solution 1

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]];

Solution 2

This is the Swift version of @JohannesFahrenkrug answer. Hope this is useful for someone:

    let localVideoAsset = Bundle.main.url(forResource: "L1C1P1C3", withExtension: "mp4")

    //Create AVMutableComposition
    let videoPlusSubtitles = AVMutableComposition()

    //Adds video track
    let videoTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

    try? videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, localVideoAsset.duration),
                                of: localVideoAsset.tracks(withMediaType: .video)[0],
                                at: kCMTimeZero)

    //Adds subtitle track
    let subtitleAsset = AVURLAsset(url: Bundle.main.url(forResource: "L1C1P1C3", withExtension: ".mp4.vtt")!)

    let subtitleTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .text, preferredTrackID: kCMPersistentTrackID_Invalid)

    try? subtitleTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, localVideoAsset.duration),
                                        of: subtitleAsset.tracks(withMediaType: .text)[0],
                                        at: kCMTimeZero)

Solution 3

Both AVPlayer and MPMoviePlayerController can display subtitles.

The difference seems to be that with AVPlayer, you control whether or not the subtitles are displayed using the closedCaptionDisplayEnabled property.

With MPMoviePlayerController, the user controls whether or not subtitles are displayed using a switch in the Setting app. You get no programatic control of this in the app.

Share:
19,738
Vasu N
Author by

Vasu N

Updated on June 04, 2022

Comments

  • Vasu N
    Vasu N about 2 years

    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.

  • Nico
    Nico almost 11 years
    closedCaptionDisplayEnabled toggles closed captions, not subtitles. They're two different things, and that property has no effect on subtitles. (One caveat: I have only tried this on OS X, not iOS.)
  • Taimur Ajmal
    Taimur Ajmal over 7 years
    Thanks alot for superb post. Can you please mention how can we set the metadata. let titleMetadataItem = AVMutableMetadataItem() titleMetadataItem.locale = NSLocale.current titleMetadataItem.key = AVMetadataCommonKeyTitle as (NSCopying & NSObjectProtocol)? titleMetadataItem.keySpace = AVMetadataKeySpaceCommon titleMetadataItem.value = self.assetObject.title as (NSCopying & NSObjectProtocol)?
  • Martin Koles
    Martin Koles about 7 years
    @Johannes Fahrenkrug: Is this solution working with real HLS stream from remote m3u8 playlist please? I am trying to do it in Swift with no luck. Can you please check my post: stackoverflow.com/questions/43161124/…. Thanks!
  • Johannes Fahrenkrug
    Johannes Fahrenkrug about 7 years
    @MartinKoles I think it should work. The best way to find out is just to try it :) Maybe you can post your results here!
  • Martin Koles
    Martin Koles about 7 years
    @JohannesFahrenkrug I have added my Swift 3 version of your code as a separate answer here, but I am getting problem to get the mutable media track from the composition. Maybe I am doing some wrong, can you please check?
  • Deepak Saki
    Deepak Saki about 7 years
    I want to play radio url with format .m3u8. how it is possible?
  • Estel
    Estel about 7 years
    My understanding is that AVComposition does not work with any HTTP streaming assets, so this solution will not work.
  • allenlinli
    allenlinli about 6 years
    Here is a nice article for post "Adding Subtitles From SEPARATE WebVTT File to AVAsset/PlayerItem" forums.developer.apple.com/thread/45060
  • Johannes Fahrenkrug
    Johannes Fahrenkrug about 6 years
    @allenlinli Wow, that's a great solution! Thanks for sharing!!
  • m_katsifarakis
    m_katsifarakis over 5 years
    @JohannesFahrenkrug this is a wonderful solution! Thanks! The only problem I seem to have is that when adding subs using AVMediaTypeText instead of AVMediaTypeSubtitle, you don't get to turn the subtitles on and off, or select between multiple languages in AVPlayerViewController. Anyone has any workaround for that?
  • Johannes Fahrenkrug
    Johannes Fahrenkrug over 5 years
    @m_katsifarakis Thank you, I wasn't able to find a solution for that. I haven't worked on this for a few years, so it's worth trying whether AVMediaTypeSubtitle works now. If not, another more involved solution could be to run an http server inside of your app in order to deliver a m3u8 file. It might be crazy enough to work :)
  • m_katsifarakis
    m_katsifarakis over 5 years
    @JohannesFahrenkrug I am currently trying just that :) Preparing a custom m3u8 file. If that works out I will add my solution here. Best regards
  • Johannes Fahrenkrug
    Johannes Fahrenkrug over 5 years
    @m_katsifarakis Cool! Yes, please post an update! Also make sure to check out this link: forums.developer.apple.com/thread/45060#202064
  • Pallav Trivedi
    Pallav Trivedi almost 5 years
    @MartinKoles Did you get it working? I am stuck at the same point. Need to use subtitles (vtt) with HLS. Please post your solution/work around.
  • Martin Koles
    Martin Koles almost 5 years
    @PallavTrivedi My solution was to make vtt files available on the backend and properly reference them in the m3u8 file. It's working since then.
  • SafeFastExpressive
    SafeFastExpressive over 3 years
    I'm confused on how the URL localVideoAsset has a duration member?
  • Amin Rezaew
    Amin Rezaew about 2 years
    that's not working for me, video and audio is ok but subtitle not showing(my subtitle is .srt), my video have two audio, you know how can i switch on my second audio?
  • perpeer
    perpeer about 2 years
    I added the subtitles as VTT, can you try that way? For multiple audio files, you can try adding from the list you got from the video like that; videoAsset.loadTracks(withMediaType: .audio).first
  • Amin Rezaew
    Amin Rezaew about 2 years
    this code worked when i changed my SRT subtitle to VTT, please edit and add this to your answer, thanks =)
  • Amin Rezaew
    Amin Rezaew about 2 years
    subtitle have black background you know how can i delete that? and is it possible to change subtitles color?