Swift 字幕

一个用于读取/写入一些常见字幕格式的 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
JSON (Podcasts Index) Subtitles.Coder.PodcastsIndex .json
LRC (歌词文件) Subtitles.Coder.LRC .lrc

基本用法

解码

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

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)

资源

Podcast Index 文本稿

格式文档 在这里

歌词文件格式

格式文档 在这里

该格式将亚秒级计时定义为“百分之一秒”,但是一些在线示例文件使用毫秒代替。此编码器支持两种格式进行解码,并且编码器在写入时支持两种格式。

[00:12.41]Is it that sweet? I guess so
[00:12.419]Is it that sweet? I guess so

CSV 编码/解码

对于字幕,似乎没有正式的 CSV 规范,因此该编码器尝试创建一个通用的“足够”的编码器/解码器,以使应用程序可以更轻松地导出到电子表格或 Google 文档中。

点击此处获取有关支持的 CSV 格式的更多详细信息

CSV 必须符合 RFC 4180

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

在解码期间,编码器会忽略标头(如果存在),并假定列的特定顺序。

默认情况下,编码器/解码器假定采用 <position>, <start-time>, <end-time>, <text> 格式,但是可以在编码器的初始化程序中配置它。

解码支持的时间格式

使用通用样式文本格式的示例

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, "мениджър на ресторант ""Ръсти Скапър""."

局限性

许可证

MIT License

Copyright (c) 2024 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.