srtgears

package module
v0.0.0-...-bb8c187 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: BSD-2-Clause Imports: 11 Imported by: 0

README

Srtgears™

Build Status Go Reference Go Report Card

Srtgears™ is a subtitle engine for reading subtitle files, manipulating / transforming them and then saving the result into another file.

Srtgears provides some very handy features which are not available in other subtitle tools, for example:

  • merge 2 subtitle files to have dual subs: one at the bottom, one at the top (this is not concatenation, but that's also supported)
  • lengthen / shorten display duration of subtitles (if you're a slow reader, you're gonna appreciate this :))
  • remove hearing impaired (HI) texts (such as "[PHONE RINGING]" or "(phone ringing)")
  • strip off formatting (such as <i>, <b>, <u>, <font>)
  • split the subtitle file at a specified time
  • statistics from the subtitles
  • etc...

Home page: https://srt-gears.appspot.com

Presentation

The Srtgears engine is presented in 3 ways:

1. Command line tool

Srtgears is available as a command line tool for easy, fast, scriptable and repeatable usage.

Binary (compiled) distributions are available on the download page:

https://srt-gears.appspot.com/download.html

The command line tool uses only the Go standard library and the srtgears engine (see below).

2. Web interface: online web page

Srtgears can also be used on the web for those who do not want to download the tool just try it out from the browser. It can be found here:

https://srt-gears.appspot.com/srtgears-online.html

The web interface is a Google App Engine project, implemented using the Go AppEngine SDK. The server side of the web interface uses the srtgears engine (see below).

The web folder is the root of the App Engine project. If you want to try it locally, you need to download the Go AppEngine SDK, and it can be started locally by running the goapp serve command of the SDK from the web folder.

3. Srtgears engine: a Go package

And last (but not least) a Go package for developers. The engine was designed to be independent from the command line and web interfaces, its API is clear, well documented and easy-to-use.

To get the source code (along with the sources of the tool and web interface), use go get:

go get github.com/icza/srtgears

Documentation can be found at:

http://godoc.org/github.com/icza/srtgears

To use the engine, first import it:

import "github.com/icza/srtgears"

And for example using the engine to merge 2 subtitle files to have a dual sub saved in Sub Station Alpha (*.ssa) format:

sp1, err := srtgears.ReadSrtFile("eng.srt")
check(err) // Check / handle error
sp2, err := srtgears.ReadSrtFile("hun.srt")
check(err) // Check / handle error
sp1.Merge(sp2)
err = srtgears.WriteSsaFile("eng+hun.ssa", sp1);
check(err) // Check / handle error

You can see more usage examples in the package doc.

Also worth noting that the subtitle transformations of the command line tool and the web interface are driven by the same Executor, it is "outsourced" to the github.com/icza/srtgears/exec package.

Limits

Input files must be UTF-8 encoded, output files will be UTF-8 encoded as well.

Supported input format is SubRip (*.srt) only, supported output formats are SubRip (*.srt) and Sub Station Alpha (*.ssa).

It should also be noted that SubRip format specification does not include subtitle positioning. Srtgears uses an unofficial extension {\anX} which may not be supported by all video players, or some players interpret the position values differently. MPC-HC has full support for it. In these cases the Sub Station Alpha output format is recommended (where the specification covers subtitle positioning / alignment).

Story

Srtgears started as a 48-hour work created for the Gopher Gala 2016 event. The initial version can be found here. I was competing solo.

License

See LICENSE

Documentation

Overview

Package srtgears implements a subtitle engine for reading subtitle files, manipulating / transforming them and then saving the result into another file.

Home page: https://srt-gears.appspot.com

Open source: https://github.com/gophergala2016/srtgears

Example (Concat)

This example shows how to concatenate 2 files where 2nd part of the movie starts at 51 min 15 sec.

package main

import (
	"github.com/icza/srtgears"
	"time"
)

func main() {
	sp1, err := srtgears.ReadSrtFile("cd1.srt")
	check(err) // Check / handle error
	sp2, err := srtgears.ReadSrtFile("cd2.srt")
	check(err) // Check / handle error
	secPartStart := time.Minute*51 + time.Second*15
	sp1.Concatenate(sp2, secPartStart)
	err = srtgears.WriteSrtFile("cd12.srt", sp1)
	check(err) // Check / handle error
}

func check(err error) {}
Output:

Example (Merge)

This example shows how to merge 2 subtitle files to have a dual sub saved in Sub Station Alpha (*.ssa) format.

package main

import (
	"github.com/icza/srtgears"
)

