使用Objective C解析SRT文件

时间:2013-01-11 22:12:46

标签: objective-c parsing nsdictionary

文字示例:

1
00:00:00,000 --> 00:00:01,000
This is the first line

2
00:00:01,000 --> 00:00:02,000
This is the second line

3
00:00:02,000 --> 00:00:03,000
This is the last line

在JavaScript中,我会用正则表达式解析它。我只是想知道,这是在Obj C中做到这一点的最好方法吗?我确信我可以找到方式来做到这一点,但我想以适当的方式做到这一点。

我只需要知道从哪里开始,我很乐意做其余的事情,但为了理解我最终会得到这样的东西(伪代码):

NSDictionary
index -> [0-9]+
start -> hh:mm:ss,mmm
end -> hh:mm:ss,mmm
text -> one of the lines of text

在这种情况下,我将在我的字典中解析三个条目。

3 个答案:

答案 0 :(得分:11)

一些背景:我写了一个小应用程序并创建了一个名为stuff.srt的文件,其中包含驻留在bundle中的示例;因此,我的手段来访问它。

这只是一个快速而肮脏的事情,一个概念验证。请注意,它不会检查结果。真实应用程序始终检查其结果。如您所见,工作以-applicationDidFinishLaunching:方法进行(我在Mac OS X中工作,而不是iOS)。

修改

有人指出,最初发布的代码没有正确处理多个文本行。为了解决这个问题,我利用了SRT files use CRLF as their line breaks的事实,并搜索了这个序列的两次出现。然后,我根据观察到的here将文本字符串中出现的所有CRLF更改为空格。这不会考虑文本每行中的前导或尾随空格。

我将stuff.srt文件的内容更改为:

1
00:00:00,000 --> 00:00:01,000
This is the first line
and it has a secondary line

2
00:00:01,000 --> 00:00:02,000
This is the second line

3
00:00:02,000 --> 00:00:03,000
This is the last line
and it has a secondary line too

并且代码已经修改如下(我还将所有内容放入@autoreleasepool指令中;在解析文件的过程中可能会生成 lot 自动释放的对象!):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"srt"];

    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

    NSScanner *scanner = [NSScanner scannerWithString:string];

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

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

            // My string constant doesn't begin with spaces because scanners
            // skip spaces and newlines by default.
            (void) [scanner scanString:@"-->" intoString:NULL];

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

            NSString *textString;
            // (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&textString];
            // BEGIN EDIT
            (void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
            textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
            // Addresses trailing space added if CRLF is on a line by itself at the end of the SRT file
            textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            // END EDIT

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

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

修订后的输出如下:

2013-02-09 16:10:17.727 SRTFileScan[4846:303] {
    end = "00:00:01,000";
    index = 1;
    start = "00:00:00,000";
    text = "This is the first line and it has a secondary line";
}
2013-02-09 16:10:17.729 SRTFileScan[4846:303] {
    end = "00:00:02,000";
    index = 2;
    start = "00:00:01,000";
    text = "This is the second line";
}
2013-02-09 16:10:17.730 SRTFileScan[4846:303] {
    end = "00:00:03,000";
    index = 3;
    start = "00:00:02,000";
    text = "This is the last line and it has a secondary line too";
}

我从今天读到的内容中学到了另一件事:SRT文件格式起源于法国,输入中显示的逗号是那里使用的小数分隔符。

答案 1 :(得分:1)

答案 2 :(得分:0)

我的建议是使用NSDateFormatter来解析第二行。我会将该字符串拆分为两个字符串(请参阅components NSString class reference中的componentsSeparatedByString:)。这是在每行读取文件行时。

所以循环将是:

  • 如果文件再次包含数据,请阅读下一行;
  • 如果下一行是4的倍数,则分配一个新对象。该对象应该能够包含两个日期,一个整数和一个字符串;
  • 如果下一行不是4的倍数,请读取该行并将其值分配给相应的字段。