vidio

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2022 License: MIT Imports: 16 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 RGB format.

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.

FileName() string
Width() int
Height() int
Depth() int
Bitrate() int
Frames() int
Duration() float64
FPS() float64
Codec() string
AudioCodec() string
FrameBuffer() []byte
video := vidio.NewVideo("input.mp4")
for video.Read() {
	// "frame" stores the video frame as a flattened RGB image in row-major order
	frame := video.FrameBuffer() // stored as: RGBRGBRGBRGB...
	// Video processing here...
}

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. Note that audio retrieval from the microphone is not yet supported.

Name() string
Width() int
Height() int
Depth() int
FPS() float64
Codec() string
FrameBuffer() []byte
camera := vidio.NewCamera(0) // Get Webcam
defer camera.Close()

// Stream the webcam
for camera.Read() {
	// "frame" stores the video frame as a flattened RGB image
	frame := camera.FrameBuffer() // stored as: RGBRGBRGBRGB...
	// Video processing here...
}

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.

FileName() string
Width() int
Height() int
Bitrate() int
Loop() int
Delay() int
Macro() int
FPS() float64
Quality() float64
Codec() string
AudioCodec() string
type Options struct {
	Bitrate     int             // Bitrate
	Loop        int             // For GIFs only. -1=no loop, 0=loop forever, >0=loop n times
	Delay       int             // Delay for Final Frame of GIFs. Default -1 (Use same delay as previous frame)
	Macro       int             // macro size for determining how to resize frames for codecs. Default 16
	FPS         float64         // Frames per second. Default 25
	Quality     float64         // If bitrate not given, use quality instead. Must be between 0 and 1. 0:best, 1:worst
	Codec       string          // Codec for video. Default libx264
	Audio       string          // File path for audio for the video. If no audio, audio=nil.
	AudioCodec  string          // Codec for audio. Default aac
}
w, h, c := 1920, 1080, 3
options := vidio.Options{} // Will fill in defaults if empty

writer := vidio.NewVideoWriter("output.mp4", w, h, &options)
defer writer.Close()

frame := make([]byte, w*h*c) // Create Frame as RGB Image and modify
writer.Write(frame) // Write Frame to video

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.

// Read png image
w, h, img := vidio.Read("input.png")

// w - width of image
// h - height of image
// img - byte array in RGB format. RGBRGBRGBRGB...

vidio.Write("output.jpg", w, h, img)

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(),
	Audio: "input.mp4",
}

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

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

Grayscale 1000 frames of 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() {
	frame := webcam.FrameBuffer()
	for i := 0; i < len(frame); i += 3 {
		rgb := frame[i : i+3]
		r, g, b := int(rgb[0]), int(rgb[1]), int(rgb[2])
		gray := uint8((3*r + 4*g + b) / 8)
		frame[i] = gray
		frame[i+1] = gray
		frame[i+2] = gray
	}
	writer.Write(frame)
	count++
	if count > 1000 {
		break
	}
}

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++ {
	_, _, img := vidio.Read(strconv.Itoa(i)+".png")
	gif.Write(img)
}

Acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(filename string) (int, int, []byte)

Reads an image from a file. Currently only supports png and jpeg.

func Write

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

Writes an image 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

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

func (*Camera) FPS

func (camera *Camera) FPS() float64

func (*Camera) FrameBuffer

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

func (*Camera) Height

func (camera *Camera) Height() int

func (*Camera) Name

func (camera *Camera) Name() string

func (*Camera) Read

func (camera *Camera) Read() bool

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

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.
	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.
	Audio      string  // File path for audio. If no audio, audio=nil.
	AudioCodec string  // Codec for audio.
}

Optional parameters for VideoWriter.

type Video

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

func NewVideo

func NewVideo(filename string) *Video

Creates a new Video struct. Uses ffprobe to get video information and fills in the Video struct with this data.

func (*Video) AudioCodec

func (video *Video) AudioCodec() string

func (*Video) Bitrate

func (video *Video) Bitrate() int

Bitrate of video.

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

func (*Video) FPS

func (video *Video) FPS() float64

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) Height

func (video *Video) Height() int

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) 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

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

func (*VideoWriter) AudioCodec

func (writer *VideoWriter) AudioCodec() string

func (*VideoWriter) Bitrate

func (writer *VideoWriter) Bitrate() int

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

func (*VideoWriter) FPS

func (writer *VideoWriter) FPS() float64

func (*VideoWriter) FileName

func (writer *VideoWriter) FileName() string

func (*VideoWriter) Height

func (writer *VideoWriter) Height() int

func (*VideoWriter) Loop

func (writer *VideoWriter) Loop() int

func (*VideoWriter) Macro

func (writer *VideoWriter) Macro() int

func (*VideoWriter) Quality

func (writer *VideoWriter) Quality() float64

func (*VideoWriter) Width

func (writer *VideoWriter) Width() int

func (*VideoWriter) Write

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

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