x264

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: GPL-2.0 Imports: 9 Imported by: 21 modules, 23 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.

README

x264-go

Build Status Go Reference Go Report Card

x264-go provides H.264/MPEG-4 AVC codec encoder based on x264 library.

C source code is included in package. If you want to use an external shared/static library (i.e., built with asm and/or OpenCL) use -tags extlib

Installation

go get -u github.com/gen2brain/x264-go

Build tags

  • extlib - use external x264 library
  • pkgconfig - enable pkg-config (used with extlib)

Examples

See screengrab example.

Usage

package main

import (
	"bytes"
	"image"
	"image/color"
	"image/draw"

	"github.com/gen2brain/x264-go"
)

func main() {
	buf := bytes.NewBuffer(make([]byte, 0))

	opts := &x264.Options{
		Width:     640,
		Height:    480,
		FrameRate: 25,
		Tune:      "zerolatency",
		Preset:    "veryfast",
		Profile:   "baseline",
		LogLevel:  x264.LogDebug,
	}

	enc, err := x264.NewEncoder(buf, opts)
	if err != nil {
		panic(err)
	}

	img := x264.NewYCbCr(image.Rect(0, 0, opts.Width, opts.Height))
	draw.Draw(img, img.Bounds(), image.Black, image.Point{}, draw.Src)

	for i := 0; i < opts.Width/2; i++ {
		img.Set(i, opts.Height/2, color.RGBA{255, 0, 0, 255})

		err = enc.Encode(img)
		if err != nil {
			panic(err)
		}
	}

	err = enc.Flush()
	if err != nil {
		panic(err)
	}

	err = enc.Close()
	if err != nil {
		panic(err)
	}
}

More

For AAC encoder see aac-go.

Documentation

Overview

Package x264 provides H.264/MPEG-4 AVC codec encoder based on x264(https://www.videolan.org/developers/x264.html) library.

Index

Constants

View Source
const (
	LogNone int32 = iota - 1
	LogError
	LogWarning
	LogInfo
	LogDebug
)

Logging constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

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

Encoder type.

func NewEncoder

func NewEncoder(w io.Writer, opts *Options) (e *Encoder, err error)

NewEncoder returns new x264 encoder.

func (*Encoder) Close

func (e *Encoder) Close() error

Close closes encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(im image.Image) (err error)

Encode encodes image.

func (*Encoder) Flush

func (e *Encoder) Flush() (err error)

Flush flushes encoder.

func (*Encoder) Timestamp added in v0.3.0

func (e *Encoder) Timestamp() (int64, int64)

Timestamp returns the current PTS and DTS.

type Options

type Options struct {
	// Frame width.
	Width int
	// Frame height.
	Height int
	// Frame rate.
	FrameRate int
	// Tunings: film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
	Tune string
	// Presets: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
	Preset string
	// Profiles: baseline, main, high, high10, high422, high444.
	Profile string
	// KeyInt is the maximum interval between keyframes (GOP size) in frames. Defaults to FrameRate when zero.
	KeyInt int
	// IntraRefresh uses periodic intra refresh instead of IDR keyframes: lower latency, but no seekable keyframes. Off by default.
	IntraRefresh bool
	// RateControl: cqp, crf, abr.
	RateControl string
	// Quality target: quantizer for cqp, CRF value for crf.
	RateConstant float32
	// Upper bound: max quantizer for cqp, max CRF for crf, VBV max bitrate (kbit/s) for abr.
	RateMax float32
	// Target average bitrate (kbit/s). Required for abr.
	Bitrate int
	// VBV buffer size (kbit). Optional; bounds rate fluctuation when RateMax is set for abr.
	VbvBufferSize int
	// Log level.
	LogLevel int32
}

Options represent encoding options.

type YCbCr

type YCbCr struct {
	*image.YCbCr
}

YCbCr is an in-memory image of Y'CbCr colors.

func NewYCbCr

func NewYCbCr(r image.Rectangle) *YCbCr

NewYCbCr returns a new YCbCr image with the given bounds and subsample ratio.

func (*YCbCr) Set

func (p *YCbCr) Set(x, y int, c color.Color)

Set sets pixel color.

func (*YCbCr) ToYCbCr

func (p *YCbCr) ToYCbCr(src image.Image)

ToYCbCr converts image.Image to YCbCr.

func (*YCbCr) ToYCbCrColor added in v0.2.0

func (p *YCbCr) ToYCbCrColor(src image.Image)

ToYCbCrColor converts image.Image to YCbCr.

func (*YCbCr) ToYCbCrDraw

func (p *YCbCr) ToYCbCrDraw(src image.Image)

ToYCbCrDraw converts image.Image to YCbCr.

Directories

Path Synopsis
Package x264c implements cgo bindings for [x264](https://www.videolan.org/developers/x264.html) library.
Package x264c implements cgo bindings for [x264](https://www.videolan.org/developers/x264.html) library.

Jump to

Keyboard shortcuts

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