Giter VIP home page Giter VIP logo

swiftsubtitles's Introduction

Swift Subtitles

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

tag Swift License MIT SPM Build

macOS iOS tvOS watchOS macCatalyst Linux

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
CSV Subtitles.Coder.CSV .csv
  • 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)

CSV coding/encoding

There appears to be no formal CSV specification for subtitles, so this coder tries to make a generic "enough" encoder/decoder to make it easier for an app to export into a spreadsheet or google docs.

Click here for more details on supported CSV formats

The CSV must conform to RFC 4180

  • Text that contains double-quotes must be double-double-quoted (eg. ">> ALICE: My cat is named ""cat"" and is quite arrogant")
  • Text containing newlines must be encapsulated in quotes. (eg. ">> ALICE: What about you?\n>> ROB: I don't have an opinion")

This library uses TinyCSV for CSV coding/decoding.

During decoding, the coder ignores the header if it exists, and assumes a particular ordering for the columns

Row format

The header text for the CSV is not important, the ordering of the fields determine what type of data to expect

Detected row formats :-

  • <position>, <start-time>, <end-time>, <text>

  • position: The position of the cue (subtitle text) within the subtitles

  • start-time: The time where the text appears on the screen

  • end-time: The time where the text is removed from the screen

  • The text to display

Time formats supported for decoding

  • SBV style: 00:00:00.000
  • SRT style: 00:00:00,000
  • Common style: 00:00:00:000
  • milliseconds: 102727

Examples using common style text formats

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""."

An example using millisecond durations and containing line feeds within the text

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

Limitations

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

License

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.

swiftsubtitles's People

Contributors

d108 avatar dagronf avatar muukii avatar thisisthefoxe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swiftsubtitles's Issues

Cues is not create

my srt text (String) in Turkish language;

1
0:00:00.000 --> 0:00:02.000
Merhaba arkadaşlar.
2
0:00:02.000 --> 0:00:04.000
Kanalıma hoş geldiniz.
3
0:00:04.000 --> 0:00:07.000
Bugün sizler için niyet ettim.
4
0:00:07.000 --> 0:00:10.000
Falk kapattım sizler de direğinizi tutup.
5
0:00:10.000 --> 0:00:12.000
Falınızı dinleyebilirsiniz.
6
0:00:12.000 --> 0:00:15.000
Ama öncelikle sizden rica'm.
7
0:00:15.000 --> 0:00:18.000
Kanalıma abone olup videolarımı beğenmeyi,
8
0:00:18.000 --> 0:00:22.000
yorum yapmayı ve bildirimlerini açmayı unutmayınız.
9
0:00:22.000 --> 0:00:24.000
Şimdiden teşekkür ediyorum.
10
0:00:24.000 --> 0:00:30.000
Ve felınıza geçiyorum.

decoding codes;
let coder = Subtitles.Coder.SRT() let subtitles = try? coder.decode(text)
When I try to print them, I get a nil value.

Feature Request: Add Subtitle Concatenation Functionality

First off, thank you for your great work on this library. It has been very useful for handling subtitle files.

I would like to request a new feature that allows concatenating subtitle cues into a continuous sentence. This functionality would be beneficial for applications that need to display subtitles as a single, coherent text instead of individual cues.

Implementing this feature would enhance the library's capabilities and provide a convenient solution for developers working with subtitles.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.