func main() {
	sp1, err := srtgears.ReadSrtFile("eng.srt")
	check(err) // Check / handle error
	sp2, err := srtgears.ReadSrtFile("hun.srt")
	check(err) // Check / handle error
	sp1.Merge(sp2)
	err = srtgears.WriteSsaFile("eng+hun.ssa", sp1)
	check(err) // Check / handle error
}

func check(err error) {}
Output:

Example (Misc)

This example shows how to change subtitle color to yellow, move to top, remove HI (hearing impaired lines), increase display duration by 10% and save result as a Sub Station Alpha (*.ssa) file.

package main

import (
	"github.com/icza/srtgears"
)

func main() {
	sp1, err := srtgears.ReadSrtFile("eng.srt")
	check(err) // Check / handle error
	sp1.SetColor("yellow")
	sp1.SetPos(srtgears.Top)
	sp1.RemoveHI()
	sp1.Lengthen(1.1)
	err = srtgears.WriteSsaFile("eng2.ssa", sp1)
	check(err) // Check / handle error
}

func check(err error) {}
Output:

Index

Examples

Constants

View Source
const (
	// HomePage is the Srtgears home page
	HomePage = "https://srt-gears.appspot.com/"
	// Author name
	Author = "Andras Belicza"
)

Variables

View Source
var (
	// Debug tells whether to print debug messages.
	Debug bool
)

Functions

func WriteSrtFile

func WriteSrtFile(name string, sp *SubsPack) (err error)

WriteSrtFile generates SubRip format (*.srt) and writes it to a file.

func WriteSrtTo

func WriteSrtTo(w io.Writer, sp *SubsPack) error

WriteSrtTo generates SubRip format (*.srt) and writes it to an io.Writer.

func WriteSsaFile

func WriteSsaFile(name string, sp *SubsPack) (err error)

WriteSsaFile generates Sub Station Alpha format (*.ssa) and writes it to a file.

func WriteSsaTo

func WriteSsaTo(w io.Writer, sp *SubsPack) (err error)

WriteSsaTo generates Sub Station Alpha format (*.ssa) and writes it to an io.Writer.

Types

type Pos

type Pos int

Pos is the subtitle position type.

const (
	PosNotSpecified Pos = iota
	BottomLeft
	Bottom
	BottomRight
	Left
	Center
	Right
	TopLeft
	Top
	TopRight
)

Possible position values of subtitles. Zero value of type Pos is PosNotSpecified.

type SortSubtitles

type SortSubtitles []*Subtitle

SortSubtitles is a type that implements sorting

func (SortSubtitles) Len

func (s SortSubtitles) Len() int

func (SortSubtitles) Less

func (s SortSubtitles) Less(i, j int) bool

func (SortSubtitles) Swap

func (s SortSubtitles) Swap(i, j int)

type SubsPack

type SubsPack struct {
	Subs []*Subtitle
}

SubsPack represents subtitles of a movie, a collection of Subtitles and other meta info.

func ReadSrtFile

func ReadSrtFile(name string) (sp *SubsPack, err error)

ReadSrtFile reads and parses a SubRip file (*.srt) and builds the model from it.

func ReadSrtFrom

func ReadSrtFrom(r io.Reader) (sp *SubsPack, err error)

ReadSrtFrom reads and parses a SubRip from an io.Reader (*.srt) and builds the model from it.

func (*SubsPack) Concatenate

func (sp *SubsPack) Concatenate(sp2 *SubsPack, secPartStart time.Duration)

Concatenate concatenates another SubsPack to this. Subtitles are not copied, only their addresses are appended to ours.

In order to get correct timing for the concatenated 2nd part, timestamps of the concatenated subtitles have to be shifted with the start time of the 2nd part of the movie (which is usually the length of the first part).

Useful if movie is present in 1 part but there are 2 subtitles for 2 parts.

func (*SubsPack) Lengthen

func (sp *SubsPack) Lengthen(factor float64)

Lengthen lengthens the display duration of all subtitles.

func (*SubsPack) Merge

func (sp *SubsPack) Merge(sp2 *SubsPack)

Merge merges another SubsPack into this to create a "dual subtitle". Subtitles are not copied, only their addresses are merged to ours.

Useful if 2 different subtitles are to be displayed at the same time, e.g. 2 different languages.

func (*SubsPack) RemoveControl

func (sp *SubsPack) RemoveControl()

RemoveControl removes controls such as {\anX} (or {\aY}), {\pos(x,y)} from all subtitles.

func (*SubsPack) RemoveHI

func (sp *SubsPack) RemoveHI()

RemoveHI removes hearing impaired lines from subtitles (such as "[PHONE RINGING]" or "(phone ringing)").

func (*SubsPack) RemoveHTML

