Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 1.44 KB

README.md

File metadata and controls

60 lines (49 loc) · 1.44 KB

Capture example

This example shows how to use the go4vl API to create a simple program that captures a video frames from an attached input (camera) device.

Firstly, the source code opens a device, devName, with a hard-coded pixel format (MPEG) and size. If the device does not support the specified format, the open operation will fail, returning an error.

func main() {
    devName := "/dev/video0"
	// open device
	device, err := device.Open(
		devName,
		device.WithPixFormat(
			v4l2.PixFormat{PixelFormat: v4l2.PixelFmtMPEG, Width: 640, Height: 480},
		),
	)
...
}

Next, the source code calls the device.Start method to start the input (capture) process.

func main() {
...
	// start stream
	ctx, stop := context.WithCancel(context.TODO())
	if err := device.Start(ctx); err != nil {
		log.Fatalf("failed to start stream: %s", err)
	}
...

}

Once the device starts, the code sets up a loop capture incoming video frame buffer from the input device and save each frame to a local file.

func main() {
...
	for frame := range device.GetOutput() {
		fileName := fmt.Sprintf("capture_%d.jpg", count)
		file, err := os.Create(fileName)
		...
		if _, err := file.Write(frame); err != nil {
			log.Printf("failed to write file %s: %s", fileName, err)
			continue
		}
		
		if err := file.Close(); err != nil {
			log.Printf("failed to close file %s: %s", fileName, err)
		}
	}

}

See the full source code here.