Page:
Scanner AV Streaming on VM in Unraid
12
Scanner AV Streaming on VM in Unraid
Skylar Grant edited this page 2024-07-14 16:32:53 +00:00
This article will go through my setup for hosting a scanner A/V stream from an Ubuntu Server VM.
Stack
- UnRAID 6.12 running on an old Alienware desktop
- Ubuntu 20.04.6 LTS Virtual Machine
- ALSA
- ffmpeg
- mediamtx
- Radioshack Pro-651 (any scanner with headphone-out should work)
- Logitech C90 webcam
Guide
Assign devices to VM
(This is not necessary if you are using a USB sound card)
- In UnRAID go to Tools -> System Devices
- Check the box(es) for the IOMMU group(s) for your audio card
- Reboot the server
Set up the VM
- Download Ubuntu Server ISO to UnRAID server
- Create an Ubuntu VM in UnRAID
-- Pass through 2-4 CPU cores for installation, can be scaled back later
-- Choose 2048-4096MB minimum RAM, at least.
-- 10GB vdisk should be enough, 20GB is what I used.
-- Leave graphic card as is, you'll need VNC to complete the initial installation
-- Assign the sound card to the VM
-- Assign the USB webcam to the VM - Start up the VM and proceed through installation of Ubuntu Server
- (Optional) Shutdown the VM and adjust the specs back to minimal (1G RAM, 1-2CPU cores)
Set up the Software Stack
- Run these commands
sudo apt update && sudo apt upgrade -y
sudo apt install ffmpeg -y
wget https://github.com/bluenviron/mediamtx/releases/download/v1.8.4/mediamtx_v1.8.4_linux_amd64.tar.gz
tar -xvf mediamtx_v1.8.4_linux_amd64.tar.gz
sudo mkdir /usr/local/share/mediamtx
sudo mv mediamtx /usr/local/bin/
sudo mv mediamtx.yml /usr/local/share/mediamtx/
- Edit mediamtx.yml and remove the
paths:
at the end, replacing with:
paths:
scanner:
source: publisher
runOnInit: ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 -c:v libx264 -preset veryfast -tune zerolatency -pix_fmt yuv420p -f rtsp rtsp://localhost:8554/scanner
- Run it with
mediamtx /usr/local/share/mediamtx/mediamtx.yml
and test, adjusting configuration as needed.
Daemonize the Stream
- Create a service file:
sudo nano /etc/systemd/system/mediamtx.service
- Place the following in the file:
[Unit]
Description=MediaMTX Streaming Service
After=network.target
[Service]
ExecStart=mediamtx /usr/local/share/mediamtx/mediamtx.yml
Restart=always
User=nobody
Group=nogroup
WorkingDirectory=/usr/local/bin/
[Install]
WantedBy=multi-user.target
- Reload
systemd
:sudo systemctl daemon-reload
- Enable the service at boot:
sudo systemctl enable mediamtx
- Start the service now:
sudo service mediamtx start
- Check on the status:
sudo service mediamtx status
- Get more log info:
journalctl -u mediamtx
Accessing the Stream
Simply open up http://[serverFQDN/IP]:8888/scanner
to view the stream, or put the HLS stream in a webpage hosted via nginx or on another machine. I used a reverse proxy to point directly at the HLS stream port.
mediamtx
and ffmpeg
Configuration Information
Paths Section:
- paths: This section contains the definitions for different streaming paths.
- scanner: This is the name of the path. You can name it whatever makes sense for your use case, such as
scanner
,camera
, orstream
.
- scanner: This is the name of the path. You can name it whatever makes sense for your use case, such as
Path Configuration:
- source: publisher: This specifies that the stream for this path will be published by an external source. In this case, the source is the FFmpeg command specified in
runOnInit
. - runOnInit: This directive tells MediaMTX to run the specified command when the server starts. The command provided here will initialize the stream.
FFmpeg Command Explanation:
- ffmpeg: This is the command-line tool used to handle multimedia data.
- -f v4l2 -i /dev/video0: This tells FFmpeg to use the Video4Linux2 (v4l2) input format to capture video from the device
/dev/video0
, which is your USB webcam. - -f alsa -i hw:0: This specifies the ALSA input format to capture audio from the device
hw:0
. - -c:v libx264: This sets the video codec to H.264 using the
libx264
encoder. - -preset veryfast: This option sets the encoding preset to
veryfast
, balancing compression efficiency and encoding speed. - -tune zerolatency: This tuning option minimizes latency, which is crucial for real-time streaming.
- -pix_fmt yuv420p: This sets the pixel format to
yuv420p
, which is widely compatible with most players and devices. - -f rtsp rtsp://localhost:8554/scanner: This sets the output format to RTSP and specifies the RTSP URL where the stream will be available, in this case,
rtsp://localhost:8554/scanner
.
This configuration sets up MediaMTX to serve RTSP and HLS streams, with the scanner
path specifically set up to capture and stream video and audio using FFmpeg.