FFmpeg wrapper providing simple, cross-platform Video I/O, GIF Creation, and Webcam Streaming in Go.
Find a file
2021-11-22 19:38:37 -08:00
.gitignore Initial commit 2021-11-22 19:14:37 -08:00
demo.go Cleaned up demo.go 2021-11-22 19:38:37 -08:00
go.mod Initial commit 2021-11-22 19:14:37 -08:00
go.sum Initial commit 2021-11-22 19:14:37 -08:00
imageio.go Initial commit 2021-11-22 19:14:37 -08:00
parsing.go Initial commit 2021-11-22 19:14:37 -08:00
README.md Update README 2021-11-22 19:17:31 -08:00
utils.go Initial commit 2021-11-22 19:14:37 -08:00
video.go Initial commit 2021-11-22 19:14:37 -08:00
videowriter.go Cleaned up demo.go 2021-11-22 19:38:37 -08:00

Video-IO

A simple Video I/O library written in Go. This library relies on FFMPEG, which must be downloaded before usage.

One of the key features of this library is it's simplicity: The FFMPEG commands used to read and write video are readily available in video.go and videowriter.go for you to modify as you need.

For portability, all the library code can be found in the videoio.go file.

Documentation

Video-IO features Video and VideoWriter structs which can read and write videos.

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.

video := NewVideo("input.mp4")
for video.NextFrame() {
    frame := video.framebuffer // "frame" stores the video frame as a flattened RGB image.
}

Notice that once the video is initialized, you will have access to certain metadata of the video such as the

  • width (pixels)
  • height (pixels)
  • bitrate (kB/s)
  • duration (seconds)
  • frames per second
  • video codec
  • pixel format

Once the frame is read by calling the NextFrame() function, the resulting frame is stored in the framebuffer as shown above. The frame buffer is an array of bytes representing the most recently read frame as an RGB image. The framebuffer is flattened and contains image data in the form: RGBRGBRGBRGB....

VideoWriter

The VideoWriter is used to write frames to a video file. You first need to create a Video struct with all the desired properties of the new video you want to create such as width, height and framerate.

video := Video{
    // width and height are required, defaults available for all other parameters.
    width:  1920,
    height: 1080,
    ... // Initialize other desired properties of the video you want to create.
}
writer := NewVideoWriter("output.mp4", video)
defer writer.Close() // Make sure to close writer.

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

Alternatively, you could manually create a VideoWriter struct and fill it in yourself.

writer := VideoWriter{
    filename:   "output.mp4",
    width:      1920,
    height:     1080
    ...
}
defer writer.Close()

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

Examples

Copy input to output.

video := NewVideo(input)

writer := NewVideoWriter(output, video)
defer writer.Close()

for video.NextFrame() {
    writer.Write(video.framebuffer)
}

Acknowledgements

  • Special thanks to Zulko and his blog post about using FFMPEG to process video.