ffmpeg

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: AGPL-3.0 Imports: 8 Imported by: ? 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 ffmpeg provides a builder-pattern API for constructing and running FFmpeg transcoding jobs in-process via go-astiav (libavcodec/libavformat CGo bindings). Hardware encoder detection and automatic fallback are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec = astiav.CodecID

Codec is an alias for astiav.CodecID, identifying a video or audio codec. Using a type alias keeps Codec values directly usable in libavcodec calls without explicit conversions.

const (
	// CodecH264 encodes video as H.264 (libx264 for software, or hardware variant).
	CodecH264 Codec = astiav.CodecIDH264
	// CodecH265 encodes video as H.265/HEVC (libx265 for software, or hardware variant).
	CodecH265 Codec = astiav.CodecIDH265
	// CodecAC3 encodes audio as Dolby AC-3.
	CodecAC3 Codec = astiav.CodecIDAc3
	// CodecCopy copies the stream without re-encoding.
	CodecCopy Codec = astiav.CodecIDNone
)

type Container

type Container string

Container identifies an output container format.

const (
	// ContainerMKV produces a Matroska (.mkv) container.
	ContainerMKV Container = "matroska"
	// ContainerMP4 produces an MP4 container.
	ContainerMP4 Container = "mp4"
)

type CropParams

type CropParams struct {
	W, H int
	X, Y int
}

CropParams describes the non-black region detected by the cropdetect filter. All values are in pixels.

func DetectCrop

func DetectCrop(ctx context.Context, inputPath string) (CropParams, error)

DetectCrop runs a cropdetect filter pass over the video at inputPath and returns the detected crop region. The returned CropParams reflect the most conservative (widest) non-black region across all decoded frames.

A cancelled context causes DetectCrop to return promptly with ctx.Err().

type HWAccel

type HWAccel int

HWAccel selects hardware acceleration for encoding.

const (
	// HWAccelNone uses software encoding only.
	HWAccelNone HWAccel = iota
	// HWAccelNVENC uses NVIDIA NVENC via CUDA.
	HWAccelNVENC
	// HWAccelVAAPI uses Intel/AMD VAAPI.
	HWAccelVAAPI
	// HWAccelQSV uses Intel Quick Sync Video.
	HWAccelQSV
	// HWAccelAuto detects available hardware at runtime and falls back to software.
	HWAccelAuto
)

func DetectHardwareDecoders

func DetectHardwareDecoders(codec Codec) []HWAccel

DetectHardwareDecoders returns all HWAccel values for which a hardware decoder is registered in libavcodec for the given codec, in priority order (QSV > NVENC > VAAPI). It probes libavcodec directly — no device is opened, only codec registration is checked.

Hardware encode and decode capabilities are independent. Use DetectHardwareEncoders to check encode-side availability.

func DetectHardwareEncoders

func DetectHardwareEncoders(codec Codec) []HWAccel

DetectHardwareEncoders returns all HWAccel values for which a hardware encoder is registered in libavcodec for the given output codec, in priority order (QSV > NVENC > VAAPI). It probes libavcodec directly — no device is opened, only codec registration is checked.

Hardware encode and decode capabilities are independent. Use DetectHardwareDecoders to check decode-side availability.

func GetHardwareDecoder

func GetHardwareDecoder(codec Codec, preferred HWAccel) HWAccel

GetHardwareDecoder returns preferred if it is available as a hardware decoder for the given codec. If preferred is HWAccelAuto or unavailable, the first available hardware decoder (in priority order) is returned. Returns HWAccelNone if no hardware decoder is available.

func GetHardwareEncoder

func GetHardwareEncoder(codec Codec, preferred HWAccel) HWAccel

GetHardwareEncoder returns preferred if it is available as a hardware encoder for the given codec. If preferred is HWAccelAuto or unavailable, the first available hardware encoder (in priority order) is returned. Returns HWAccelNone if no hardware encoder is available.

type Progress

type Progress struct {
	// FramesProcessed is the number of encoded frames written so far.
	FramesProcessed int64
	// PercentComplete is the estimated completion percentage (0–100).
	PercentComplete float64
}

Progress is a periodic update emitted during transcoding.

