subtitles

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

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 21 Imported by: 5

README

About

GoDoc

This is a go library and command-line tools for handling .srt, .vtt and .ssa subtitles

Installation

Windows and macOS binaries are available under Releases

Or install them directly from git:

go install github.com/martinlindhe/subtitles/cmd/subber@latest

Sub-projects

  • subber command line tool for subtitles

Library example - convert srt to vtt

import "github.com/martinlindhe/subtitles"

in := "1\n" +
    "00:00:04,630 --> 00:00:06,018\n" +
    "Go ninja!\n" +
    "\n" +
    "1\n" +
    "00:01:09,630 --> 00:01:11,005\n" +
    "No ninja!\n"
res, _ := subtitles.NewFromSRT(in)

// Output: WEBVTT
//
// 00:04.630 --> 00:06.018
// Go ninja!
//
// 01:09.630 --> 01:11.005
// No ninja!
fmt.Println(res.AsVTT())

Library example - download subtitles

f, _ := os.Open(fileName)
defer f.Close()

finder := subtitles.NewSubFinder(f, fileName, "en")
text, err := finder.TheSubDb()

License

Under MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToUTF8

func ConvertToUTF8(b []byte) string

ConvertToUTF8 returns a utf8 string

func LooksLikeTextSubtitle

func LooksLikeTextSubtitle(filename string) bool

LooksLikeTextSubtitle returns true i byte stream seems to be of a recognized format

func SubDbHashFromFile

func SubDbHashFromFile(f *os.File) (string, error)

SubDbHashFromFile returns a checksum in hex-string representation conforming to http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes

func TimeSRT

func TimeSRT(t time.Time) string

TimeSRT renders a timestamp for use in .srt

func TimeVTT

func TimeVTT(t time.Time) string

TimeVTT renders a timestamp for use in WebVTT

Types

type Caption

type Caption struct {
	Seq   int
	Start time.Time
	End   time.Time
	Text  []string
}

Caption represents one subtitle block

func (Caption) AsSRT

func (cap Caption) AsSRT() string

AsSRT renders the caption as srt

func (Caption) AsVTT

func (cap Caption) AsVTT() string

AsVTT renders the caption as WebVTT

type SubFinder

type SubFinder struct {
	FileName  string
	Language  string
	VideoFile *os.File
	Quiet     bool
}

SubFinder represents a video being queried for subtitles

func NewSubFinder

func NewSubFinder(video *os.File, fileName string, language string) *SubFinder

NewSubFinder returns a new SubFinder

func (SubFinder) TheSubDb

func (f SubFinder) TheSubDb(args ...string) ([]byte, error)

TheSubDb downloads a subtitle from thesubdb.com

type Subtitle

type Subtitle struct {
	Captions []Caption
}

Subtitle holds a parsed subtitle

func NewFromCCDBCapture

func NewFromCCDBCapture(s string) (res Subtitle, err error)

NewFromCCDBCapture parses a ccdb capture text into []Caption, assumes s is a clean utf8 string

func NewFromDCSub

func NewFromDCSub(s string) (res Subtitle, err error)

NewFromDCSub parses a dcsubtitle xml sub, assumes s is a clean utf8 string

func NewFromSRT

func NewFromSRT(s string) (res Subtitle, err error)

NewFromSRT parses a .srt text into Subtitle, assumes s is a clean utf8 string

Example
in := "1\n" +
	"00:00:04,630 --> 00:00:06,018\n" +
	"Go ninja!\n" +
	"\n" +
	"1\n" +
	"00:01:09,630 --> 00:01:11,005\n" +
	"No ninja!\n"

res, _ := NewFromSRT(in)
Output:

WEBVTT

00:04.630 --> 00:06.018
Go ninja!

01:09.630 --> 01:11.005
No ninja!

func NewFromSSA

func NewFromSSA(s string) (res Subtitle, err error)

NewFromSSA parses a .ssa text into []Caption, assumes s is a clean utf8 string

func NewFromVTT

func NewFromVTT(s string) (res Subtitle, err error)

NewFromVTT parses a .vtt text into Subtitle, assumes s is a clean utf8 string

func Parse

func Parse(b []byte) (Subtitle, error)

Parse tries to parse a subtitle

func (*Subtitle) AsSRT

func (subtitle *Subtitle) AsSRT() (res string)

AsSRT renders the sub in .srt format

func (*Subtitle) AsVTT

func (subtitle *Subtitle) AsVTT() (res string)

AsVTT renders the sub in WebVTT format https://en.wikipedia.org/wiki/WebVTT

func (*Subtitle) FilterCaptions

func (subtitle *Subtitle) FilterCaptions(filter string)

FilterCaptions pass the captions through a filter function

func (*Subtitle) RemoveAds

func (subtitle *Subtitle) RemoveAds(outputPrefix string, verbose bool) *Subtitle

RemoveAds removes advertisement from the subtitles

func (*Subtitle) ResyncSubs

func (subtitle *Subtitle) ResyncSubs(sync int)

ResyncSubs adjust text timing by `sync` milliseconds

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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