-1

我想在 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];
}

获取以下代码段作为来自服务器的响应:

<?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 bandwidth="2138017" codecs="avc1.42C01F" frameRate="30000/1001" height="720" id="video/avc1" scanType="progressive" width="1280"/>
    </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" bandwidth="143079" 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 bandwidth="130" id="subtitles/en">
        <BaseURL>subtitles/en/en_vtt.txt</BaseURL>
      </Representation>
    </AdaptationSet>
  </Period>
</MPD>

无法从此处提取字幕。

4

1 回答 1

0
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]; 
} 
}];
于 2018-05-18T07:01:33.050 回答