如何修改此SRT文件解析器? [英] How can I modify this SRT file parser?

查看:71
本文介绍了如何修改此SRT文件解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些很好的代码来解析stackoverflow上的.srt文件(使用Objective C解析SRT文件),如下所示:

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

我在电视连续剧中有许多.srt文件,其中包含很多贷方"字幕,这有点破坏了体验并对其进行了编码,给我留下了像这样的非顺序索引:

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

当我尝试导入文件时,这会使FCPX阻塞.我是NSScanner的新手,尝试了我能想到的一切,但都没有成功.我很乐意在修改上述内容中提供任何帮助,以完全跳过子索引行 (如果可能的话).我可以用单独的代码按顺序将它们重新添加.谢谢!

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!

更新:感谢您提出通过"while"循环skaak进行索引的建议,但是该问题似乎仍在挑战逻辑,因为它从未超过第一次通过(!!).日志如下所示-首先使用NSDictionary,然后附加到NSMutableString(对我而言可能更有用).请注意,在这两种情况下,第一个子项的确更改为1,但索引4,5,7保持不变,而不是重新编号为2,3,4.

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...

另一个令人费解的观察是,如果我放入loopCounter ++,它也表明"while"循环只会使我感到困惑,尽管我确实提到不熟悉NSScanner.

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.

推荐答案

尝试一下

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

这篇关于如何修改此SRT文件解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