From 7118f1566cf42fe3a45dd66a159995ba9f6892eb Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 28 Dec 2018 10:44:53 +1030 Subject: [PATCH] revid: added input flag for webcam input and added func to handle webcam input using ffmpeg --- cmd/revid-cli/main.go | 4 +++- revid/config.go | 1 + revid/revid.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index 32b18be6..36256c0c 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -95,7 +95,7 @@ func handleFlags() revid.Config { var ( cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") - inputPtr = flag.String("Input", "", "The input type: Raspivid, File") + inputPtr = flag.String("Input", "", "The input type: Raspivid, File, Webcam") inputCodecPtr = flag.String("InputCodec", "", "The codec of the input: H264, Mjpeg") output1Ptr = flag.String("Output1", "", "The first output type: Http, Rtmp, File, Udp, Rtp") output2Ptr = flag.String("Output2", "", "The second output type: Http, Rtmp, File, Udp, Rtp") @@ -143,6 +143,8 @@ func handleFlags() revid.Config { } switch *inputPtr { + case "webcam": + cfg.Input = revid.Webcam case "Raspivid": cfg.Input = revid.Raspivid case "File": diff --git a/revid/config.go b/revid/config.go index 06a4b8da..23c1e58c 100644 --- a/revid/config.go +++ b/revid/config.go @@ -91,6 +91,7 @@ const ( Udp MpegtsRtp Rtp + Webcam ) // Default config settings diff --git a/revid/revid.go b/revid/revid.go index c686ece8..4eb45106 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -259,6 +259,8 @@ func (r *Revid) reset(config Config) error { } switch r.config.Input { + case Webcam: + r.setupInput = r.startWebcam case Raspivid: r.setupInput = r.startRaspivid case File: @@ -496,6 +498,37 @@ func (r *Revid) startRaspivid() error { return err } +func (r *Revid) startWebcam() error { + r.config.Logger.Log(smartlogger.Info, pkg+"starting webcam") + r.cmd = exec.Command("ffmpeg", []string{ + "-i", "/dev/video0", + "-r", "25", + "-f", "h264", "-", + }...) + + d, err := strconv.Atoi(r.config.FrameRate) + if err != nil { + return err + } + + delay := time.Second / time.Duration(d) + stdout, err := r.cmd.StdoutPipe() + if err != nil { + return err + } + + err = r.cmd.Start() + if err != nil { + r.config.Logger.Log(smartlogger.Fatal, pkg+"cannot start webcam", "error", err.Error()) + return err + } + + r.config.Logger.Log(smartlogger.Info, pkg+"reading camera data") + err = r.lexTo(r.encoder, stdout, delay) + r.config.Logger.Log(smartlogger.Info, pkg+"finished reading camera data") + return err +} + // setupInputForFile sets things up for getting input from a file func (r *Revid) setupInputForFile() error { fps, err := strconv.Atoi(r.config.FrameRate)