将WEBVTT字幕格式转换为SRT格式objective c

Convert WEBVTT subtitle format to SRT format objective c

如何将 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
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 = @"::,";
    } 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::,";
        }
    }

    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];
}