From d5191d9daf38cce08b1083492e31c976a79dab4a Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 3 Dec 2019 12:05:57 +1030 Subject: [PATCH 1/4] Revid CLI accepts MJPEG as an input codec The ffmpeg command uses the input codec value (H264 or mjpeg). However, it does not yet capture in mjpeg correctly. --- cmd/revid-cli/main.go | 2 ++ device/webcam/webcam.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index ea031a42..f8532fc0 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -202,6 +202,8 @@ func handleFlags() config.Config { cfg.InputCodec = codecutil.PCM case "ADPCM": cfg.InputCodec = codecutil.ADPCM + case "MJPEG": + cfg.InputCodec = codecutil.MJPEG default: log.Log(logger.Error, pkg+"bad input codec argument") } diff --git a/device/webcam/webcam.go b/device/webcam/webcam.go index 256a3900..788f4057 100644 --- a/device/webcam/webcam.go +++ b/device/webcam/webcam.go @@ -31,6 +31,7 @@ import ( "os/exec" "strings" + "bitbucket.org/ausocean/av/codec/codecutil" "bitbucket.org/ausocean/av/device" "bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/utils/logger" @@ -115,10 +116,22 @@ func (w *Webcam) Set(c config.Config) error { func (w *Webcam) Start() error { args := []string{ "-i", w.cfg.InputPath, - "-f", "h264", "-r", fmt.Sprint(w.cfg.FrameRate), } + switch w.cfg.InputCodec { + default: + return fmt.Errorf("revid: invalid input codec: %v", w.cfg.InputCodec) + case codecutil.H264: + args = append(args, + "-f", "h264", + ) + case codecutil.MJPEG: + args = append(args, + "-f", "mjpeg", + ) + } + br := w.cfg.Bitrate * 1000 args = append(args, "-b:v", fmt.Sprint(br), @@ -129,6 +142,7 @@ func (w *Webcam) Start() error { ) w.log.Log(logger.Info, pkg+"ffmpeg args", "args", strings.Join(args, " ")) + fmt.Printf("%v", args) // !!!! w.cmd = exec.Command("ffmpeg", args...) var err error From 48d36c20823b2fd2fcddf215319c9758aae69359 Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 3 Dec 2019 14:36:16 +1030 Subject: [PATCH 2/4] Ffmpeg gets different args for mjpeg encoding --- device/webcam/webcam.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/device/webcam/webcam.go b/device/webcam/webcam.go index 788f4057..a987f913 100644 --- a/device/webcam/webcam.go +++ b/device/webcam/webcam.go @@ -114,10 +114,14 @@ func (w *Webcam) Set(c config.Config) error { // Start will build the required arguments for ffmpeg and then execute the // command, piping video output where we can read using the Read method. func (w *Webcam) Start() error { + br := w.cfg.Bitrate * 1000 + args := []string{ "-i", w.cfg.InputPath, "-r", fmt.Sprint(w.cfg.FrameRate), - } + "-b:v", fmt.Sprint(br), + "-s", fmt.Sprintf("%dx%d", w.cfg.Width, w.cfg.Height), + } switch w.cfg.InputCodec { default: @@ -125,6 +129,8 @@ func (w *Webcam) Start() error { case codecutil.H264: args = append(args, "-f", "h264", + "-maxrate", fmt.Sprint(br), + "-bufsize", fmt.Sprint(br/2), ) case codecutil.MJPEG: args = append(args, @@ -132,17 +138,11 @@ func (w *Webcam) Start() error { ) } - br := w.cfg.Bitrate * 1000 args = append(args, - "-b:v", fmt.Sprint(br), - "-maxrate", fmt.Sprint(br), - "-bufsize", fmt.Sprint(br/2), - "-s", fmt.Sprintf("%dx%d", w.cfg.Width, w.cfg.Height), "-", ) w.log.Log(logger.Info, pkg+"ffmpeg args", "args", strings.Join(args, " ")) - fmt.Printf("%v", args) // !!!! w.cmd = exec.Command("ffmpeg", args...) var err error From 565ba3079cbbceb6cd4c18d5496db78a1dfb3f83 Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 3 Dec 2019 14:55:08 +1030 Subject: [PATCH 3/4] Formatted code --- cmd/revid-cli/main.go | 2 +- device/webcam/webcam.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index f8532fc0..59b31be6 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -202,7 +202,7 @@ func handleFlags() config.Config { cfg.InputCodec = codecutil.PCM case "ADPCM": cfg.InputCodec = codecutil.ADPCM - case "MJPEG": + case "MJPEG": cfg.InputCodec = codecutil.MJPEG default: log.Log(logger.Error, pkg+"bad input codec argument") diff --git a/device/webcam/webcam.go b/device/webcam/webcam.go index a987f913..0ac08751 100644 --- a/device/webcam/webcam.go +++ b/device/webcam/webcam.go @@ -121,7 +121,7 @@ func (w *Webcam) Start() error { "-r", fmt.Sprint(w.cfg.FrameRate), "-b:v", fmt.Sprint(br), "-s", fmt.Sprintf("%dx%d", w.cfg.Width, w.cfg.Height), - } + } switch w.cfg.InputCodec { default: From 70802022bb80a5754981b6898069fbefb6331d77 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 4 Dec 2019 11:40:12 +1030 Subject: [PATCH 4/4] revid/revid.go MPEG lexer enabled and encoder options set for v4l --- revid/revid.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/revid/revid.go b/revid/revid.go index 786b75f5..d0bc73f3 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -191,7 +191,15 @@ func (r *Revid) reset(c config.Config) error { panic("unknown input codec for raspivid input") } case config.InputFile, config.InputV4L: - st = mts.EncodeH264 + switch r.cfg.InputCodec { + case codecutil.H264: + st = mts.EncodeH264 + case codecutil.MJPEG: + st = mts.EncodeMJPEG + encOptions = append(encOptions, mts.PacketBasedPSI(int(r.cfg.MinFrames))) + default: + panic("unknown input codec for v4l or input file input") + } case config.InputRTSP: switch r.cfg.InputCodec { case codecutil.H265: @@ -329,7 +337,12 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io. case config.InputV4L: r.input = webcam.New(r.cfg.Logger) - r.lexTo = h264.Lex + switch r.cfg.InputCodec { + case codecutil.H264: + r.lexTo = h264.Lex + case codecutil.MJPEG: + r.lexTo = mjpeg.Lex + } case config.InputFile: r.input = file.New()