transcode

package
v0.0.0-...-ae8f3be Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package transcode 提供 FFmpeg 转码引擎集成

本包实现了基于 FFmpeg 的视频转码功能:

  • 支持多种输入/输出格式
  • 支持硬件加速编解码
  • 支持滤镜处理
  • 支持实时流转码

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized = errors.New("transcode: engine not initialized")
	ErrAlreadyRunning = errors.New("transcode: engine already running")
	ErrInvalidInput   = errors.New("transcode: invalid input")
	ErrInvalidOutput  = errors.New("transcode: invalid output")
	ErrEncodeFailed   = errors.New("transcode: encode failed")
	ErrDecodeFailed   = errors.New("transcode: decode failed")
)

错误定义

Functions

func Transcode

func Transcode(ctx context.Context, input, output string, config Config) error

Transcode 便捷函数:转码文件

func TranscodeReader

func TranscodeReader(ctx context.Context, input io.Reader, output io.Writer, config Config) error

TranscodeReader 从 Reader 转码到 Writer

Types

type AudioConfig

type AudioConfig struct {
	Codec      av.CodecType // 目标编码格式
	SampleRate int          // 采样率
	Channels   int          // 声道数
	Bitrate    int          // 码率 (bps)
}

AudioConfig 音频转码配置

type Config

type Config struct {
	Video      *VideoConfig  // 视频配置(nil 表示复制)
	Audio      *AudioConfig  // 音频配置(nil 表示复制)
	CopyVideo  bool          // 直接复制视频流
	CopyAudio  bool          // 直接复制音频流
	StartTime  time.Duration // 开始时间
	Duration   time.Duration // 持续时间(0 表示到结尾)
	Threads    int           // 编码线程数
	HWDevice   string        // 硬件设备类型
	HWDeviceID string        // 硬件设备 ID
}

Config 转码配置

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 返回默认配置

type Engine

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

Engine FFmpeg 转码引擎

func NewEngine

func NewEngine(config Config) *Engine

NewEngine 创建转码引擎

func (*Engine) Close

func (e *Engine) Close() error

Close 关闭引擎

func (*Engine) Open

func (e *Engine) Open(inputURL string) error

Open 打开输入源

func (*Engine) Progress

func (e *Engine) Progress() Progress

Progress 返回当前进度

func (*Engine) SetCompleteCallback

func (e *Engine) SetCompleteCallback(fn func())

SetCompleteCallback 设置完成回调

func (*Engine) SetErrorCallback

func (e *Engine) SetErrorCallback(fn func(error))

SetErrorCallback 设置错误回调

func (*Engine) SetOutput

func (e *Engine) SetOutput(outputURL string, format string) error

SetOutput 设置输出

func (*Engine) SetProgressCallback

func (e *Engine) SetProgressCallback(fn func(Progress))

SetProgressCallback 设置进度回调

func (*Engine) Start

func (e *Engine) Start(ctx context.Context) error

Start 开始转码

func (*Engine) State

func (e *Engine) State() EngineState

State 返回当前状态

func (*Engine) Stop

func (e *Engine) Stop() error

Stop 停止转码

func (*Engine) Wait

func (e *Engine) Wait() error

Wait 等待转码完成

type EngineState

type EngineState int32

EngineState 转码引擎状态

const (
	StateIdle EngineState = iota
	StateRunning
	StatePaused
	StateStopping
	StateStopped
	StateFailed
)

func (EngineState) String

func (s EngineState) String() string

String 返回状态名称

type Progress

type Progress struct {
	FramesIn      int64         // 输入帧数
	FramesOut     int64         // 输出帧数
	BytesIn       int64         // 输入字节数
	BytesOut      int64         // 输出字节数
	Duration      time.Duration // 已处理时长
	Elapsed       time.Duration // 已运行时间
	Speed         float64       // 处理速度(相对于实时)
	BitrateIn     float64       // 输入码率 (kbps)
	BitrateOut    float64       // 输出码率 (kbps)
	PercentDone   float64       // 完成百分比
	EstimatedTime time.Duration // 预计剩余时间
}

Progress 转码进度

type VideoConfig

type VideoConfig struct {
	Codec    av.CodecType // 目标编码格式
	Width    int          // 目标宽度(0 表示保持原始)
	Height   int          // 目标高度(0 表示保持原始)
	Bitrate  int          // 目标码率 (bps)
	FPS      int          // 目标帧率(0 表示保持原始)
	Profile  string       // 编码 profile
	Preset   string       // 编码预设
	CRF      int          // 恒定质量因子(0 表示使用码率模式)
	GOPSize  int          // GOP 大小
	BFrames  int          // B 帧数量
	PixelFmt string       // 像素格式
	Filters  string       // FFmpeg 滤镜字符串
	Hardware bool         // 启用硬件加速
}

VideoConfig 视频转码配置

Source Files

  • engine.go

Jump to

Keyboard shortcuts

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