Added multiplatform webcam streaming

This commit is contained in:
Alex Eidt 2021-12-26 23:52:44 -08:00
parent ba32c5c493
commit 0cc5ffe925
5 changed files with 375 additions and 61 deletions

103
README.md
View file

@ -1,6 +1,6 @@
# Video-IO
# Vidio
A simple Video I/O library written in Go. This library relies on [FFmpeg](https://www.ffmpeg.org/), which must be downloaded before usage.
A simple Video I/O library written in Go. This library relies on [FFmpeg](https://www.ffmpeg.org/), and [FFProbe](https://www.ffmpeg.org/) which must be downloaded before usage.
## `Video`
@ -9,16 +9,17 @@ The `Video` struct stores data about a video file you give it. The code below sh
```go
video := NewVideo("input.mp4")
for video.NextFrame() {
frame := video.framebuffer // "frame" stores the video frame as a flattened RGB image.
// "frame" stores the video frame as a flattened RGB image.
frame := video.framebuffer // stored as: RGBRGBRGBRGB...
}
```
```
```go
type Video struct {
filename string
width int
height int
channels int
depth int
bitrate int
frames int
duration float64
@ -31,43 +32,81 @@ type Video struct {
}
```
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...`.
## `Camera`
The `Camera` can read from any cameras on the device running Vidio.
```go
type Camera struct {
name string
width int
height int
depth int
fps float64
codec string
framebuffer []byte
pipe *io.ReadCloser
cmd *exec.Cmd
}
```
```go
camera := NewCamera(0) // Get Webcam
defer camera.Close()
// Stream the webcam.
for camera.NextFrame() {
// "frame" stores the video frame as a flattened RGB image.
frame := camera.framebuffer // stored as: 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.
```go
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.
type Options struct {
width int // Width of Output Frames
height int // Height of Output Frames
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
macro int // macro size for determining how to resize frames for codecs
fps float64 // Frames per second
codec string // Codec for video
in_pix_fmt string // Pixel Format of incoming bytes
out_pix_fmt string // Pixel Format for video being written
}
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.
```go
type VideoWriter struct {
filename string
width int
height int
bitrate int
loop int
delay int
macro int
fps float64
codec string
in_pix_fmt string
out_pix_fmt string
pipe *io.WriteCloser
cmd *exec.Cmd
}
```
```go
writer := VideoWriter{
filename: "output.mp4",
width: 1920,
height: 1080
...
}
defer writer.Close()
w, h, c := 1920, 1080, 3
options = Options{width: w, height: w, bitrate: 100000}
writer := NewVideoWriter("output.mp4", &options)
defer writer.Close() // Make sure to close writer.
frame = make([]byte, w*h*c) // Create Frame as RGB Image and modify.
writer.Write(frame) // Write Frame to video.
...
```
## Examples
@ -76,8 +115,14 @@ Copy `input` to `output`.
```go
video := NewVideo(input)
options := Options{
width: video.width,
height: video.height,
fps: video.fps,
bitrate: video.bitrate
}
writer := NewVideoWriter(output, video)
writer := NewVideoWriter(output, &options)
defer writer.Close()
for video.NextFrame() {