subtitle

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package subtitle finds a video's subtitle tracks, embedded or in files next to it, and serves the text ones as WebVTT for the web player.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadKey means OpenSubtitles turned the API key down.
	ErrBadKey = errors.New("OpenSubtitles didn't accept the API key")
	// ErrBadLogin means OpenSubtitles turned the username or password down.
	ErrBadLogin = errors.New("OpenSubtitles didn't accept the username or password")
	// ErrQuota means today's downloads are used up.
	ErrQuota = errors.New("today's OpenSubtitles downloads are used up")
)
View Source
var OpenSubtitlesBase = "https://api.opensubtitles.com/api/v1"

OpenSubtitlesBase is the API. The docs' screenshot server points it at a stand-in at build time.

Functions

func APILanguage added in v0.1.10

func APILanguage(code string) string

APILanguage turns a language code into the API's own: pt-BR is pt-br, Simplified Chinese is zh-cn, Norwegian Bokmål is no. Unknown ones give "".

func APILanguageInfo added in v0.1.10

func APILanguageInfo(code string) (iso, variant, name string)

APILanguageInfo is fromAPILanguage for a code from a client, which may be the API's own (pt-br) or ISO 639-1 (pt).

func FileHash added in v0.1.10

func FileHash(path string) (string, error)

FileHash is a video's OpenSubtitles hash: its size plus the sum of its first and last 64 KB as 64-bit numbers, as 16 hex digits. It reads only those two pieces, so it's quick even on network storage.

func FoundLabel added in v0.1.10

func FoundLabel(name string, forced, sdh bool) (string, string)

FoundLabel is a found subtitle's label and note, from its language's name: "Portuguese (Brazil, SDH)".

func LanguageCode added in v0.2.1

func LanguageCode(s string) string

LanguageCode turns a language tag ("eng", "en", "pt-BR") into the short code tracks use, or "" when unknown.

Types

type Converter

type Converter struct {
	FFmpeg string
	// FFprobe lists a video's attached fonts (see Fonts).
	FFprobe string
	Dir     string
	// contains filtered or unexported fields
}

Converter turns subtitle tracks into WebVTT and caches the results, so each track is converted once per version of its file.

func NewConverter

func NewConverter(ffmpeg, dir string) *Converter

func (*Converter) Fonts added in v0.2.1

func (c *Converter) Fonts(ctx context.Context, videoPath string) ([]Font, error)

Fonts lists the fonts attached to a video, in file order.

func (*Converter) HasVTT added in v0.2.1

func (c *Converter) HasVTT(videoPath string, t Track) bool

HasVTT says whether track t is already converted, so reading it costs nothing (an embedded track otherwise takes a read of the whole video).

func (*Converter) Serve

func (c *Converter) Serve(w http.ResponseWriter, r *http.Request, videoPath string, t Track, all []Track)

Serve writes track t of the video at videoPath as WebVTT. all lists the video's tracks: the first request for an embedded track extracts every embedded text track in one pass, since that costs one read of the file.

func (*Converter) ServeASS added in v0.2.1

func (c *Converter) ServeASS(w http.ResponseWriter, r *http.Request, videoPath string, t Track)

ServeASS writes a styled track as it is, in UTF-8.

func (*Converter) ServeFont added in v0.2.1

func (c *Converter) ServeFont(w http.ResponseWriter, r *http.Request, videoPath string, f Font)

ServeFont writes font f of the video, extracting it once.

func (*Converter) StyledSource added in v0.2.1

func (c *Converter) StyledSource(videoPath string, t Track) (string, int, error)

StyledSource is where a transcode reads a styled track to draw it in: its file (the video for an embedded track) and its subtitle stream there, or, for a track whose times are moved, a moved copy of it (stream -1).

func (*Converter) VTT added in v0.2.1

func (c *Converter) VTT(videoPath string, t Track, all []Track) ([]byte, error)

VTT returns track t as WebVTT, as it is in its file (unshifted).

func (*Converter) WithFFprobe added in v0.2.1

func (c *Converter) WithFFprobe(path string) *Converter

WithFFprobe sets the ffprobe that lists a video's fonts.

type Font added in v0.2.1

type Font struct {
	// Index is the font's place among the file's fonts, for FontFile.
	Index int `json:"index"`
	// Name is its file name in the video, such as "Gandhi Sans.ttf".
	Name     string `json:"name"`
	MimeType string `json:"mimeType,omitempty"`
	// contains filtered or unexported fields
}

Font is a font attached to a video file, as Matroska files carry the fonts their ASS subtitles use.

type Found added in v0.1.10

type Found struct {
	// ID is the API's file id, passed back to add it.
	ID       int64  `json:"id"`
	Language string `json:"language,omitempty"` // ISO 639-1, such as en
	// Variant is the API's own language code where it says more, such as
	// pt-br or zh-tw.
	Variant string `json:"variant,omitempty"`
	Label   string `json:"label"` // in English, such as "Portuguese (Brazil)"
	Release string `json:"release,omitempty"`
	SDH     bool   `json:"sdh,omitempty"`
	Forced  bool   `json:"forced,omitempty"`
	// Machine is true for files a machine translated.
	Machine   bool `json:"machine,omitempty"`
	Downloads int  `json:"downloads"`
	// MatchesFile is true when the file was made for this very video file,
	// so its timing fits.
	MatchesFile bool `json:"matchesFile,omitempty"`
	// Trusted is true when a trusted member uploaded it.
	Trusted bool `json:"trusted,omitempty"`
}

Found is a subtitle file the search turned up.

type OpenSubtitles added in v0.1.10

type OpenSubtitles struct {
	Key       OpenSubtitlesKey
	Base      string // OpenSubtitlesBase, or a test server
	UserAgent string // "Matinee v1.2.3": the API asks every app to name itself
	HTTP      *http.Client
	// contains filtered or unexported fields
}

OpenSubtitles searches opensubtitles.com and fetches its files.

func (*OpenSubtitles) Check added in v0.1.10

func (o *OpenSubtitles) Check(ctx context.Context) error

Check makes sure the API key works, and the username and password when they're given.

func (*OpenSubtitles) Download added in v0.1.10

func (o *OpenSubtitles) Download(ctx context.Context, id int64) (data []byte, remaining int, err error)

Download fetches a file the search found, as SubRip. Remaining is how many more files the account may fetch today, where the API says.

func (*OpenSubtitles) Search added in v0.1.10

func (o *OpenSubtitles) Search(ctx context.Context, q Query) ([]Found, error)

Search finds subtitles for a video: those made for the file itself first, then the most downloaded.

type OpenSubtitlesKey added in v0.1.10

type OpenSubtitlesKey struct {
	APIKey   string `json:"apiKey"`
	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"`
}

OpenSubtitlesKey is what an admin entered. It stays on the server.

type Query added in v0.1.10

type Query struct {
	// TMDBID is a movie's TMDB id; ShowTMDBID with Season and Episode
	// find an episode.
	TMDBID     int64
	ShowTMDBID int64
	Season     int
	Episode    int
	// Title and Year find a video with no TMDB id: an episode's show title.
	Title string
	Year  int
	// Hash is the file's OpenSubtitles hash (see FileHash): subtitles made
	// for this very file are marked.
	Hash string
	// Languages are ISO 639-1 codes, or the API's own, such as pt-br.
	Languages []string
}

Query is what's known about a video.

type Sync added in v0.2.1

type Sync struct {
	// State is syncing (under way), synced (moved), inSync (already
	// right), unsure (no fit stood out, left as it was), failed (couldn't
	// be read or listened to) or off (someone undid it).
	State string `json:"state"`
	// OffsetMs and Rate are how a synced track was moved: each time t is
	// shown at t×Rate + OffsetMs. Rate is absent when it's 1.
	OffsetMs int64   `json:"offsetMs,omitempty"`
	Rate     float64 `json:"rate,omitempty"`
	// Auto says Matinee did it on its own, without anyone asking.
	Auto bool `json:"auto,omitempty"`
	// Reference is what it was lined up with: audio, or subtitles (another
	// track inside the video, whose times are right).
	Reference string `json:"reference,omitempty"`
}

Sync is what lining a track up with the audio did.

type Track

type Track struct {
	ID    string `json:"id"`    // "e<N>" for embedded stream N, "x<hash>" for a sidecar file
	Label string `json:"label"` // in English, such as "English (SDH)"
	// Note is the track's own title, where it says more than its language
	// and flags, such as "Commentary", so apps can build a label in their
	// own language.
	Note     string `json:"note,omitempty"`
	Language string `json:"language,omitempty"` // ISO 639-1 where known, e.g. "en"
	Codec    string `json:"codec"`
	Forced   bool   `json:"forced,omitempty"`
	Default  bool   `json:"default,omitempty"`
	SDH      bool   `json:"sdh,omitempty"`
	External bool   `json:"external,omitempty"`
	// BurnIn marks picture-based subtitles (PGS, VobSub, DVB), which browsers
	// can't render: the server draws them into the video during a transcode.
	BurnIn bool `json:"burnIn,omitempty"`
	// Styled marks ASS/SSA tracks, whose positions, fonts and effects a
	// player can draw from the original (GET …/subtitles/{id}.ass, with the
	// file's fonts) or the server can draw into a transcode.
	Styled bool `json:"styled,omitempty"`
	// Found marks subtitles someone found online for the video (see
	// OpenSubtitles); Removable says the person asking may remove them.
	Found     bool `json:"found,omitempty"`
	Removable bool `json:"removable,omitempty"`
	// Sync says whether the track's times were lined up with the audio,
	// and how (see subsync). Absent for tracks nobody synced.
	Sync *Sync `json:"sync,omitempty"`
	// Shift is how the track's times are moved when it's served.
	Shift subsync.Shift `json:"-"`

	Stream int    `json:"-"` // embedded: index among the file's subtitle streams
	Path   string `json:"-"` // external: the sidecar file
}

Track is one subtitle track a player can offer.

func Find

func Find(tracks []Track, id string) (Track, bool)

Find returns the track with the given id.

func List

func List(videoPath string, embedded []probe.Subtitle) []Track

List returns a video's tracks: embedded streams in file order, then sidecar files. Codecs Matinee can't show at all are left out.

func Sidecars

func Sidecars(videoPath string) []Track

Sidecars finds subtitle files named after a video, like "Movie.srt", "Movie.en.srt", "Movie.en.forced.srt" or "Movie.English.SDH.srt", and in a Subs or Subtitles folder beside it: "Subs/Movie.en.srt", "Subs/<video name>/2_English.srt" (how many TV releases come), or "Subs/2_English.srt" when the folder holds just the one video. Picture subtitles come as Blu-ray .sup files and VobSub .idx/.sub pairs; they're drawn in.

Jump to

Keyboard shortcuts

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