转载

iOS 笔记-SRT视频字幕的解析与同步

srt字幕的格式

1 00:00:31,788 --> 00:00:34,020 {/a3}下面我给大家分享第四个问题  2 00:00:34,412 --> 00:00:35,740 {/a3}如何开办企业  3 00:00:36,140 --> 00:00:37,684 {/a3}如何开办企业呢  4 00:00:37,964 --> 00:00:39,060 {/a3}在这个问题当中  5 00:00:39,268 --> 00:00:41,692 {/a3}我将给大家讲两个方面的内容  6 00:00:41,892 --> 00:00:43,868 {/a3}第一个方面就是开办企业的流程  7 00:00:44,260 --> 00:00:45,500 {/a3}第二个方面的内容  
这是我的视频字幕。
首先解析出来视频字幕的时间和字幕内容。
     NSString *appendStr = @"http://192.168.1.60:5080/Mooc/caption/dc05f541-269c-4f1b-ad99-782f5adcefdf_1447910856426.srt";     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:appendStr]];          NSURLSession *session = [NSURLSession sharedSession];     NSURLSessionTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {         if (!error) {             // GBK编码             NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);                          // 解码             NSString *string = [[NSString alloc] initWithData:data encoding:enc];                          //按行分割存放到数组中             NSArray *singlearray = [string componentsSeparatedByString:@"/n"];                                       _subtitlesarray = [NSMutableArray array];             _begintimearray = [NSMutableArray array];             _endtimearray = [NSMutableArray array];                          //遍历存放所有行的数组             for (NSString *ss in singlearray) {                                  NSRange range = [ss rangeOfString:@"{//a3}"];                                  NSRange range2 = [ss rangeOfString:@" --> "];                                  if (range.location != NSNotFound) {                     [_subtitlesarray addObject:[ss substringFromIndex:range.location + range.length]];                 }                 if (range2.location != NSNotFound) {                                          NSString *beginstr = [ss substringToIndex:range2.location];                     NSString *endstr = [ss substringFromIndex:range2.location+range2.length];                                           NSArray * arr = [beginstr componentsSeparatedByString:@":"];                     NSArray * arr1 = [arr[2] componentsSeparatedByString:@","];                                          //将开始时间数组中的时间换化成秒为单位的                     float teim=[arr[0] floatValue] * 60*60 + [arr[1] floatValue]*60 + [arr1[0] floatValue] + [arr1[1] floatValue]/1000;                     //将float类型转化成NSNumber类型才能存入数组                     NSNumber *beginnum = [NSNumber numberWithFloat:teim];                     [_begintimearray addObject:beginnum];                                          NSArray * array = [endstr componentsSeparatedByString:@":"];                     NSArray * arr2 = [array[2] componentsSeparatedByString:@","];                                          //将结束时间数组中的时间换化成秒为单位的                     float fl=[array[0] floatValue] * 60*60 + [array[1] floatValue]*60 + [arr2[0] floatValue] + [arr2[1] floatValue]/1000;                     NSNumber *endnum = [NSNumber numberWithFloat:fl];                     [_endtimearray addObject:endnum];                 }             }             NSLog(@" 开始时间数组-=-=-==-=%@",_begintimearray);             NSLog(@" 结束时间数组-=-=-==-=%@",_endtimearray);             NSLog(@" 字幕数组-=-=-==-=%@",_subtitlesarray);                      }else{             NSLog(@"error  is  %@",error.localizedDescription);         }     }];     [dataTask resume]; 

上面代码已经把开始时间,结束时间,字幕分别放到三个数组中了。

把时间换成秒为单位的,为了能与获取的当前时间相匹配然后再比较。

视频播放时开个定时器,监控当前时间,与开始时间,结束时间比较,在之间的就把相应的字幕放进去显示就对了

 self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeAction) userInfo:nil repeats:YES]; 
 - (void)timeAction {          //判断播放状态      if (_player.rate != 1) {         return;     }     NSInteger currentSecond = CMTimeGetSeconds(_player.currentItem.currentTime);      for (int i = 0; i<_begintimearray.count ; i++) {         NSInteger beginarr =  [_begintimearray[i] integerValue];         NSInteger endarr = [_endtimearray[i]integerValue];         if (currentSecond > beginarr && currentSecond< endarr) {             //同步字幕             _subtitlesLabel.text = _subtitlesarray[i];             NSLog(@" 字幕  %@",_subtitlesarray[i]);         }     }          //播放完成 暂停     if (currentSecond == (int)CMTimeGetSeconds(self.player.currentItem.duration)) {         [self.player pause];         [self updateUI];     } } 

这里提醒注意下,把时间都转为NSInteger类型进行比较,一开始使用的NSNumber比较,字幕显示混乱=。=

这样字幕显示出来了,如果字幕和视频的对话有偏差,就把定时器的时间调整的再小一点应该就好了~

正文到此结束
Loading...