Documentation
¶
Overview ¶
Package av defines basic interfaces and data structures of container demux/mux and audio encode/decode.
Index ¶
- Constants
- Variables
- type AudioCodecData
- type AudioDecoder
- type AudioEncoder
- type AudioFrame
- type AudioResampler
- type ChannelLayout
- type CodecData
- type CodecType
- type DemuxCloser
- type Demuxer
- type HandshakeMuxCloser
- type HandshakeMuxer
- type HeaderPacket
- type MuxCloser
- type Muxer
- type Packet
- type PacketReader
- type PacketWriter
- type SampleFormat
- type VideoCodecData
Constants ¶
const ( U8 = SampleFormat(iota + 1) // 8-bit unsigned integer S16 // signed 16-bit integer S32 // signed 32-bit integer FLT // 32-bit float DBL // 64-bit float U8P // 8-bit unsigned integer in planar S16P // signed 16-bit integer in planar S32P // signed 32-bit integer in planar FLTP // 32-bit float in planar DBLP // 64-bit float in planar U32 // unsigned 32-bit integer )
const ( ChFrontCenter = ChannelLayout(1 << iota) ChFrontLeft ChFrontRight ChBackCenter ChBackLeft ChBackRight ChSideLeft ChSideRight ChLowFreq ChNr ChMono = ChFrontCenter ChStereo = ChFrontLeft | ChFrontRight Ch2_1 = ChStereo | ChBackCenter Ch2Point1 = ChStereo | ChLowFreq ChSurround = ChStereo | ChFrontCenter Ch3Point1 = ChSurround | ChLowFreq )
Variables ¶
var ( H264 = MakeVideoCodecType(avCodecTypeMagic + 1) //nolint:gochecknoglobals H265 = MakeVideoCodecType(avCodecTypeMagic + 2) //nolint:gochecknoglobals JPEG = MakeVideoCodecType(avCodecTypeMagic + 3) //nolint:gochecknoglobals VP8 = MakeVideoCodecType(avCodecTypeMagic + 4) //nolint:gochecknoglobals VP9 = MakeVideoCodecType(avCodecTypeMagic + 5) //nolint:gochecknoglobals AV1 = MakeVideoCodecType(avCodecTypeMagic + 6) //nolint:gochecknoglobals MJPEG = MakeVideoCodecType(avCodecTypeMagic + 7) //nolint:gochecknoglobals AAC = MakeAudioCodecType(avCodecTypeMagic + 1) //nolint:gochecknoglobals PCM_MULAW = MakeAudioCodecType(avCodecTypeMagic + 2) //nolint:gochecknoglobals,revive,stylecheck PCM_ALAW = MakeAudioCodecType(avCodecTypeMagic + 3) //nolint:gochecknoglobals,revive,stylecheck SPEEX = MakeAudioCodecType(avCodecTypeMagic + 4) //nolint:gochecknoglobals NELLYMOSER = MakeAudioCodecType(avCodecTypeMagic + 5) //nolint:gochecknoglobals PCM = MakeAudioCodecType(avCodecTypeMagic + 6) //nolint:gochecknoglobals OPUS = MakeAudioCodecType(avCodecTypeMagic + 7) //nolint:gochecknoglobals )
Functions ¶
This section is empty.
Types ¶
type AudioCodecData ¶
type AudioCodecData interface {
CodecData
SampleFormat() SampleFormat // audio sample format
SampleRate() int // audio sample rate
ChannelLayout() ChannelLayout // audio channel layout
PacketDuration(pkt []byte) (time.Duration, error) // get audio compressed packet duration
}
type AudioDecoder ¶ added in v0.5.0
type AudioDecoder interface {
Decode(data []byte) (bool, AudioFrame, error) // decode one compressed audio packet
Close() // close decode, free cgo contexts
}
AudioDecoder can decode compressed audio packets into raw audio frame. use ffmpeg.NewAudioDecoder to create it.
type AudioEncoder ¶ added in v0.5.0
type AudioEncoder interface {
CodecData() (AudioCodecData, error) // encoder's codec data can put into container
Encode(frame AudioFrame) ([][]byte, error) // encode raw audio frame into compressed pakcet(s)
Close() // close encoder, free cgo contexts
SetSampleRate(sampleRate int) error // set encoder sample rate
SetChannelLayout(channelLayout ChannelLayout) error // set encoder channel layout
SetSampleFormat(sampleFormat SampleFormat) error // set encoder sample format
SetBitrate(bitrate int) error // set encoder bitrate
SetOption(key string, option interface{}) error // encoder setopt, in ffmpeg is av_opt_set_dict()
GetOption(key string, option interface{}) error // encoder getopt
}
AudioEncoder can encode raw audio frame into compressed audio packets. cgo/ffmpeg inplements AudioEncoder, using ffmpeg.NewAudioEncoder to create it.
type AudioFrame ¶ added in v0.5.0
type AudioFrame struct {
SampleFormat SampleFormat // audio sample format, e.g: S16,FLTP,...
ChannelLayout ChannelLayout // audio channel layout, e.g: CH_MONO,CH_STEREO,...
SampleCount int // sample count in this frame
SampleRate int // sample rate
Data [][]byte // data array for planar format len(Data) > 1
}
Raw audio frame.
func (AudioFrame) Concat ¶ added in v0.5.0
func (s AudioFrame) Concat(in AudioFrame) AudioFrame
Concat two audio frames.
func (AudioFrame) Duration ¶ added in v0.5.0
func (s AudioFrame) Duration() time.Duration
func (AudioFrame) HasSameFormat ¶ added in v0.5.0
func (s AudioFrame) HasSameFormat(other AudioFrame) bool
Check this audio frame has same format as other audio frame.
func (AudioFrame) Slice ¶ added in v0.5.0
func (s AudioFrame) Slice(start int, end int) AudioFrame
Split sample audio sample from this frame.
type AudioResampler ¶ added in v0.5.0
type AudioResampler interface {
Resample(frame AudioFrame) (AudioFrame, error) // convert raw audio frames
}
AudioResampler can convert raw audio frames in different sample rate/format/channel layout.
type ChannelLayout ¶
type ChannelLayout uint16
ChannelLayout represents Audio channel layout.
func (ChannelLayout) Count ¶
func (s ChannelLayout) Count() int
func (ChannelLayout) String ¶
func (s ChannelLayout) String() string
type CodecData ¶
type CodecData interface {
Type() CodecType // Video/Audio codec type
}
CodecData is some important bytes for initialising audio/video decoder, can be converted to VideoCodecData or AudioCodecData using:
codecdata.(AudioCodecData) or codecdata.(VideoCodecData)
for H264, CodecData is AVCDecoderConfigure bytes, includes SPS/PPS. for H265, CodecData is AVCDecoderConfigure bytes, includes VPS/SPS/PPS.
type CodecType ¶
type CodecType uint32
CodecType represents Video/Audio codec type. can be H264/AAC/SPEEX/...
func MakeAudioCodecType ¶
MakeAudioCodecType makes a new audio codec type.
func MakeVideoCodecType ¶
MakeVideoCodecType makes a new video codec type.
type DemuxCloser ¶
DemuxCloser exposes Demuxer with Close() method.
type Demuxer ¶
type Demuxer interface {
Streams() ([]CodecData, error) // reads the header, contains video/audio meta infomations
PacketReader // read compressed audio/video packets
}
Demuxer can read compressed audio/video packets from container formats like MP4/FLV/MPEG-TS.
type HandshakeMuxCloser ¶
type HandshakeMuxCloser interface {
HandshakeMuxer
MuxCloser
}
type HandshakeMuxer ¶
type HeaderPacket ¶
type Muxer ¶
type Muxer interface {
WriteHeader(codecs []CodecData) error // write the file header
PacketWriter // write compressed audio/video packets
WriteTrailer() error // finish writing file, this func can be called only once
}
Muxer describes the steps of writing compressed audio/video packets into container formats like MP4/FLV/MPEG-TS.
Container formats, rtmp.Conn, and transcode.Muxer implements Muxer interface.
type Packet ¶
type Packet struct {
IsKeyFrame bool // video packet is key frame
Idx int8 // stream index in container format
CompositionTime time.Duration // packet presentation time minus decode time for H264 B-Frame
Time time.Duration // packet decode time
Duration time.Duration // packet duration
Data []byte // packet data
}
Packet stores compressed audio/video data.
type PacketReader ¶
type PacketWriter ¶
type SampleFormat ¶
type SampleFormat uint8
SampleFormat represents Audio sample format.
func (SampleFormat) BytesPerSample ¶
func (s SampleFormat) BytesPerSample() int
func (SampleFormat) IsPlanar ¶
func (s SampleFormat) IsPlanar() bool
IsPlanar Check if this sample format is in planar.
func (SampleFormat) String ¶
func (s SampleFormat) String() string