AVPlayer/MPMoviePlayerController的字幕 [英] Subtitles for AVPlayer/MPMoviePlayerController

查看:172
本文介绍了AVPlayer/MPMoviePlayerController的字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用m3u8视频格式流式传输视频,现在我需要显示相同的字幕.

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

我在Apple文档中进行了搜索,发现可以通过使用AVPlayerclosedCaptionDisplayEnabled属性来实现此目的.

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

我想知道字幕的格式应该是什么? .srt格式可以吗?

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

我还能使用MPMoviePlayerController达到同样的目的吗?

Also can I achieve the same using MPMoviePlayerController?

感谢您的帮助.

推荐答案

更新03/06/2020:在GitHub上,jbweimar创建了一个示例项目,该项目使用了AVAssetResourceLoaderDelegate方法,看上去非常承诺: https://github.com/jbweimar/external-webvtt-example

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

更新10/30/2018::值得检查苹果工程师的答案(感谢 @allenlini 对于

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.

原始答案:

似乎在m3u8流描述中引用WebVTT文件是官方支持的方式.似乎并没有正式支持在事实之后"添加它们(请参阅 Apple工程师的此声明(页面底部)).

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.

请注意您需要如何使用AVMediaTypeText而不是AVMediaTypeSubtitle.这似乎是iOS中的错误.

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

这篇关于AVPlayer/MPMoviePlayerController的字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