Documentation
¶
Overview ¶
Package subsync lines a subtitle file up with its video. Subtitles found online or ripped for another release often show too early or too late, or drift further off as the film goes on because they were timed for a different frame rate. subsync listens to the video for where people are talking, reads where the subtitles say lines are spoken, and finds the shift (and, when it fits better, the change of speed) that lines the two up best.
Index ¶
Constants ¶
const FrameMs = 20
FrameMs is how long each step of a speech map is.
const MaxOffsetMs = 15 * 60 * 1000
MaxOffsetMs is the furthest Align looks: subtitles for a release with a longer studio logo or a recap are a few seconds to minutes off.
const MinScore = 9
MinScore is the Score a fit needs to be trusted. Fits for the wrong video or a badly broken file stay well below it.
const Unheard = 255
Unheard marks the parts of a speech map that weren't listened to.
Variables ¶
var ErrNoCues = errors.New("no spoken lines to line up")
var Rates = []float64{1, 23.976 / 24, 24 / 23.976, 23.976 / 25, 25 / 23.976, 24 / 25, 25 / 24}
Rates are the changes of speed worth trying: subtitles timed for a film at 24 or 23.976 frames a second drift when the video runs at another of 23.976, 24 or 25 (European TV and discs speed films up to 25).
Functions ¶
Types ¶
type Audio ¶
type Audio struct {
Stream int // among the file's audio streams, as in FFmpeg's 0:a:N
// Center listens to the front center channel only, where films and
// series put the dialogue, when the stream has one (5.1 and up).
Center bool
}
Audio says which of a video's audio streams to listen to.
type Cue ¶
type Cue struct {
StartMs, EndMs int64
// Sound marks lines that describe sounds or music, such as "[door
// slams]" or "♪ ♪", which nobody says.
Sound bool
}
Cue is one subtitle line's time on screen, in milliseconds.
type Reference ¶
type Reference []float64
Reference is what the subtitles are lined up with, one value per FrameMs step: a speech map, or another subtitle track's lines (see CueTrack).
func CueReference ¶
CueReference is a reference from a track whose times are right, such as subtitles inside the video.
func SpeechReference ¶
SpeechReference is a reference from a speech map. What wasn't listened to is NaN, which counts for nothing either way.
type Result ¶
type Result struct {
Shift
// Score is how clearly the fit stands out from all the others it
// tried: how many spreads above the typical one it is.
Score float64 `json:"score"`
// Sure says the fit stands out enough to trust.
Sure bool `json:"sure"`
}
Result is the best fit Align found.
type Shift ¶
Shift moves subtitle times: each time t becomes t×Rate + OffsetMs. A Rate of 0 means 1.
type Speech ¶
type Speech []byte
Speech is a video's speech map: for each FrameMs step, how likely it is that someone is talking, from 0 to 254, or Unheard.
func Detect ¶
Detect turns frame loudness (in dB) into a speech map. Speech is louder than the quiet moments around it, and it rises and falls several times a second with each syllable, where music and background noise mostly hold steady. A frame scores high when it has both.
func Listen ¶
func Listen(ctx context.Context, ffmpeg, file string, a Audio, windows []Window, lowPriority func(pid int)) (Speech, error)
Listen decodes one audio stream of a video and maps where people talk. lowPriority, when set, is given FFmpeg's process id once it starts, so listening can make way for everything else on the server. With windows, only those parts are listened to, and the rest of the map is Unheard.
type Window ¶
type Window struct{ StartMs, LenMs int64 }
Window is a part of a video to listen to.