SRTParser

用于解析 SubRip 文本 (SRT) 字幕的 Swift 包。

CI

安装

你可以通过将 swift-srt-parser 添加到你的项目中,从而将其添加到 Xcode 工程。

https://github.com/mihai8804858/swift-srt-parser

如果你想在 SwiftPM 项目中使用 swift-srt-parser,只需将其添加到你的 Package.swift 文件中即可。

dependencies: [
  .package(url: "https://github.com/mihai8804858/swift-srt-parser", from: "1.0.0")
]

然后将产品添加到任何需要访问该库的目标中。

.product(name: "SRTParser", package: "swift-srt-parser"),

快速开始

private let parser = SRTParser()
let contents = ...
let srt = try parser.parse(contents)
let contents = try parser.print(srt)

示例

解析

let contents = """
1
00:02:17,440 --> 00:02:20,375
{\an8}Senator, we're making
our <b>final</b> approach into {u}Coruscant{/u}.

2
00:02:20,476 --> 00:02:22,501 X1:201 X2:516 Y1:397 Y2:423
{b}Very good, {i}Lieutenant{/i}{/b}.
"""

dump(try SRTParser().parse(contents))
▿ SRTParser.SRT
  ▿ cues: 2 elements
    ▿ SRTParser.SRT.Cue
      - counter: 1
      ▿ metadata: SRTParser.SRT.CueMetadata
        ▿ timing: SRTParser.SRT.Timing
          ▿ start: SRTParser.SRT.Time
            - hours: 0
            - minutes: 2
            - seconds: 17
            - milliseconds: 440
          ▿ end: SRTParser.SRT.Time
            - hours: 0
            - minutes: 2
            - seconds: 20
            - milliseconds: 375
        - coordinates: nil
        ▿ position: Optional(SRTParser.SRT.Position.topCenter)
          - some: SRTParser.SRT.Position.topCenter
      ▿ text: SRTParser.SRT.StyledText
        ▿ components: 5 elements
          ▿ SRTParser.SRT.StyledText.Component.plain
            ▿ plain: (1 element)
              - text: "Senator, we\'re making\nour "
          ▿ SRTParser.SRT.StyledText.Component.bold
            ▿ bold: (1 element)
              ▿ children: 1 element
                ▿ SRTParser.SRT.StyledText.Component.plain
                  ▿ plain: (1 element)
                    - text: "final"
          ▿ SRTParser.SRT.StyledText.Component.plain
            ▿ plain: (1 element)
              - text: " approach into "
          ▿ SRTParser.SRT.StyledText.Component.underline
            ▿ underline: (1 element)
              ▿ children: 1 element
                ▿ SRTParser.SRT.StyledText.Component.plain
                  ▿ plain: (1 element)
                    - text: "Coruscant"
          ▿ SRTParser.SRT.StyledText.Component.plain
            ▿ plain: (1 element)
              - text: "."
    ▿ SRTParser.SRT.Cue
      - counter: 2
      ▿ metadata: SRTParser.SRT.CueMetadata
        ▿ timing: SRTParser.SRT.Timing
          ▿ start: SRTParser.SRT.Time
            - hours: 0
            - minutes: 2
            - seconds: 20
            - milliseconds: 476
          ▿ end: SRTParser.SRT.Time
            - hours: 0
            - minutes: 2
            - seconds: 22
            - milliseconds: 501
        ▿ coordinates: Optional(SRTParser.SRT.Coordinates(x1: 201, x2: 516, y1: 397, y2: 423))
          ▿ some: SRTParser.SRT.Coordinates
            - x1: 201
            - x2: 516
            - y1: 397
            - y2: 423
        - position: nil
      ▿ text: SRTParser.SRT.StyledText
        ▿ components: 2 elements
          ▿ SRTParser.SRT.StyledText.Component.bold
            ▿ bold: (1 element)
              ▿ children: 2 elements
                ▿ SRTParser.SRT.StyledText.Component.plain
                  ▿ plain: (1 element)
                    - text: "Very good, "
                ▿ SRTParser.SRT.StyledText.Component.italic
                  ▿ italic: (1 element)
                    ▿ children: 1 element
                      ▿ SRTParser.SRT.StyledText.Component.plain
                        ▿ plain: (1 element)
                          - text: "Lieutenant"
          ▿ SRTParser.SRT.StyledText.Component.plain
            ▿ plain: (1 element)
              - text: "."

打印

let srt = SRT(cues: [
  SRT.Cue(
    counter: 1,
    metadata: SRT.CueMetadata(
      timing: SRT.Timing(
        start: SRT.Time(hours: 0, minutes: 2, seconds: 17, milliseconds: 440),
        end: SRT.Time(hours: 0, minutes: 2, seconds: 20, milliseconds: 375)
      ),
      coordinates: SRT.Coordinates(x1: 201, x2: 516, y1: 397, y2: 423),
      position: nil
    ),
    text: SRT.StyledText(components: [
      .plain(text: "Senator, we're making\nour "),
      .bold(children: [.plain(text: "final")]),
      .plain(text: " approach into "),
      .underline(children: [.plain(text: "Coruscant")]),
      .plain(text: ".")
    ])
  ),
  SRT.Cue(
    counter: 2,
    metadata: SRT.CueMetadata(
      timing: SRT.Timing(
        start: SRT.Time(hours: 0, minutes: 2, seconds: 20, milliseconds: 476),
        end: SRT.Time(hours: 0, minutes: 2, seconds: 22, milliseconds: 501)
      ),
      coordinates: nil,
      position: .bottomCenter
    ),
    text: SRT.StyledText(components: [
      .bold(children: [
        .plain(text: "Very good, "),
        .italic(children: [.plain(text: "Lieutenant")])
      ]),
      .plain(text: ".")
    ])
  )
])

print(try SRTParser().print(srt))
1
00:02:17,440 --> 00:02:20,375 X1:201 X2:516 Y1:397 Y2:423
Senator, we're making
our <b>final</b> approach into <u>Coruscant</u>.

2
00:02:20,476 --> 00:02:22,501
{\an2}<b>Very good, <i>Lieutenant</i></b>.

许可证

该库在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE