subtitle

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: AGPL-3.0 Imports: 22 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)".

Types

type Converter

type Converter struct {
	FFmpeg 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) 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.

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 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"`
	// 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"`

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

Jump to

Keyboard shortcuts

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