vidio

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 17 Imported by: 7

README

Vidio

A simple Video I/O library written in Go. This library relies on FFmpeg, and FFProbe which must be downloaded before usage and added to the system path.

All frames are encoded and decoded in 8-bit RGBA format.

For Audio I/O using FFmpeg, see the aio project.

Installation

go get github.com/AlexEidt/Vidio

Video

The Video struct stores data about a video file you give it. The code below shows an example of sequentially reading the frames of the given video.

Calling the Read() function will fill in the Video struct framebuffer with the next frame data as 8-bit RGBA data, stored in a flattened byte array in row-major order where each pixel is represented by four consecutive bytes representing the R, G, B and A components of that pixel. Note that the A (alpha) component will always be 255. When iteration over the entire video file is not required, we can lookup a specific frame by calling ReadFrame(n int). By calling ReadFrames(n ...int), we can immediately access multiple frames as a slice of RGBA images and skip the framebuffer.

vidio.NewVideo(filename string) (*vidio.Video, error)
vidio.NewVideoStreams(filename string) ([]*vidio.Video, error)

FileName() string
Width() int
Height() int
Depth() int
Bitrate() int
Frames() int
Stream() int
Duration() float64
FPS() float64
Codec() string
HasStreams() bool
FrameBuffer() []byte
MetaData() map[string]string
SetFrameBuffer(buffer []byte) error

Read() bool
ReadFrame(n int) error
ReadFrames(n ...int) ([]*image.RGBA, error)
Close()

If all frames have been read, video will be closed automatically. If not all frames are read, call video.Close() to close the video.

Camera

The Camera can read from any cameras on the device running Vidio. It takes in the stream index. On most machines the webcam device has index 0.

vidio.NewCamera(stream int) (*vidio.Camera, error)

Name() string
Width() int
Height() int
Depth() int
FPS() float64
Codec() string
FrameBuffer() []byte
SetFrameBuffer(buffer []byte) error

Read() bool
Close()

VideoWriter

The VideoWriter is used to write frames to a video file. The only required parameters are the output file name, the width and height of the frames being written, and an Options struct. This contains all the desired properties of the new video you want to create.

vidio.NewVideoWriter(filename string, width, height int, options *vidio.Options) (*vidio.VideoWriter, error)

FileName() string
StreamFile() string
Width() int
Height() int
Bitrate() int
Loop() int
Delay() int
Macro() int
FPS() float64
Quality() float64
Codec() string

Write(frame []byte) error
Close()
type Options struct {
	Bitrate    int     // Bitrate.
	Loop       int     // For GIFs only. -1=no loop, 0=infinite loop, >0=number of loops.
	Delay      int     // Delay for final frame of GIFs in centiseconds.
	Macro      int     // Macroblock size for determining how to resize frames for codecs.
	FPS        float64 // Frames per second for output video.
	Quality    float64 // If bitrate not given, use quality instead. Must be between 0 and 1. 0:best, 1:worst.
	Codec      string  // Codec for video.
	StreamFile string  // File path for extra stream data.
}

The Options.StreamFile parameter is intended for users who wish to process a video stream and keep the audio (or other streams). Instead of having to process the video and store in a file and then combine with the original audio later, the user can simply pass in the original file path via the Options.StreamFile parameter. This will combine the video with all other streams in the given file (Audio, Subtitle, Data, and Attachments Streams) and will cut all streams to be the same length. Note that Vidio is not a audio/video editing library.

This means that adding extra stream data from a file will only work if the filename being written to is a container format.

Images

Vidio provides some convenience functions for reading and writing to images using an array of bytes. Currently, only png and jpeg formats are supported. When reading images, an optional buffer can be passed in to avoid array reallocation.

Read(filename string, buffer ...[]byte) (int, int, []byte, error)
Write(filename string, width, height int, buffer []byte) error

Examples

Copy input.mp4 to output.mp4. Copy the audio from input.mp4 to output.mp4 as well.

video, _ := vidio.NewVideo("input.mp4")
options := vidio.Options{
	FPS: video.FPS(),
	Bitrate: video.Bitrate(),
}
if video.HasStreams() {
	options.StreamFile = video.FileName()
}

