AlexxIT / go2rtc

Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc.
https://github.com/AlexxIT/Blog
MIT License
4.14k stars 332 forks source link

ffmpeg copy mjpeg on Raspberry Pi not outputting stream #747

Open dts350z opened 9 months ago

dts350z commented 9 months ago

On Raspberry Pi Bookworm (or DietPi) on Pi 5, Pi 4, and Pi zero 2 W, using docker and go2rtc 1.8.2, the "add" format for copying an mjpeg stream doesn't output a stream:

ffmpeg:device?video=/dev/video0&input_format=mjpeg&video_size=1920x1080

Produces a ffmpeg command:

ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -f mjpeg -

However when you go to: http://192.168.102.48:1984/stream.html?src=one&mode=mjpeg

It says mjpeg eof. I've also seen something about a header error, but didn't capture the error.

My input device:

glenn@raspberrypi:~ $ sudo v4l2-ctl --device=/dev/video0 --all
Driver Info:
        Driver name      : uvcvideo
        Card type        : USB Video: USB Video
        Bus info         : usb-0000:01:00.0-1.4
        Driver version   : 6.1.58
        Capabilities     : 0x84a00001
                Video Capture
                Metadata Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps      : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Media Driver Info:
        Driver name      : uvcvideo
        Model            : USB Video: USB Video
        Serial           :
        Bus info         : usb-0000:01:00.0-1.4
        Media version    : 6.1.58
        Hardware revision: 0x00002100 (8448)
        Driver version   : 6.1.58
Interface Info:
        ID               : 0x03000002
        Type             : V4L Video
Entity Info:
        ID               : 0x00000001 (1)
        Name             : USB Video: USB Video
        Function         : V4L2 I/O
        Flags            : default
        Pad 0x01000007   : 0: Sink
          Link 0x0200000d: from remote pad 0x100000a of entity 'Processing 2' (Video Pixel Formatter): Data, Enabled, Immutable
Priority: 2
Video input : 0 (Input 1: ok)
Format Video Capture:
        Width/Height      : 1920/1080
        Pixel Format      : 'YUYV' (YUYV 4:2:2)
        Field             : None
        Bytes per Line    : 3840
        Size Image        : 4147200
        Colorspace        : sRGB
        Transfer Function : Rec. 709
        YCbCr/HSV Encoding: ITU-R 601
        Quantization      : Default (maps to Limited Range)
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 1920, Height 1080
        Default     : Left 0, Top 0, Width 1920, Height 1080
        Pixel Aspect: 1/1
Selection Video Capture: crop_default, Left 0, Top 0, Width 1920, Height 1080, Flags:
Selection Video Capture: crop_bounds, Left 0, Top 0, Width 1920, Height 1080, Flags:
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 5.000 (5/1)
        Read buffers     : 0

User Controls

                     brightness 0x00980900 (int)    : min=-128 max=127 step=1 default=-11 value=-11
                       contrast 0x00980901 (int)    : min=0 max=255 step=1 default=148 value=148
                     saturation 0x00980902 (int)    : min=0 max=255 step=1 default=180 value=180
                            hue 0x00980903 (int)    : min=-128 max=127 step=1 default=0 value=0

Just FYI ,

streams:
  usb0:   ffmpeg:device?video=0#video=mjpeg

which gives:

ffmpeg -hide_banner -v error -f v4l2 -i /dev/video0 -c:v mjpeg -an -f mjpeg -

works, but I'd like to have ffmpeg just do the copy, rather than decode and re-encode the same format, to save CPU.

dts350z commented 9 months ago

Here is the error:

18:05:00.672 ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: streams: unsupported scheme: fffmpeg:device?video=/dev/video0&input_format=mjpeg&video_size=1920x1080"
AlexxIT commented 9 months ago
  1. Looks like you have triple f for fffmpeg in your config
  2. Why do you think, your camera supports the mjpeg codec?
dts350z commented 9 months ago

1) Typo. With the offending f removed (this is just the format string copied from the "Add" web UI):

mpeg: streams magic: unsupported header: ff80ff80 in the web page

and

18:48:52.201 ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: magic: unsupported header: 14500145"

