Documentation
¶
Overview ¶
Package ffmpeg provides low-level ffmpeg/ffprobe subprocess wrappers for stream probing, subtitle extraction, and PCM audio extraction. This package is pure I/O — it does not depend on subsync's Cue type.
Index ¶
- Constants
- func Available() bool
- func ExtractPCMWithFilter(ctx context.Context, path string, startMs, durationMs int64, af string) ([]int16, error)
- func ExtractRawSubtitle(ctx context.Context, videoPath string, streamIndex int) ([]byte, error)
- func ExtractSegmentPCM(ctx context.Context, path string, startMs, durationMs int64) ([]int16, error)
- func IsTextSubtitleCodec(codec string) bool
- func NormalizeFFprobeLang(lang string, mapper LangMapper) string
- func ProbeAvailable() bool
- func ProbeDuration(ctx context.Context, path string) (int64, error)
- func ProbeVideoFPS(ctx context.Context, path string) float64
- type CommandRunner
- type LangMapper
- type Track
Constants ¶
const ( CodecASS = "ass" CodecSSA = "ssa" CodecSubrip = "subrip" CodecSRT = "srt" )
Codec name constants for the most commonly referenced subtitle formats.
const MaxExtractBytes = 50 * 1024 * 1024
MaxExtractBytes is the maximum size for ffmpeg stdout reads (subtitle extraction, ASS conversion). Prevents OOM from pathological streams.
const PCMSampleRate = 8000
PCMSampleRate is the sample rate used for PCM audio extraction.
Variables ¶
This section is empty.
Functions ¶
func ExtractPCMWithFilter ¶
func ExtractPCMWithFilter(ctx context.Context, path string, startMs, durationMs int64, af string) ([]int16, error)
ExtractPCMWithFilter extracts PCM audio with a custom ffmpeg audio filter.
func ExtractRawSubtitle ¶
ExtractRawSubtitle runs ffmpeg to extract a subtitle stream by index, converting to SRT format. Returns raw bytes (no parsing).
func ExtractSegmentPCM ¶
func ExtractSegmentPCM(ctx context.Context, path string, startMs, durationMs int64) ([]int16, error)
ExtractSegmentPCM extracts raw PCM audio from a time range. Returns int16 samples at PCMSampleRate. durationMs=0 means no limit.
func IsTextSubtitleCodec ¶
IsTextSubtitleCodec reports whether the ffprobe codec name is a text-based subtitle format that can be converted to SRT.
func NormalizeFFprobeLang ¶
func NormalizeFFprobeLang(lang string, mapper LangMapper) string
NormalizeFFprobeLang normalizes ffprobe language tags to ISO 639-1. Handles ISO 639-2/3 codes, BCP 47 tags, and "und"/"undetermined".
func ProbeDuration ¶
ProbeDuration returns the duration of a media file in milliseconds.
Types ¶
type CommandRunner ¶
type CommandRunner interface {
// Run executes a command and returns its combined stdout output.
Run(ctx context.Context, name string, args ...string) ([]byte, error)
}
CommandRunner abstracts subprocess execution for testability. The default implementation uses exec.CommandContext. Test code can inject a mock runner to exercise parsing/selection logic without real ffmpeg binaries.
var DefaultRunner CommandRunner = execRunner{}
DefaultRunner is the package-level CommandRunner used by exported functions. Override in tests to inject a mock.
type LangMapper ¶
LangMapper maps ISO 639-3 language codes to ISO 639-1.
type Track ¶
type Track struct {
CodecName string
CodecType string
Language string // extracted from tags
Title string // extracted from tags
RFrameRate string // raw r_frame_rate from ffprobe (e.g. "24000/1001")
Index int
Forced bool // from disposition
HearingImpaired bool // from disposition
}
Track holds metadata for a single stream from ffprobe output.
func ParseProbeOutput ¶
ParseProbeOutput parses raw ffprobe JSON output into Track structs.
func ProbeStreams ¶
ProbeStreams runs ffprobe and returns parsed stream metadata. Filters by codec_type if filterType is non-empty (e.g. "subtitle", "audio").
func SelectBestSubTrack ¶
func SelectBestSubTrack(tracks []Track, lang, excludeLang string, mapper LangMapper) *Track
SelectBestSubTrack picks the best text subtitle track from ffprobe output.