【问题标题】:Convert WEBVTT subtitle format to SRT format objective c将WEBVTT字幕格式转为SRT格式objective c
【发布时间】:2018-04-17 10:40:20
【问题描述】:

如何将WEBVTT字幕格式转换为SRT格式

WEBVTT 文件是这样的

WEBVTT

1
00:08.000 --> 00:12.000
Our remote engineer support program
is designed to assist you

00:12.000 --> 00:15.000
in solving operational 
or maintenance  problems 

SRT 格式

1
00:00:00,000 --> 00:00:18,032
Come on over in my direction
So thankful for that, it's such a blessin', yeah
Turn every situation into Heaven, yeah

2
00:00:18,032 --> 00:00:30,048
Oh, you are
My sunrise on the darkest day
Got me feelin' some kind of way
Make me wanna savor every moment slowly
Slowly

【问题讨论】:

  • 对格式规则有什么解释吗?你有什么尝试吗?那只是字符串和解析,不是吗?

标签: ios objective-c srt webvtt


【解决方案1】:
Here is my own solution,

+ (NSString *)srtSubtitleFromVTT:(NSString *)content {

  NSArray *lines = [self split:content];
  NSString *output = @"";
  NSInteger i = 0;
  NSError *error;
  NSString *newLine;

  for (NSString *line in lines) {
     newLine = line;
     NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

   if([[line stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\r "]]  rangeOfCharacterFromSet:notDigits].location == NSNotFound) {
        continue;
    }

    NSString *pattern1 = @"(\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})"; // '01:52:52.554'
    NSString *pattern2 = @"(\\d{2}):(\\d{2})\\.(\\d{3})"; // '00:08.301'

    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern1 options:0 error:&error];
    NSArray* matches = [regex matchesInString:line options:0 range: NSMakeRange(0, line.length)];

    NSString *pattern ;
    NSString *template;

    if (matches.count) {
        pattern = pattern1;
        template = @"$1:$2:$3,$4";
    } else {
        regex = [NSRegularExpression regularExpressionWithPattern: pattern2 options:0 error:&error];
        matches = [regex matchesInString:line options:0 range: NSMakeRange(0, line.length)];
        if (matches.count) {
            pattern = pattern2;
            template = @"00:$1:$2,$3";
        }
    }

    if (matches.count && !error) {
        i++;
        output = [output stringByAppendingString:@"\r\n\r\n"];
        output = [output stringByAppendingString:[@(i) stringValue]];
        output = [output stringByAppendingString:@"\r\n"];

        newLine = [line stringByReplacingWithPattern:pattern withTemplate:template error:&error];
    }

    if([newLine containsString:@"WEBVTT"]) {
        continue;
    }
    output = [output stringByAppendingString:newLine];
    output = [output stringByAppendingString:@"\r\n"];
}

    return output;
}


+ (NSArray *)split:(NSString *)content {

    NSArray *lines = [content componentsSeparatedByString: @"\n"];
    if (lines.count == 1) {
        lines  = [content componentsSeparatedByString: @"\r\n"];
        if (lines.count == 1) {
            lines  = [content componentsSeparatedByString: @"\n"];
        }
    }
    return lines;
}

//将此方法添加到NSString类文件中

- (NSString *)stringByReplacingWithPattern:(NSString *)pattern withTemplate:(NSString *)template error:(NSError **)error {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:error];
    return [regex stringByReplacingMatchesInString:self
                                           options:0
                                             range:NSMakeRange(0, self.length)
                                      withTemplate:template];
}

【讨论】:

  • 嗨@Ahd Radwan,找不到方法stringByReplacingWithPattern
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多