func (sp *SubsPack) RemoveHTML()

RemoveHTML removes HTML formatting from all subtitles.

func (*SubsPack) Scale

func (sp *SubsPack) Scale(factor float64)

Scale scales the timestamps of the subtitles. The duration for which subtitles are visible is not changed.

func (*SubsPack) SetColor

func (sp *SubsPack) SetColor(color string)

SetColor sets the color of all subtitles.

func (*SubsPack) SetPos

func (sp *SubsPack) SetPos(pos Pos)

SetPos sets the position of all subtitles.

func (*SubsPack) Shift

func (sp *SubsPack) Shift(delta time.Duration)

Shift shifts all subtitles with the specified delta.

func (*SubsPack) Sort

func (sp *SubsPack) Sort()

Sort sorts the subtitles by appearance timestamp.

func (*SubsPack) Split

func (sp *SubsPack) Split(at time.Duration) (sp2 *SubsPack)

Split splits this SubsPack into 2 at the specified time. Subtitles before the split time will remain in this, subtitles after the split time will be added to a new SubsPack that is returned.

Useful to create 2 subtitles if movie is present in 2 parts but subtitle is for one.

func (*SubsPack) Stats

func (sp *SubsPack) Stats() *SubsStats

Stats analyzes the subtitle pack and returns various statistics. Subtitles will be modified so you should not attempt to save it after calling this.

type SubsStats

type SubsStats struct {
	Subs                      int           // Total number of subs.
	Lines                     int           // # of lines
	AvgLinesPerSub            float64       // Avg lines per sub
	Chars                     int           // # of characters (spaces included)
	CharsNoSpace              int           // # of characters (without spaces)
	AvgCharsPerLine           float64       // Avg chars (no space) per line
	Words                     int           // # of words
	AvgWordsPerLine           float64       // Avg words per line
	AvgCharsPerWord           float64       // Avg chars per word
	TotalDispDur              time.Duration // Total subtitle display time
	SubVisibRatio             float64       // Subtitle visible ratio (compared to total time)
	AvgDispDurPerNonSpaceChar time.Duration // Avg. display duration per non-space char
	HTMLs                     int           // # of subs having HTML formatting
	Controls                  int           // # of subs having controls
	HIs                       int           // # of subs having hearing impaired lines
}

SubsStats is statistics that can be gathered from a SubsPack.

type Subtitle

type Subtitle struct {
	TimeIn  time.Duration // Timestamp when subtitle appears
	TimeOut time.Duration // Timestamp when subtitle disappears
	Lines   []string      // Lines of text to be displayed
	Pos     Pos           // Position where to display it
	Color   string        // Color of the text, HTML RRGGBB format or a color name
}

Subtitle represents 1 subtitle, 1 displayable text (which may be broken into multiple lines).

func (*Subtitle) DisplayDuration

func (s *Subtitle) DisplayDuration() time.Duration

DisplayDuration returns the duration for which the subtitle is visible.

func (*Subtitle) Lengthen

func (s *Subtitle) Lengthen(factor float64)

Lengthen lengthens the display duration of the subtitle.

func (*Subtitle) RemoveControl

func (s *Subtitle) RemoveControl() (removed bool)

RemoveControl removes controls such as {\anX} (or {\aY}), {\pos(x,y)}. Returns true if controls were present.

func (*Subtitle) RemoveHI

func (s *Subtitle) RemoveHI() (remove bool)

RemoveHI removes hearing impaired lines (such as "[PHONE RINGING]" or "(phone ringing)"). Returns true if HI lines were present.

func (*Subtitle) RemoveHTML

func (s *Subtitle) RemoveHTML() (removed bool)

RemoveHTML removes HTML formatting. Returns true if HTML formatting was present.

func (*Subtitle) Scale

func (s *Subtitle) Scale(factor float64)

Scale scales the appearance timestamp of the subtitle. The duration for which subtitle is visible is not changed.

func (*Subtitle) Shift

func (s *Subtitle) Shift(delta time.Duration)

Shift shifts the subtitle with the specified delta.

Directories

Path Synopsis
cmd
packrelease
This is the main package of the packrelease tool which packs cross-compiled releases into the web/release folder, and also generates the HTML table in the format ready for the download.html.
This is the main package of the packrelease tool which packs cross-compiled releases into the web/release folder, and also generates the HTML table in the format ready for the download.html.
srtgears
This is the main package of the Srtgears command line tool.
This is the main package of the Srtgears command line tool.
Package exec is a parameter driven executor which parses the parameters and performs subtitle transformations.
Package exec is a parameter driven executor which parses the parameters and performs subtitle transformations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL