我想要捕获并保存一个mjpeg格式的视频文件,并将分辨率设置为1920x1080p。当我不指定分辨率时,捕捉效果很好,我可以用30fps录制和播放它。但是如果我改变记录的图像大小,我会得到一个时间间隔。网络摄像头会记录数倍的时间,并将整个拍摄时间放入所需的视频长度中。例如,摄像头应该以30fps记录120帧,所以视频捕获应该持续4秒,以及记录的视频长度。相反,捕获持续了大约20秒。整个20秒都在4秒内,这给出了一个时间间隔。我该如何避免这种情况呢?我是否使用了错误的命令来定义分辨率?这是我的代码,我已经尝试过"webcam.Set(3,1920)“等,以及gocv.NewMat()或NewMatWithSize(...)。以下是我的代码
/*-------------------------------------------- savevideotest.go -----
|
| Created -
|
| Version 1.0 2021-01-03 Initial version
|------------------------------------------------------------------
| Version - - -
|------------------------------------------------------------------
| Purpose: Video capture tool
| Video Capture Tool to be used as main tool when it comes
| to recording videos with the raspberry pi
*-------------------------------------------------------------------*/
package main
import (
"flag"
"fmt"
"gocv.io/x/gocv"
"strconv"
"strings"
"sync"
"time"
"github.com/op/go-logging"
"os"
)
var(
videolength = 120 //videolength in frames (recording with 25fps). 15k = 10min
framecheck [2]int
videoformat *string
location *string
camcount *int
path *string
log = logging.MustGetLogger("example")
format = logging.MustStringFormatter(`%{color} %{time:2006:Jan:2:15:04:05.000} %{shortfunc} > %{level:.4s} %{id:03x}%{color:reset} %{message}`) // Mon Jan 2 15:04:05 MST 2006 )
)
/*-------------------------------------------- init -----
| Function init
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Initialize flags for usage in other functions by parsing
| Parameters: ---
| Returns: ---
*-------------------------------------------------------------------*/
func init() {
videoformat = flag.String("videoformat",".avi","desired video format")
location = flag.String("location", "BU_", "location of raspberry pi")
camcount = flag.Int("camcount",1,"number of used cams")
path = flag.String("path","","sets the path of videofile")
}
/*-------------------------------------------- main -----
| Function main
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: main function, creating infinite recording loop
| with error control(var framecheck) and log generation
| Parameters: ---
| Returns: ---
*-------------------------------------------------------------------*/
func main() {
flag.Parse()
printHeader()
for {
savevideo(videolength)
//fmt.Printf("%v\n",framecheck)
for k:= 0; k < *camcount; k++ {
if framecheck[k] != videolength {
fmt.Printf("Webcam %v hat nicht die geforderte Videolänge aufgenommen! Das Programm wird terminiert.", k)
writeVideoLog(k)
writeErrorLog(k)
return
}else {
//framecheck[k] = 0
writeVideoLog(k)
}
}
}
}
/*-------------------------------------------- generateFileName -----
| Function AddItem
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Function to generate the desired Filename by using flags
| Parameters: deviceID int
| Returns: filename.String
*-------------------------------------------------------------------*/
func generateFileName(j int) string {
flag.Parse()
var filename strings.Builder
filename.WriteString(*path)
filename.WriteString(*location)
filename.WriteString("__")
filename.WriteString("Cam")
filename.WriteString(strconv.Itoa(j))
filename.WriteString("__")
filename.WriteString(time.Now().Format("20060102_150405"))
filename.WriteString(*videoformat)
return filename.String()
}
/*-------------------------------------------- saveVideo -----
| Function saveVideo
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Function to record and save the video
| Parameters: videolength int
| Returns: ---
*-------------------------------------------------------------------*/
func savevideo(videolength int) {
flag.Parse()
var wg sync.WaitGroup
wg.Add(*camcount)
for j := 0; j < *camcount; j++ {
go func(j int) {
defer wg.Done()
deviceID := j
saveFile := generateFileName(j)
webcam, err := gocv.VideoCaptureDevice(deviceID)// OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
img := gocv.NewMatWithSize(1280, 720, gocv.MatTypeCV64F)
defer img.Close()
webcam.Set(3, 1280)
webcam.Set(4, 780)
if ok := webcam.Read(&img); !ok {
fmt.Printf("Cannot read device %v\n", deviceID)
return
}
writer, err := gocv.VideoWriterFile(saveFile, "MJPG", 30, 1280, 720, true)
if err != nil {
fmt.Printf("error opening video writer device: %v\n", saveFile)
return
}
defer writer.Close()
fmt.Printf("%v %v ", img.Cols(), img.Rows())
for i := 0; i < videolength; i++ {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
framecheck[j]++
writer.Write(img)
}
}(j)
}
wg.Wait()
}
/*-------------------------------------------- printHeader -----
| Function printHeader
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Function to print the header of the logfile
| Parameters: ---
| Returns: ---
*-------------------------------------------------------------------*/
func printHeader() {
flag.Parse()
file, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
backend1 := logging.NewLogBackend(file, "", 0)
backend2 := logging.NewLogBackend(file, "", 0)
// For messages written to backend2 we want to add some additional
// information to the output, including the used log level and the name of
// the function.
backend2Formatter := logging.NewBackendFormatter(backend2, format)
// Only errors and more severe messages should be sent to backend1
backend1Leveled := logging.AddModuleLevel(backend1)
backend1Leveled.SetLevel(logging.ERROR, "")
// Set the backends to be used.
logging.SetBackend(backend1Leveled, backend2Formatter)
log.Info("|---------------|--------|---------------------------\n")
log.Info("|BUSA VIDEO LOG | |\n")
log.Info("|---------------|--------|---------------------------\n")
log.Info("|LOG SETUP | INFO | ROTATIONSIZE 800000000 [KBytes]]\n")
log.Info("| | | Maximum size of Video directory\n")
log.Info("| | | before old videos get deleted\n")
log.Info("| |--------|---------------------------\n")
log.Info("| | INFO | MaxFileSizeLOG 500 [KBytes]\n")
log.Info("| | | Maximum size of Logfile\n")
log.Info("| | | before old logfiles get copied\n")
log.Info("| | | away to BUSA_VIDEO.log.TIMESTAMP\n")
log.Info("| |--------|---------------------------\n")
log.Info("| | INFO | MaxNumOldLOGS 10\n")
log.Info("| | | Maximum number of old Logfiles\n")
log.Info("| | | before oldest logfile get deleted\n")
log.Info("|---------------|--------|---------------------------\n")
log.Info("|LOCATION SETUP | INFO | LOCATION_NAME " + *location + "\n")
log.Info("| | | Name of Location\n")
log.Info("| | | Used in filename\n")
log.Info("| |--------|---------------------------\n")
log.Info("| | INFO | OBJECT_ID 1500021BU027-542~~~~~\n")
log.Info("| | | ID for MQTT Topic to identify Object\n")
log.Info("| | | where video are recorded\n")
log.Info("|---------------|--------|---------------------------\n")
log.Info("|MQTT SETUP | INFO | localIP 10.170.63.11\n")
log.Info("| | | IP Addr that get used\n")
log.Info("| | | in MQTT message for URL of Video\n")
log.Info("| |--------|---------------------------\n")
log.Info("| | INFO | BUSA_MQTT_BROKER\n")
log.Info("| | | MQTT Broker\n")
log.Info("| |--------|---------------------------\n")
log.Info("| | INFO | BOX_ID 70820e1224c5\n")
log.Info("| | | Hardware ID derived from MAC-Addr\n")
log.Info("|---------------|--------|---------------------------\n")
}
/*-------------------------------------------- writeErrorLog -----
| Function writeErrorLog
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Function to print an error log entry in case something wents wrong
| Parameters: deviceID
| Returns: ---
*-------------------------------------------------------------------*/
func writeErrorLog(k int) {
flag.Parse()
file, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
backend1 := logging.NewLogBackend(file, "", 0)
backend2 := logging.NewLogBackend(file, "", 0)
// For messages written to backend2 we want to add some additional
// information to the output, including the used log level and the name of
// the function.
backend2Formatter := logging.NewBackendFormatter(backend2, format)
// Only errors and more severe messages should be sent to backend1
backend1Leveled := logging.AddModuleLevel(backend1)
backend1Leveled.SetLevel(logging.ERROR, "")
// Set the backends to be used.
logging.SetBackend(backend1Leveled, backend2Formatter)
log.Error(" | ERROR | Kritischer Fehler: Webcam " + strconv.Itoa(k) + " hat nicht die erforderliche Anzahl an Frames aufgenommen. Die Anwendung wurde beendet.\n")
}
/*-------------------------------------------- writeVideoLog -----
| Function writeVideoLog
| Created 01.03.2021 -
| Last change -- / ---- - --
| Purpose: Function to write an info log entry when a video is saved
| Parameters: deviceID
| Returns: ---
*-------------------------------------------------------------------*/
func writeVideoLog(k int) {
flag.Parse()
file, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
backend1 := logging.NewLogBackend(file, "", 0)
backend2 := logging.NewLogBackend(file, "", 0)
// For messages written to backend2 we want to add some additional
// information to the output, including the used log level and the name of
// the function.
backend2Formatter := logging.NewBackendFormatter(backend2, format)
// Only errors and more severe messages should be sent to backend1
backend1Leveled := logging.AddModuleLevel(backend1)
backend1Leveled.SetLevel(logging.ERROR, "")
// Set the backends to be used.
logging.SetBackend(backend1Leveled, backend2Formatter)
log.Info(" | INFO | Neues Video " + generateFileName(k) + "\n")
}哦,对了,我还是个编程新手,尤其是对Golang。我学习Java已经有一个月了,但作为实习生在go项目中工作,以了解作为一名开发人员的感觉。(PS:我希望deadprogram能看到这一点,他似乎是golang和gocv中的神,我到目前为止查到的每一个问题都在他下面:D)
发布于 2021-08-22 12:15:12
您是否尝试过使用以下命令设置FPS
webcam.Set(5, 30)其中5是FPS标志。
https://stackoverflow.com/questions/66547544
复制相似问题