SwiftSubtitles 1.5.2

SwiftSubtitles 1.5.2

Darren Ford 具有。



 
依赖于
DSFRegex~> 3.4.0
TinyCSV~> 1.0.0
 

  • 作者:
  • Darren Ford

Swift Subtitles

一个用于读取/写入某些常用字幕格式的 Swift 包。

tag Swift License MIT SPM Build

macOS iOS tvOS watchOS macCatalyst Linux

可用的程序员

格式 程序员 文件扩展名
SBV (SubViewer) Subtitles.Coder.SBV .sbv
SUB (MicroDVD)* Subtitles.Coder.SUB .sub
SRT (SubRip) Subtitles.Coder.SRT .srt
VTT (WebVTT) Subtitles.Coder.VTT .vtt
CSV Subtitles.Coder.CSV .csv
  • 只读

基本使用

解码

基本解码利用文件扩展名来确定解码时使用的编码器。

let subtitles = try Subtitles(fileURL: <some file url>)
subtitles.cues.forEach { cue in
	// Do something with 'cue'
}

如果您知道要解码的字幕类型,也可以实例化一个编码器对象并直接使用它。

let subtitleContent = ...
let coder = Subtitles.Coder.SBV()
let subtitles = try coder.decode(subtitleContent)
...
let encodedContent = try coder.encode(subtitles: subtitles)

编码

let cue1 = Subtitles.Cue(
	position: 1,
	startTime: Subtitles.Time(minute: 10),
	endTime: Subtitles.Time(minute: 11),
	text: "점점 더 많아지는\n시민들의 성난 목소리로..."
)

let cue2 = Subtitles.Cue(
	position: 2,
	startTime: Subtitles.Time(minute: 13, second: 5),
	endTime: Subtitles.Time(minute: 15, second: 10, millisecond: 101),
	text: "Second entry"
)

let subtitles = Subtitles([cue1, cue2])

// Encode based on the subtitle file extension
let content = try Subtitles.encode(subtitles, fileExtension: "srt")

// Encode using an explicit coder
let coder = Subtitles.Coder.VTT()
let content2 = try coder.encode(subtitles: subtitles)

CSV 编码/解码

似乎没有针对字幕的正式 CSV 规范,所以这个编码器试图创建一个通用的“足够”编码器/解码器,以便应用程序更容易导出到电子表格或谷歌文档中。

CSV 必须符合 RFC 4180

  • 包含双引号的文本必须使用双重引号(例如:">> ALICE: 我的猫名叫 ""cat"" 并且非常自大")
  • 包含换行符的文本必须包含在引号内。(例如:">> ALICE: 你呢?\n>> ROB: 我没有意见")

这个库使用 TinyCSV 进行 CSV 编码/解码。

在解码过程中,如果存在标题,编码器会忽略它,并假设特定列的顺序

行格式

CSV 的标题文字并不重要,字段的顺序决定了预期数据类型

检测到的行格式 :-

  • <位置>, <开始时间>, <结束时间>, <文本>

  • 位置:字幕文本(提示)在字幕中的位置

  • 开始时间:文本出现在屏幕上的时间

  • 结束时间:文本从屏幕上消失的时间

  • 要显示的文本

支持的解码时间格式

  • SBV风格: 00:00:00.000
  • SRT风格: 00:00:00,000
  • 常见风格: 00:00:00:000
  • 毫秒值: 102727

使用常见风格文本格式的示例

No.,Timecode In,Timecode Out,Subtitle
1, 00:00:00:599, 00:00:04.160, ">> ALICE: Hi, my name is Alice Miller and this is John Brown"
2, 00:00:04:160, 00:00:06.770, ">> JOHN: and we're the owners of ""Miller Bakery""."
Position,Start time,End Time,Text
51,00:00:00:599,00:00:04.160,">> ALICE: Hi, my name is Alice Miller and this is John Brown"
52,00:00:04:160,00:00:06.770,">> JOHN: and we're the owners of ""Miller Bakery""."

使用毫秒持续时间和包含文本中的换行符的示例

1, 91216, 93093, "РегалВю ТЕЛЕМАРКЕТИНГ
АНДЕРСЪН - МЕНИДЖЪР"
2, 102727, 104562, "Тук пише, че 5 години сте бил"
3, 104646, 107232, "мениджър на ресторант ""Ръсти Скапър""."

限制

  • 一些VTT功能不受支持(NOTE, STYLE, REGION)。这些将在导入时被丢弃。

许可证

MIT。你可以用它做任何你想要的事情,只要在用到它的时候注明我的工作。如果用它了,告诉我,我很乐意听到这个消息!

MIT License

Copyright (c) 2023 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.