type TranscodeBuilder

type TranscodeBuilder struct {
	// contains filtered or unexported fields
}

TranscodeBuilder constructs a Transcoder using a fluent API.

func NewTranscode

func NewTranscode(inputPath, outputPath string) *TranscodeBuilder

NewTranscode returns a builder for a transcode job from inputPath to outputPath. Default codecs are CodecCopy for both video and audio. When no container is set, the output format is inferred from the output file extension.

func (*TranscodeBuilder) Build

func (b *TranscodeBuilder) Build() *Transcoder

Build returns a runnable Transcoder.

func (*TranscodeBuilder) ExcludeStreams

func (b *TranscodeBuilder) ExcludeStreams(indices ...int) *TranscodeBuilder

ExcludeStreams marks the given input stream indices to be dropped from the output. Packets from excluded streams are silently discarded during muxing.

func (*TranscodeBuilder) HardwareAccel

func (b *TranscodeBuilder) HardwareAccel(h HWAccel) *TranscodeBuilder

HardwareAccel sets the hardware acceleration mode.

func (*TranscodeBuilder) ToAudioCodec

func (b *TranscodeBuilder) ToAudioCodec(c Codec) *TranscodeBuilder

ToAudioCodec sets the output audio codec.

func (*TranscodeBuilder) ToContainer

func (b *TranscodeBuilder) ToContainer(c Container) *TranscodeBuilder

ToContainer sets the output container format. If not called, the container is inferred from the output file extension.

func (*TranscodeBuilder) ToVideoCodec

func (b *TranscodeBuilder) ToVideoCodec(c Codec) *TranscodeBuilder

ToVideoCodec sets the output video codec.

func (*TranscodeBuilder) WithAudioStreamTitles

func (b *TranscodeBuilder) WithAudioStreamTitles(titles map[int]string) *TranscodeBuilder

WithAudioStreamTitles sets per-stream title overrides for regular (non-downmix) audio output streams. The map is keyed by input stream index; only streams whose index appears in the map have their title metadata set. A nil argument is a no-op. Titles are written as stream metadata via outStream.SetMetadata.

func (*TranscodeBuilder) WithAutoDownmixTitle

func (b *TranscodeBuilder) WithAutoDownmixTitle() *TranscodeBuilder

WithAutoDownmixTitle enables automatic derivation of the downmix stream title from the actual encoder channel layout resolved after encoder setup. The title is set to "X.Y" where X is the non-LFE channel count and Y is 1 if an LFE channel is present, 0 otherwise (e.g. "2.1" or "2.0"). Has no effect when no downmix stream is configured.

func (*TranscodeBuilder) WithCoverArt

func (b *TranscodeBuilder) WithCoverArt(imageBytes []byte, mimeType string) *TranscodeBuilder

WithCoverArt embeds imageBytes as a cover art attachment stream in the MKV output. This method is a no-op when the output container is not ContainerMKV. mimeType must be "image/jpeg" or "image/png"; any other value is treated as a no-op. When called with a valid MIME type, any existing attachment streams present in the source file are excluded from the output so that only the supplied artwork is embedded. A nil or empty imageBytes slice is a no-op.

func (*TranscodeBuilder) WithCrop

func (b *TranscodeBuilder) WithCrop(params *CropParams) *TranscodeBuilder

WithCrop applies a crop filter to the video stream during encoding. The crop region is specified by params: W and H are the output dimensions in pixels, and X and Y are the offsets from the top-left corner of the input frame. WithCrop is a no-op when params is nil. Crop is silently skipped at build time when videoCodec is CodecCopy, since copying a stream precludes any filter. Storing cropParams unconditionally avoids a call-order dependency: WithCrop and ToVideoCodec may be called in any order on the builder.

func (*TranscodeBuilder) WithDefaultAudioStream

func (b *TranscodeBuilder) WithDefaultAudioStream(idx *int) *TranscodeBuilder

WithDefaultAudioStream marks the audio stream at the given input stream index as the default audio track in the output. All other audio stream dispositions have their default flag cleared. A nil argument is a no-op: audio stream dispositions are copied from the input unchanged.

func (*TranscodeBuilder) WithDefaultSubtitleStream