on browser reload, and:

18:51:20.732 ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: EOF"

another time.

2) Not a camera, but an HDMI to USB interface. See the output of sudo v4l2-ctl --device=/dev/video0 --all I had pasted in above. Also, again, I am just taking your ffmpeg format string from the "add" web UI.

AlexxIT commented 9 months ago

Don't use add page for adding ffmpeg sources. Add them to the config page. And show Add > FFmpeg Device screenshot.

dts350z commented 9 months ago

image

image

image

image

dts350z commented 9 months ago

Just noticed that if I reboot, with all of the above already in place, vs. "Save & Restart", I do get an output, no errors, but "flashing" black output (output flashes on and off with all back showing when the video isn't).

AlexxIT commented 9 months ago

Can you record some data and post it here?

ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -t 10 -f mjpeg dump.mjpeg
dts350z commented 9 months ago
glenn@raspberrypi:~ $ ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -t 10 -f mjpeg dump.mjpeg
/dev/video0: Device or resource busy
glenn@raspberrypi:~ $ sudo systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
glenn@raspberrypi:~ $ ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -t 10 -f mjpeg dump.mjpeg
ioctl(VIDIOC_QBUF): Bad file descriptor

With an iphone connected to the HDMI to USB adapter:

glenn@raspberrypi:~ $ ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -t 10 -f mjpeg dump.mjpeg
[mjpeg @ 0x55a1c81540] bits 60 is invalid
File 'dump.mjpeg' already exists. Overwrite? [y/N] y
ioctl(VIDIOC_QBUF): Bad file descriptor

Resulting file plays OK in VLC either way

Note: this is the system os ffmpeg. I don't know how to access the ffmpeg inside the gotortc docker container.

[Uploading iphone-dump.zip…]()

AlexxIT commented 9 months ago

Your upload broker. Can you add it once again?

dts350z commented 9 months ago

[Uploading iphone-dump.zip…]()

dts350z commented 9 months ago

[Uploading lightning-adapter-only-.zip…]()

dts350z commented 9 months ago

[Uploading iphone-dump (2).zip…]()

AlexxIT commented 9 months ago

No. All of your uploading broken

dts350z commented 9 months ago

Please try this: https://drive.google.com/file/d/1P6FgESR87KEWvZsn80RmfVlsDyHjf-4m/view?usp=sharing

AlexxIT commented 9 months ago

Your MJPEG stream has some trash on start. I hope latest release will handle this https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.4

dts350z commented 9 months ago

Thanks for working on this.

The stream starts without error, and the CPU load is MUCH less, however the resulting stream (played in chrome, on windows) randomly and frequently "flashes" between normal and a black background. Very roughly one or more times per second or two.

Using the OS supplied ffmpeg:

sjaa@station251:~$ ffmpeg -hide_banner -v error -f v4l2 -input_format mjpeg -video_size 1920x1080 -i /dev/video0 -c copy -t 10 -f mjpeg dump.mjpeg
[mjpeg @ 0x55acf69570] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1144152 >= 1144152
[mjpeg @ 0x55acf69570] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 3744491 >= 3744491
[mjpeg @ 0x55acf69570] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 6144837 >= 6144837
ioctl(VIDIOC_QBUF): Bad file descriptor

Seems like that plays in VLC OK, however, without "flashing".

https://drive.google.com/file/d/1PBkv6NBON7C2pw19UwLk01tga6s_Sz8T/view?usp=sharing

I tried adding -r30 to the ffmpeg command. That gave a ton more dts error messages.

https://drive.google.com/file/d/1PDuoAEviHYxu71JLpApOjmYkb0O8jpeI/view?usp=sharing

Both of those files seem to play normally when streamed:

streams:
  usb0:   ffmpeg:device?video=0#video=mjpeg#hardware
  file0:  ffmpeg:/config/r30.mjpeg
  file1:  ffmpeg:/config/dump.mjpeg

however.

AlexxIT commented 8 months ago

Both your files has a lot of trash at the start. I have already increase probe size to 4KB. I don't think it is good idea to increase this size more. This can be problem for other users