// 设置字幕字符串
- (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 ...