func (b *TranscodeBuilder) WithDefaultSubtitleStream(idx *int) *TranscodeBuilder

WithDefaultSubtitleStream marks the subtitle stream at the given input stream index as the default subtitle track in the output. All other subtitle stream dispositions have their default flag cleared. A nil argument is a no-op: subtitle stream dispositions are copied from the input unchanged.

func (*TranscodeBuilder) WithDownmix

func (b *TranscodeBuilder) WithDownmix(idx *int) *TranscodeBuilder

WithDownmix synthesizes an additional downmixed audio stream from the input stream at the given index. The downmix targets a 2.1 channel layout with AC-3 encoding; if the encoder does not support 2.1, stereo is used instead. The synthesized stream is appended after all other output streams. A nil argument is a no-op: no downmix stream is synthesized.

func (*TranscodeBuilder) WithDownmixTitle

func (b *TranscodeBuilder) WithDownmixTitle(title string) *TranscodeBuilder

WithDownmixTitle sets a title prefix to prepend to the downmix stream's channel layout label when WithAutoDownmixTitle is also set. An empty string is a no-op. For example, passing "English" produces "English 2.0" instead of "2.0".

func (*TranscodeBuilder) WithH265CRF

func (b *TranscodeBuilder) WithH265CRF(crf int) *TranscodeBuilder

WithH265CRF sets the constant-quality value for H.265 video encoders. A value of 0 (the default) leaves the encoder's built-in default in effect. For libx265 this sets the CRF; for hevc_nvenc it sets the CQ value; for hevc_qsv and hevc_vaapi it sets the global_quality (ICQ) value. Valid explicit values are 1 through 51; values outside that range are treated as a no-op. Typical values range from 18 (high quality) to 28 (lower quality). Has no effect when the video codec is CodecCopy or when the encoder is not an H.265 variant.

func (*TranscodeBuilder) WithHardwareDevice

func (b *TranscodeBuilder) WithHardwareDevice(path string) *TranscodeBuilder

WithHardwareDevice sets the device path passed to CreateHardwareDeviceContext for both the decoder and encoder hardware device contexts. Typical values are "/dev/dri/renderD128" for VAAPI/QSV or "0"/"1" for CUDA device indices. An empty string leaves the path unset (libav auto-selects the hardware device).

func (*TranscodeBuilder) WithProgressChan

func (b *TranscodeBuilder) WithProgressChan(ch chan<- Progress) *TranscodeBuilder

WithProgressChan sets a channel to receive periodic progress updates. Updates are sent non-blocking; a full channel silently drops updates.

func (*TranscodeBuilder) WithStartHook

func (b *TranscodeBuilder) WithStartHook(fn func()) *TranscodeBuilder

WithStartHook sets a function that is called once the transcoder has finished all setup and is about to enter the main packet read loop. Intended for testing (e.g. triggering context cancellation at a deterministic point) and light instrumentation.

func (*TranscodeBuilder) WithSubtitleStreamTitles

func (b *TranscodeBuilder) WithSubtitleStreamTitles(titles map[int]string) *TranscodeBuilder

WithSubtitleStreamTitles sets per-stream title overrides for subtitle output streams. The map is keyed by input stream index; only streams whose index appears in the map have their title metadata set. A nil argument is a no-op. Titles are written as stream metadata via outStream.SetMetadata.

type Transcoder

type Transcoder struct {
	TranscodeBuilder
	// contains filtered or unexported fields
}

Transcoder is a ready-to-run transcode job produced by TranscodeBuilder.Build. It embeds TranscodeBuilder so all configuration is accessible in one place.

func (*Transcoder) HardwareAccelerated

func (t *Transcoder) HardwareAccelerated() bool

HardwareAccelerated reports whether any video stream used a hardware encoder during the most recent Run call. Always false before Run is called or when Run returns an error before encoder setup completes.

func (*Transcoder) Run

func (t *Transcoder) Run(ctx context.Context) error

Run executes the transcode job. It blocks until the job completes, the context is cancelled, or an error occurs. A cancelled context causes Run to return promptly with ctx.Err().

Jump to

Keyboard shortcuts

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