Sunday, September 18, 2022

[Solved] Streaming USB webcam via command command line with cvlc (VLC) with MJPG output

Welp, I've been using VLC to stream my webcam to my LAN so I can see when my Amazon packages arrive since my doorbell isn't that loud. At first I used motion since it was the only thing that somewhat worked, but it was horrid...people looked like they were just going across my screen only with the same leg up and just moving every second instead of actually walking. Then I did some tweaks with VLC and it was laggy but it worked way better than motion. After many hours I found that Theora and Ogg had the smoothest results.

Then, I wanted to use only the command line version since it would reduce memory usage by half...then some more hours to even get it working and then some more to get MJPG working then realizing all that time was pointless since now it's way faster without transcoding lol. I also found out that using MKV is (for me) faster than Ogg with far less dropped frames. For MKV you need the libmatroska-dev library. 

    sudo apt-get install libmatroska-dev

Anyways...for those who want to know how to set the GUI for decent webcam streaming on your LAN...see below.


1. Select your video device (duh)
2. Check the [Show more options] box and then fill in the Advanced options.
2 (a) If you want to use MJPG instead of the default YUYV, fill in the third line [Video input chroma format] with MJPG, otherwise leave it blank or whatever other option you need, such as h264. Set the other info so that you can get all your settings done at once. Then click OK
3. At the bottom in the [Edit Options] box, copy the whole line. That's your v4l2 options. You'll need that later so save it into a file otherwise you have to select your options EVERY TIME.

INFO:I use HTTP as my stream so your settings may differ if you stream with something else. And I'm only streaming at 800x448 resolution. Again, it works for my purpose. 

4. Click Stream -> Next, click the pull-down and select HTTP and click Add -> Next.
If you want to transcode, create a new profile. Select Ogg/Ogm

-As mentioned earlier, I switched to MKV-
Then click the [Video codec] tab -> check the Video checkbox and select Theora in the Codec pull-down. Adjust the Frame Rate to fit your needs. I use 16 since it's good enough and doesn't stutter. If you want audio then select that as well. For my purposes, I don't use audio to keep the stream fast. Then click Create, and then Next.

5. Copy the [Generated stream output string] to be used later for the command line option.

6. Click Stream to make sure it works.

7. To save on RAM usage, use the command line program, cvlc

cvlc -vvv <your device> <the v4l2 options> <the transcode info>

    The -vvv device is in [MRL] just above [Edit Options]

cvlc -vvv v4l2:///dev/video0 --no-audio :v4l2-standard= :v4l2-dev=/dev/video0 :v4l2-vbidev= :v4l2-chroma=MJPG :v4l2-input=0 :v4l2-audio-input=-1 :v4l2-width=800 :v4l2-height=448 :v4l2-aspect-ratio= :v4l2-fps=16 :no-v4l2-controls-reset :v4l2-brightness=-1 :v4l2-brightness-auto=-1 :v4l2-contrast=-1 :v4l2-saturation=-1 :v4l2-hue=-1 :v4l2-hue-auto=-1 :v4l2-white-balance-temperature=-1 :v4l2-auto-white-balance=-1 :v4l2-red-balance=-1 :v4l2-blue-balance=-1 :v4l2-gamma=-1 :v4l2-autogain=-1 :v4l2-gain=-1 :v4l2-sharpness=-1 :v4l2-chroma-gain=-1 :v4l2-chroma-gain-auto=-1 :v4l2-power-line-frequency=0 :v4l2-backlight-compensation=-1 :v4l2-band-stop-filter=-1 :no-v4l2-hflip :no-v4l2-vflip :v4l2-rotate=-1 :v4l2-color-killer=-1 :v4l2-color-effect=-1 :v4l2-audio-volume=-1 :v4l2-audio-balance=-1 :no-v4l2-audio-mute :v4l2-audio-bass=-1 :v4l2-audio-treble=-1 :no-v4l2-audio-loudness :v4l2-set-ctrls= :live-caching=150 --sout '#transcode{vcodec=theo,vb=815,fps=16,scale=Auto,width=800,height=448,acodec=none,scodec=none}:http{mux=mkv,dst=:8080/}' --no-sout-all --sout-keep --quiet

And if you don't want or need to transcode (since I set the resolution and the fps and all the other options, I'm sending it as I want it...although the fps ends up as 250 [sometimes 1000, not sure what's going on there] instead of 16!)

cvlc -vvv v4l2:///dev/video0 --no-audio :v4l2-standard= :v4l2-dev=/dev/video0 :v4l2-vbidev= :v4l2-chroma=MJPG :v4l2-input=0 :v4l2-audio-input=-1 :v4l2-width=800 :v4l2-height=448 :v4l2-aspect-ratio= :v4l2-fps=16 :no-v4l2-controls-reset :v4l2-brightness=-1 :v4l2-brightness-auto=-1 :v4l2-contrast=-1 :v4l2-saturation=-1 :v4l2-hue=-1 :v4l2-hue-auto=-1 :v4l2-white-balance-temperature=-1 :v4l2-auto-white-balance=-1 :v4l2-red-balance=-1 :v4l2-blue-balance=-1 :v4l2-gamma=-1 :v4l2-autogain=-1 :v4l2-gain=-1 :v4l2-sharpness=-1 :v4l2-chroma-gain=-1 :v4l2-chroma-gain-auto=-1 :v4l2-power-line-frequency=0 :v4l2-backlight-compensation=-1 :v4l2-band-stop-filter=-1 :no-v4l2-hflip :no-v4l2-vflip :v4l2-rotate=-1 :v4l2-color-killer=-1 :v4l2-color-effect=-1 :v4l2-audio-volume=-1 :v4l2-audio-balance=-1 :no-v4l2-audio-mute :v4l2-audio-bass=-1 :v4l2-audio-treble=-1 :no-v4l2-audio-loudness :v4l2-set-ctrls= :live-caching=150 --sout '#standard{access=http,mux=mkv,dst=:8080/}' --quiet


Transcoding with MJPG is about 60% CPU usage, YUYV is about 35% CPU usage, and no transcoding is about 2% (evenings due to less light) to 7% (daytime due to brighter colors) CPU usage. I went with no transcoding since I'm running Linux on a mini PC with only 2 cores and 2 threads.

If you want to just use the GUI, skip the [Advanced options] part and just paste the v4l2 options, which you saved earlier, directly in the [Edit Options] box. You still need to select your [Video device name] tho. Then select the transcode profile you created or uncheck the [Activate Transcode] checkbox to skip transcoding, and then stream.

Hope this helps!

No comments:

Post a Comment