revid: added input flag for webcam input and added func to handle webcam input using ffmpeg

This commit is contained in:
saxon 2018-12-28 10:44:53 +10:30
parent 32e2d61ec1
commit 7118f1566c
3 changed files with 37 additions and 1 deletions

View File

@ -95,7 +95,7 @@ func handleFlags() revid.Config {
var ( var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") 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") inputCodecPtr = flag.String("InputCodec", "", "The codec of the input: H264, Mjpeg")
output1Ptr = flag.String("Output1", "", "The first output type: Http, Rtmp, File, Udp, Rtp") 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") output2Ptr = flag.String("Output2", "", "The second output type: Http, Rtmp, File, Udp, Rtp")
@ -143,6 +143,8 @@ func handleFlags() revid.Config {
} }
switch *inputPtr { switch *inputPtr {
case "webcam":
cfg.Input = revid.Webcam
case "Raspivid": case "Raspivid":
cfg.Input = revid.Raspivid cfg.Input = revid.Raspivid
case "File": case "File":

View File

@ -91,6 +91,7 @@ const (
Udp Udp
MpegtsRtp MpegtsRtp
Rtp Rtp
Webcam
) )
// Default config settings // Default config settings

View File

@ -259,6 +259,8 @@ func (r *Revid) reset(config Config) error {
} }
switch r.config.Input { switch r.config.Input {
case Webcam:
r.setupInput = r.startWebcam
case Raspivid: case Raspivid:
r.setupInput = r.startRaspivid r.setupInput = r.startRaspivid
case File: case File:
@ -496,6 +498,37 @@ func (r *Revid) startRaspivid() error {
return err 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 // setupInputForFile sets things up for getting input from a file
func (r *Revid) setupInputForFile() error { func (r *Revid) setupInputForFile() error {
fps, err := strconv.Atoi(r.config.FrameRate) fps, err := strconv.Atoi(r.config.FrameRate)