revid: using smarter startWebcam func as suggested by Dan

This commit is contained in:
saxon 2018-12-29 17:04:35 +10:30
parent 7f707288eb
commit e4e4e217c6
1 changed files with 48 additions and 9 deletions

View File

@ -499,27 +499,66 @@ func (r *Revid) startRaspivid() error {
} }
func (r *Revid) startWebcam() error { func (r *Revid) startWebcam() error {
r.config.Logger.Log(smartlogger.Info, pkg+"starting webcam") r.config.Logger.Log(smartlogger.Info, pkg+"starting V4L2 camera")
r.cmd = exec.Command("ffmpeg", []string{
"-i", "/dev/video0", // ffmpeg -f v4l2 -i /dev/video0 -crf 40 -c:v libx264 -b:v 200000 -maxrate 200000 -bufsize 100000 -g 100 -vf hflip,vflip -r 5 -s 320x240 -f flv -
"-r", "25",
"-f", "h264", "-", args := []string{
}...) "-f", "v4l2",
"-i", r.config.InputFileName,
}
switch r.config.InputCodec {
default:
return fmt.Errorf("revid: invalid input codec: %v", r.config.InputCodec)
case H264:
args = append(args,
"-c:v", "libx264",
"-g", r.config.IntraRefreshPeriod,
)
if r.config.QuantizationMode == QuantizationOn {
args = append(args, "-crf", r.config.Quantization)
}
case Mjpeg:
args = append(args, "-c:v", "mjpeg")
}
switch {
case r.config.FlipHorizontal == true && r.config.FlipVertical == true:
args = append(args, "-vf", "hflip,vflip")
case r.config.FlipHorizontal == true:
args = append(args, "-vf", "hflip")
case r.config.FlipVertical == true:
args = append(args, "-vf", "vflip")
}
br, err := strconv.Atoi(r.config.Bitrate)
if err != nil {
return err
}
args = append(args, "-b:v", r.config.Bitrate,
"-maxrate", r.config.Bitrate,
"-bufsize", fmt.Sprint(br/2),
"-r", r.config.FrameRate,
"-s", fmt.Sprintf("%sx%s", r.config.Width, r.config.Height),
"-f", "flv",
"-",
)
r.config.Logger.Log(smartlogger.Info, pkg+"ffmpeg args", "ffmpegArgs", strings.Join(args, " "))
r.cmd = exec.Command("ffmpeg", args...)
d, err := strconv.Atoi(r.config.FrameRate) d, err := strconv.Atoi(r.config.FrameRate)
if err != nil { if err != nil {
return err return err
} }
delay := time.Second / time.Duration(d) delay := time.Second / time.Duration(d)
stdout, err := r.cmd.StdoutPipe() stdout, err := r.cmd.StdoutPipe()
if err != nil { if err != nil {
return err return err
} }
err = r.cmd.Start() err = r.cmd.Start()
if err != nil { if err != nil {
r.config.Logger.Log(smartlogger.Fatal, pkg+"cannot start webcam", "error", err.Error())
return err return err
} }