Subtitles for AVPlayer / MPMoviePlayerController

I use m3u8 video format for streaming video, and now I need to display subtitles for them.

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

I'm interested in knowing what subtitle format should be? Will there be a .srt format?

Also can I achieve the same using MPMoviePlayerController?

Any help is appreciated.

+5
source share
3 answers

, WebVTT m3u8 . " ", , (. Apple ( )).

- - , ;-). (ZIP) Chris Adamson, Apple , . .

, AVMediaTypeText AVMediaTypeSubtitle. , 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]];
+13

AVPlayer MPMoviePlayerController .

, AVPlayer , closedCaptionDisplayEnabled.

MPMoviePlayerController , "". .

+2

iOS WWDC 2013 WebVTT ( ). - , 50 ( ).

I don’t know if WebVTT is supported at the AVFoundation or MPMoviePlayer level, or whether the application downloads and analyzes the subtitle files themselves.

A quick search of "webvtt m3u8" showed a link to this HTTP streaming project , which suggests that you can do this work simply by referencing the WebVTT files in your m3u8 playlist. However, I have no example for you, since I could not trivially guess the m3u8 URL for the WWDC session.

0
source

All Articles