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 ¶
- type Codec
- type Container
- type CropParams
- type HWAccel
- type Progress
- type TranscodeBuilder
- func (b *TranscodeBuilder) Build() *Transcoder
- func (b *TranscodeBuilder) ExcludeStreams(indices ...int) *TranscodeBuilder
- func (b *TranscodeBuilder) HardwareAccel(h HWAccel) *TranscodeBuilder
- func (b *TranscodeBuilder) ToContainer(c Container) *TranscodeBuilder
- func (b *TranscodeBuilder) ToVideoCodec(c Codec) *TranscodeBuilder
- func (b *TranscodeBuilder) WithAudioStreamTitles(titles map[int]string) *TranscodeBuilder
- func (b *TranscodeBuilder) WithAutoDownmixTitle() *TranscodeBuilder
- func (b *TranscodeBuilder) WithCoverArt(imageBytes []byte, mimeType string) *TranscodeBuilder
- func (b *TranscodeBuilder) WithCrop(params *CropParams) *TranscodeBuilder
- func (b *TranscodeBuilder) WithDefaultAudioStream(idx *int) *TranscodeBuilder
- func (b *TranscodeBuilder) WithDefaultSubtitleStream(idx *int) *TranscodeBuilder
- func (b *TranscodeBuilder) WithDownmix(idx *int) *TranscodeBuilder
- func (b *TranscodeBuilder) WithDownmixTitle(title string) *TranscodeBuilder
- func (b *TranscodeBuilder) WithH265CRF(crf int) *TranscodeBuilder
- func (b *TranscodeBuilder) WithHardwareDevice(path string) *TranscodeBuilder
- func (b *TranscodeBuilder) WithProgressChan(ch chan<- Progress) *TranscodeBuilder
- func (b *TranscodeBuilder) WithSubtitleStreamTitles(titles map[int]string) *TranscodeBuilder
- type Transcoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
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 ( // CodecH265 encodes video as H.265/HEVC (libx265 for software, or hardware variant). CodecH265 Codec = astiav.CodecIDH265 // CodecCopy copies the stream without re-encoding. CodecCopy Codec = astiav.CodecIDNone )
type CropParams ¶
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 GetHardwareEncoder ¶
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) 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) 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.