如何从 AVPlayer 中的 mpd 文件中提取字幕?
Posted
技术标签:
【中文标题】如何从 AVPlayer 中的 mpd 文件中提取字幕?【英文标题】:How to extract subtitles from mpd files in an AVPlayer? 【发布时间】:2018-05-15 09:38:17 【问题描述】:我想在 AVPlayer 中显示字幕。字幕嵌入在 mpd 文件中。我需要从下面的 xml 中获取字幕并将其保存在文档目录中的某个位置。
我正在这里下载文件:
- (void)getSubTitle
NSURLSession *session = [NSURLSession sharedSession];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.dictResponse objectForKey:@"mpd_url"]]];
NSURLSessionDownloadTask *taskD = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error)
if (error == nil)
NSData *data = [NSData dataWithContentsOfURL:location];
NSString *context = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Subtitle: %@",context);
else
NSLog(@"Subtitle: %@",error.localizedDescription);
];
[taskD resume];
在sn-p下面得到这个作为来自服务器的响应:
<?xml version="1.0" ?>
<MPD mediaPresentationDuration="PT2M30.550S" minBufferTime="PT8.34S" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="static" xmlns="urn:mpeg:dash:schema:mpd:2011">
<!-- Created with Bento4 mp4-dash.py, VERSION=1.7.0-614 -->
<Period>
<!-- Video -->
<AdaptationSet maxHeight="720" maxWidth="1280" mimeType="video/mp4" minHeight="720" minWidth="1280" segmentAlignment="true" startWithSAP="1">
<SegmentTemplate initialization="$RepresentationID$/init.mp4" media="$RepresentationID$/seg-$Number$.m4s" startNumber="1" timescale="30000">
<SegmentTimeline>
<S d="250250" r="17"/>
<S d="12012"/>
</SegmentTimeline>
</SegmentTemplate>
<Representation band codecs="avc1.42C01F" frameRate="30000/1001" id="video/avc1" scanType="progressive" />
</AdaptationSet>
<!-- Audio -->
<AdaptationSet mimeType="audio/mp4" segmentAlignment="true" startWithSAP="1">
<SegmentTemplate initialization="$RepresentationID$/init.mp4" media="$RepresentationID$/seg-$Number$.m4s" startNumber="1" timescale="48000">
<SegmentTimeline>
<S d="400384" r="17"/>
<S d="19328"/>
</SegmentTimeline>
</SegmentTemplate>
<Representation audiosamplingRate="48000" band codecs="mp4a.40.2" id="audio/und/mp4a">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
</Representation>
</AdaptationSet>
<!-- Subtitles (Sidecar) -->
<AdaptationSet contentType="text" lang="en" mimeType="text/vtt">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="subtitle"/>
<Representation band id="subtitles/en">
<BaseURL>subtitles/en/en_vtt.txt</BaseURL>
</Representation>
</AdaptationSet>
</Period>
</MPD>
无法从这里提取字幕。
【问题讨论】:
解析 XML 以检索period.adaptationSets.last.representation.baseURL
,然后在此 URL 下载文件。
感谢您的回复。我得到了另一个使用 AVAsset 的解决方案。
好的,好的,那么请将您的解决方案作为自己的答案发布。 :)
谢谢,但请改用屏幕底部的蓝色“回答您的问题”按钮。
【参考方案1】:
I can get the subtitles using this:
AVAsset *avAsset = [mAVPlayer.currentItem asset];
NSArray *requestKeys = [NSArray arrayWithObjects:@"tracks",@"playable",nil];
[avAsset loadValuesAsynchronouslyForKeys:requestKeys completionHandler:^
AVKeyValueStatus status =[avAsset statusOfValueForKey:@"tracks" error:nil];
if(status == AVKeyValueStatusLoaded)
AVMediaSelectionGroup *subtitle = [avAsset mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicLegible];
];
【讨论】:
以上是关于如何从 AVPlayer 中的 mpd 文件中提取字幕?的主要内容,如果未能解决你的问题,请参考以下文章
想要在 AVPlayer 中播放视频时逐帧提取视频中的所有图像