mjpeg

package module
v0.0.0-...-38318e5 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 6 Imported by: 38

README

mjpeg

Build Status Go Reference Go Report Card

MJPEG video writer implementation in Go.

Examples

Let's see an example how to turn the JPEG files 1.jpg, 2.jpg, ..., 10.jpg into a movie file:

checkErr := func(err error) {
    if err != nil {
        panic(err)
    }
}

// Video size: 200x100 pixels, FPS: 2
aw, err := mjpeg.New("test.avi", 200, 100, 2)
checkErr(err)

// Create a movie from images: 1.jpg, 2.jpg, ..., 10.jpg
for i := 1; i <= 10; i++ {
    data, err := ioutil.ReadFile(fmt.Sprintf("%d.jpg", i))
    checkErr(err)
    checkErr(aw.AddFrame(data))
}

checkErr(aw.Close())

Example to add an image.Image as a frame to the video:

aw, err := mjpeg.New("test.avi", 200, 100, 2)
checkErr(err)

var img image.Image
// Acquire / initialize image, e.g.:
// img = image.NewRGBA(image.Rect(0, 0, 200, 100))

buf := &bytes.Buffer{}
checkErr(jpeg.Encode(buf, img, nil))
checkErr(aw.AddFrame(buf.Bytes()))

checkErr(aw.Close())

Documentation

Overview

Package mjpeg contains an MJPEG video format writer.

Examples

Let's see an example how to turn the JPEG files 1.jpg, 2.jpg, ..., 10.jpg into a movie file:

checkErr := func(err error) {
    if err != nil {
        panic(err)
    }
}

// Video size: 200x100 pixels, FPS: 2
aw, err := mjpeg.New("test.avi", 200, 100, 2)
checkErr(err)

// Create a movie from images: 1.jpg, 2.jpg, ..., 10.jpg
for i := 1; i <= 10; i++ {
    data, err := ioutil.ReadFile(fmt.Sprintf("%d.jpg", i))
    checkErr(err)
    checkErr(aw.AddFrame(data))
}

checkErr(aw.Close())

Example to add an image.Image as a frame to the video:

aw, err := mjpeg.New("test.avi", 200, 100, 2)
checkErr(err)

var img image.Image
// Acquire / initialize image, e.g.:
// img = image.NewRGBA(image.Rect(0, 0, 200, 100))

buf := &bytes.Buffer{}
checkErr(jpeg.Encode(buf, img, nil))
checkErr(aw.AddFrame(buf.Bytes()))

checkErr(aw.Close())

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooLarge reports if more frames cannot be added,
	// else the video file would get corrupted.
	ErrTooLarge = errors.New("Video file too large")
)

Functions

This section is empty.

Types

type AviWriter

type AviWriter interface {
	// AddFrame adds a frame from a JPEG encoded data slice.
	AddFrame(jpegData []byte) error

	// Close finalizes and closes the avi file.
	Close() error
}

AviWriter is an *.avi video writer. The video codec is MJPEG.

func New

func New(aviFile string, width, height, fps int32) (awr AviWriter, err error)

New returns a new AviWriter. The Close() method of the AviWriter must be called to finalize the video file.

Directories

Path Synopsis
This example encodes all jpeg images (*.jpg) into an MJPEG movie file (flying-gopher.avi).
This example encodes all jpeg images (*.jpg) into an MJPEG movie file (flying-gopher.avi).

Jump to

Keyboard shortcuts

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