writer, _ := vidio.NewVideoWriter("output.mp4", video.Width(), video.Height(), &options)

defer writer.Close()

for video.Read() {
    writer.Write(video.FrameBuffer())
}

Read 1000 frames of a webcam stream and store in output.mp4.

webcam, _ := vidio.NewCamera(0)
defer webcam.Close()

options := vidio.Options{FPS: webcam.FPS()}
writer, _ := vidio.NewVideoWriter("output.mp4", webcam.Width(), webcam.Height(), &options)
defer writer.Close()

count := 0
for webcam.Read() && count < 1000 {
	writer.Write(webcam.FrameBuffer())
	count++
}

Create a gif from a series of png files enumerated from 1 to 10 that loops continuously with a final frame delay of 1000 centiseconds.

w, h, _, _ := vidio.Read("1.png") // Get frame dimensions from first image

options := vidio.Options{FPS: 1, Loop: 0, Delay: 1000}
gif, _ := vidio.NewVideoWriter("output.gif", w, h, &options)
defer gif.Close()

for i := 1; i <= 10; i++ {
	w, h, img, _ := vidio.Read(fmt.Sprintf("%d.png", i))
	gif.Write(img)
}

Write all frames of video.mp4 as jpg images.

video, _ := vidio.NewVideo("video.mp4")

img := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
video.SetFrameBuffer(img.Pix)

frame := 0
for video.Read() {
	f, _ := os.Create(fmt.Sprintf("%d.jpg", frame))
	jpeg.Encode(f, img, nil)
	f.Close()
	frame++
}

Write the last frame of video.mp4 as jpg image (without iterating over all video frames).

video, _ := video.NewVideo("video.mp4")

img := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
video.SetFrameBuffer(img.Pix)

video.ReadFrame(video.Frames() - 1)

f, _ := os.Create(fmt.Sprintf("%d.jpg", video.Frames() - 1))
jpeg.Encode(f, img, nil)
f.Close()

Write the first and last frames of video.mp4 as jpg images (without iterating over all video frames).

video, _ := vidio.NewVideo("video.mp4")

frames, _ := video.ReadFrames(0, video.Frames() - 1)

for index, frame := range frames {
	f, _ := os.Create(fmt.Sprintf("%d.jpg", index))
	jpeg.Encode(f, frame, nil)
	f.Close()
}

Acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(filename string, buffer ...[]byte) (int, int, []byte, error)

Reads an image into an rgba byte buffer from a file. Currently only supports png and jpeg.

func Write

func Write(filename string, width, height int, buffer []byte) error

Writes a rgba byte buffer to a file. Currently only supports png and jpeg.

Types

type Camera

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

func NewCamera

func NewCamera(stream int) (*Camera, error)

Creates a new camera struct that can read from the device with the given stream index.

func (*Camera) Close

func (camera *Camera) Close()

Closes the pipe and stops the ffmpeg process.

func (*Camera) Codec

func (camera *Camera) Codec() string

func (*Camera) Depth

func (camera *Camera) Depth() int

Channels of video frames.

func (*Camera) FPS

func (camera *Camera) FPS() float64

Frames per second of video.

func (*Camera) FrameBuffer

func (camera *Camera) FrameBuffer() []byte

func (*Camera) Height

func (camera *Camera) Height() int

func (*Camera) Name

func (camera *Camera) Name() string

Camera device name.

func (*Camera) Read

func (camera *Camera) Read() bool

Reads the next frame from the webcam and stores in the framebuffer.

func (*Camera) SetFrameBuffer added in v1.1.0

func (camera *Camera) SetFrameBuffer(buffer []byte) error

func (*Camera) Width

func (camera *Camera) Width() int

type Options

type Options struct {
	Bitrate    int     // Bitrate.
	Loop       int     // For GIFs only. -1=no loop, 0=infinite loop, >0=number of loops.
	Delay      int     // Delay for final frame of GIFs in centiseconds.
	Macro      int     // Macroblock size for determining how to resize frames for codecs.
	FPS        float64 // Frames per second for output video.
	Quality    float64 // If bitrate not given, use quality instead. Must be between 0 and 1. 0:best, 1:worst.
	Codec      string  // Codec for video.
	StreamFile string  // File path for extra stream data.
}

