Search code examples
iosobjective-csrtwebvtt

Convert WEBVTT subtitle format to SRT format objective c


How can I convert WEBVTT subtitle format to SRT format

WEBVTT file is some thing like this

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 Format

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

Solution

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

    //Add this method to NSString category file

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