-
Notifications
You must be signed in to change notification settings - Fork 871
/
main.go
64 lines (53 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// What it does:
//
// This example uses the VideoCapture class to capture AVI video from a connected webcam,
// then saves 100 frames to a video file on disk.
//
// How to run:
//
// savevideo [camera ID] [video file]
//
// go run ./cmd/savevideo/main.go 0 testvideo.avi
//
package main
import (
"fmt"
"os"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("How to run:\n\tsavevideo [camera ID] [video file]")
return
}
deviceID := os.Args[1]
saveFile := os.Args[2]
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
img := gocv.NewMat()
defer img.Close()
if ok := webcam.Read(&img); !ok {
fmt.Printf("Cannot read device %v\n", deviceID)
return
}
writer, err := gocv.VideoWriterFile(saveFile, "MJPG", 25, img.Cols(), img.Rows(), true)
if err != nil {
fmt.Printf("error opening video writer device: %v\n", saveFile)
return
}
defer writer.Close()
for i := 0; i < 100; i++ {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
writer.Write(img)
}
}