muukii / SwiftSubtitles

A Swift package for reading/writing subtitle formats (srt, svb, vtt)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift Subtitles

A Swift package for reading/writing some common subtitle formats.

Swift Package Manager

Available coders

Format Coder File extension
SBV (SubViewer) Subtitles.Coder.SBV .sbv
SUB (MicroDVD)* Subtitles.Coder.SUB .sub
SRT (SubRip) Subtitles.Coder.SRT .srt
VTT (WebVTT) Subtitles.Coder.VTT .vtt
  • Read-only

Basic usage

Decoding

Basic decoding uses the file extension to determine the coder to use when decoding.

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

You can also instantiate a coder object and use that directly if you know the type of subtitles you'll be decoding

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

Encoding

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)

Limitations

  • Some VTT functionality is not supported (NOTE, STYLE, REGION). These will be discarded on import.

License

MIT. Use it for anything you want, just attribute my work if you do. Let me know if you do use it somewhere, I'd love to hear about it!

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.

About

A Swift package for reading/writing subtitle formats (srt, svb, vtt)

License:MIT License


Languages

Language:Swift 100.0%