ffmpeg

package
v0.1.147 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: GPL-2.0, GPL-3.0 Imports: 14 Imported by: 0

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

View Source
const (
	CodecASS    = "ass"
	CodecSSA    = "ssa"
	CodecSubrip = "subrip"
	CodecSRT    = "srt"
)

Codec name constants for the most commonly referenced subtitle formats.

View Source
const MaxExtractBytes = 50 * 1024 * 1024

MaxExtractBytes is the maximum size for ffmpeg stdout reads (subtitle extraction, ASS conversion). Prevents OOM from pathological streams.

View Source
const PCMSampleRate = 8000

PCMSampleRate is the sample rate used for PCM audio extraction.

Variables

This section is empty.

Functions

func Available

func Available() bool

Available checks if ffmpeg is on PATH.

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

func ExtractRawSubtitle(ctx context.Context, videoPath string, streamIndex int) ([]byte, error)

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

func IsTextSubtitleCodec(codec string) bool

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 ProbeAvailable

func ProbeAvailable() bool

ProbeAvailable checks if ffprobe is on PATH.

func ProbeDuration

func ProbeDuration(ctx context.Context, path string) (int64, error)

ProbeDuration returns the duration of a media file in milliseconds.

func ProbeVideoFPS

func ProbeVideoFPS(ctx context.Context, path string) float64

ProbeVideoFPS returns the video stream's frame rate, or 0 if unavailable.

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

type LangMapper func(string) string

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

func ParseProbeOutput(data []byte) ([]Track, error)

ParseProbeOutput parses raw ffprobe JSON output into Track structs.

func ProbeStreams

func ProbeStreams(ctx context.Context, path, filterType string) ([]Track, error)

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.

Jump to

Keyboard shortcuts

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