Optional parameters for VideoWriter.

type Video

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

func NewVideo

func NewVideo(filename string) (*Video, error)

func NewVideoStreams added in v1.2.0

func NewVideoStreams(filename string) ([]*Video, error)

Read all video streams from the given file.

func (*Video) Bitrate

func (video *Video) Bitrate() int

Bitrate of video in bits/s.

func (*Video) Close

func (video *Video) Close()

Closes the pipe and stops the ffmpeg process.

func (*Video) Codec

func (video *Video) Codec() string

func (*Video) Depth

func (video *Video) Depth() int

Channels of video frames.

func (*Video) Duration

func (video *Video) Duration() float64

Video duration in seconds.

func (*Video) FPS

func (video *Video) FPS() float64

Frames per second of video.

func (*Video) FileName

func (video *Video) FileName() string

func (*Video) FrameBuffer

func (video *Video) FrameBuffer() []byte

func (*Video) Frames

func (video *Video) Frames() int

Total number of frames in video.

func (*Video) HasStreams added in v1.3.0

func (video *Video) HasStreams() bool

Returns true if file has any audio, subtitle, data or attachment streams.

func (*Video) Height

func (video *Video) Height() int

func (*Video) MetaData added in v1.2.0

func (video *Video) MetaData() map[string]string

Raw Metadata from ffprobe output for the video file.

func (*Video) Read

func (video *Video) Read() bool

Reads the next frame from the video and stores in the framebuffer. If the last frame has been read, returns false, otherwise true.

func (*Video) ReadFrame added in v1.5.0

func (video *Video) ReadFrame(n int) error

Reads the N-th frame from the video and stores it in the framebuffer. If the index is out of range or the operation failes, the function will return an error. The frames are indexed from 0.

func (*Video) ReadFrames added in v1.5.0

func (video *Video) ReadFrames(n ...int) ([]*image.RGBA, error)

Read the N-amount of frames with the given indexes and return them as a slice of RGBA image pointers. If one of the indexes is out of range, the function will return an error. The frames are indexes from 0.

func (*Video) SetFrameBuffer added in v1.1.0

func (video *Video) SetFrameBuffer(buffer []byte) error

func (*Video) Stream added in v1.2.0

func (video *Video) Stream() int

Returns the zero-indexed video stream index.

func (*Video) Width

func (video *Video) Width() int

type VideoWriter

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

func NewVideoWriter

func NewVideoWriter(filename string, width, height int, options *Options) (*VideoWriter, error)

Creates a new VideoWriter struct with default values from the Options struct.

func (*VideoWriter) Bitrate

func (writer *VideoWriter) Bitrate() int

Bitrate of video in bits/s.

func (*VideoWriter) Close

func (writer *VideoWriter) Close()

Closes the pipe and stops the ffmpeg process.

func (*VideoWriter) Codec

func (writer *VideoWriter) Codec() string

func (*VideoWriter) Delay

func (writer *VideoWriter) Delay() int

Delay for final frame of GIFs in centiseconds.

func (*VideoWriter) FPS

func (writer *VideoWriter) FPS() float64

Frames per second of video.

func (*VideoWriter) FileName

func (writer *VideoWriter) FileName() string

func (*VideoWriter) Height

func (writer *VideoWriter) Height() int

func (*VideoWriter) Loop

func (writer *VideoWriter) Loop() int

For GIFs only, defines looping behavior. -1=no loop, 0=infinite loop, >0=number of loops.

func (*VideoWriter) Macro

func (writer *VideoWriter) Macro() int

Macroblock size for determining how to resize frames for codecs.

func (*VideoWriter) Quality

func (writer *VideoWriter) Quality() float64

Video Codec Quality parameter. Must be between 0 and 1. 0:best, 1:worst.

func (*VideoWriter) StreamFile added in v1.3.0

func (writer *VideoWriter) StreamFile() string

File used to fill in extra stream data.

func (*VideoWriter) Width

func (writer *VideoWriter) Width() int

func (*VideoWriter) Write

func (writer *VideoWriter) Write(frame []byte) error

Writes the given frame to the video file.

Jump to

Keyboard shortcuts

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