How can I modify this SRT file parser?

一曲冷凌霜 提交于 2021-02-11 14:41:42

问题


I found some good code for parsing .srt files on stackoverflow (Parsing SRT file with Objective C) shown below:

NSScanner *scanner = [NSScanner scannerWithString:[theTextView string]];
while (![scanner isAtEnd])
{
    @autoreleasepool
    {
        NSString *indexString;
        (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];

        NSString *startString;
        (void) [scanner scanUpToString:@" --> " intoString:&startString];

        (void) [scanner scanString:@"-->" intoString:NULL];

        NSString *endString;
        (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];

        NSString *textString;
        (void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
        textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
        textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    indexString, @"index",
                                    startString, @"start",
                                    endString , @"end",
                                    textString , @"text",
                                    nil];

        NSLog(@"%@", dictionary);
    }
}

I have a number of .srt files from a TV series that contain a lot of ‘credit’ subs which kinda spoil the experience and coded them out, leaving me with non-sequential indexes like this:

// deleted subtitles
3
00:00:11,070 --> 00:00:14,466
Screenwriter: Name here...

4
00:00:14,633 --> 00:00:17,466
Music: Name here...

5
00:00:17,686 --> 00:00:20,680
Narrator: Name here...

// deleted subtitle
7
00:01:17,966 --> 00:01:21,966
Episode 12

which chokes FCPX when I try to import the file. I’m completely new to NSScanner and tried everything I can think of without success. I'd appreciate any help in modifying the above just to skip the sub index line altogether (if possible?). I'm okay with adding them back in sequentially with separate code. Thanks!

UPDATE: Thanks for your suggestion of indexing through the 'while' loop skaak, but the problem still seems to defy logic as it never increases beyond the very first pass (!!). The logs are shown below - firstly using an NSDictionary and then appending to an NSMutableString (probably more useful for my purposes). Note that in both cases the first sub does get changed to 1, but indices 4,5,7 remain unchanged rather than being renumbered 2,3,4.

2020-07-29 18:35:26.267 SRT Editor[12494:903] 
{
    end = "00:00:14,466";
    index = 1;
    start = "00:00:11,070";
    text = "Screenwriter: Hashida Sugako\n\n4 00:00:14,633 --> 00:00:17,466 Music: Sakada Koichi\n\n5 00:00:17,686 --> 00:00:20,680 Narrator: Naraoka Tomoko\n\n7 00:01:28,633 --> 00:01:34,233 It was early spring in 1958...
}

2020-07-29 18:51:15.612 SRT Editor[12646:903] 

1
00:00:11,07000:00:14,466Screenwriter: Hashida Sugako

4 00:00:14,633 --> 00:00:17,466 Music: Sakada Koichi

5 00:00:17,686 --> 00:00:20,680 Narrator: Naraoka Tomoko

7 00:01:28,633 --> 00:01:34,233 It was early spring in 1958...

Another puzzling observation is that if I put in a loopCounter++ it also suggests the 'while' loop only makes one pass through which baffles me, though I did mention being unfamiliar with NSScanner.


回答1:


Try this

NSUInteger index = 1;

NSScanner *scanner = [NSScanner scannerWithString:[theTextView string]];
while (![scanner isAtEnd])
{
    @autoreleasepool
    {
        NSString *indexString;
        (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];

        NSString *startString;
        (void) [scanner scanUpToString:@" --> " intoString:&startString];

        (void) [scanner scanString:@"-->" intoString:NULL];

        NSString *endString;
        (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];

        NSString *textString;
        (void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
        textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
        textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    // Use my own incremental index
                                    @( index ), @"index",
                                    startString, @"start",
                                    endString , @"end",
                                    textString , @"text",
                                    nil];

        NSLog(@"%@", dictionary);

        // Move to next index
        index ++;
    }
}


来源:https://stackoverflow.com/questions/63127546/how-can-i-modify-this-srt-file-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!