// 设置字幕字符串
- (void)setSrt:(NSString *)srt {
// 去除\t\r
NSString *lyric = [NSString stringWithString:srt];
lyric = [lyric stringByReplacingOccurrencesOfString:@"\r" withString:@""];
lyric = [lyric stringByReplacingOccurrencesOfString:@"\t" withString:@""];
NSArray *arr = [lyric componentsSeparatedByString:@"\n"];
NSMutableArray *tempArr = [NSMutableArray new]; // 存放Item的数组
NSMutableDictionary *itemDic = [NSMutableDictionary dictionary]; // 存放歌词信息的Item
__block NSInteger i = 0; // 标记, 0:序号 1: 时间 2:英文 3:中文
for (NSString *str in arr) {
@autoreleasepool {
NSString *tempStr = [NSString stringWithString:str];
tempStr = [tempStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (tempStr.length > 0) {
switch (i) {
case 0:
[itemDic setObject:tempStr forKey:@"index"];
break;
case 1:{
//时间
NSRange range2 = [tempStr rangeOfString:@"-->"];
if (range2.location != NSNotFound) {
NSString *beginstr = [tempStr substringToIndex:range2.location];
beginstr = [beginstr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray * arr = [beginstr componentsSeparatedByString:@":"];
if (arr.count == 3) {
NSArray * arr1 = [arr[2] componentsSeparatedByString:@","];
if (arr1.count == 2) {
//将开始时间数组中的时间换化成秒为单位的
CGFloat start = [arr[0] floatValue] * 60*60 + [arr[1] floatValue]*60 + [arr1[0] floatValue] + [arr1[1] floatValue]/1000;
[itemDic setObject:@(start) forKey:@"start"];
NSString *endstr = [tempStr substringFromIndex:range2.location+range2.length];
endstr = [endstr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray * array = [endstr componentsSeparatedByString:@":"];
if (array.count == 3) {
NSArray * arr2 = [array[2] componentsSeparatedByString:@","];
if (arr2.count == 2) {
//将结束时间数组中的时间换化成秒为单位的
CGFloat end = [array[0] floatValue] * 60*60 + [array[1] floatValue]*60 + [arr2[0] floatValue] + [arr2[1] floatValue]/1000;
[itemDic setObject:@(end) forKey:@"end"];
}
}
}
}
}
break;
}
case 2:
[itemDic setObject:tempStr forKey:@"en"];
break;
case 3: {
[itemDic setObject:tempStr forKey:@"ch"];
break;
}
default:
break;
}
i ++;
}else {
// 遇到空行,就添加到数组
i = 0;
NSDictionary *dic = [NSDictionary dictionaryWithDictionary:itemDic];
[tempArr addObject:dic];
[itemDic removeAllObjects];
}
}
}
_srtArr = tempArr;
}
iOS SRT字幕解析
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- ERROR ITMS-90166: "Missing Code Signing Entitlements. No ...