This commit is contained in:
cpu
2023-12-31 17:23:19 +01:00
parent 47679930fd
commit 3b4fae99a1

View File

@@ -1,9 +1,12 @@
Prerequisities
--------------
Update the Raspberry Pi OS
> `sudo apt update`
`sudo apt full-upgrade`
Install the collection of command line video4linux utilities
> `sudo apt-get install v4l-utils`
> `sudo apt install v4l-utils ffmpeg`
Lists all available cameras
> `v4l2-ctl --list-devices`
@@ -64,14 +67,9 @@ Or using ffmpeg
Compressed: mjpeg : Motion-JPEG : 160x120 176x144 320x240 352x288 640x480 800x600 960x720
Raw : yuyv422 : YUYV 4:2:2 : 160x120 176x144 320x240 352x288 640x480 800x600 960x720 1600x1200
```
Lists all available audio recording devices
> `arecord -l`
```
**** List of CAPTURE Hardware Devices ****
card 1: Q9000 [QuickCam Pro 9000], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
```
Record a video
> `ffmpeg -f v4l2 -input_format h264 -video_size 1920x1080 -framerate 30 -i /dev/video0 -vcodec copy video.mp4`
Live Streaming
--------------
Use you own `LIVE-STREAM-KEY` and run
@@ -84,6 +82,14 @@ Note: Does not work on Waylad ([What is Wayland in Linux Distros and Should You
> `DISPLAY=:0 ffmpeg -hide_banner -f x11grab -framerate 5 -video_size 830x630 -i :0.0+50,95 -vf format=pix_fmts=yuv420p -vcodec libx264 -tune zerolatency -preset veryfast -f flv rtmp://peertube.virtonline.eu:1935/live/LIVE-STREAM-KEY`
### Live stream camera with sound and desktop as an overlay to peertube
Lists all available audio recording devices
> `arecord -l`
```
**** List of CAPTURE Hardware Devices ****
card 1: Q9000 [QuickCam Pro 9000], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
```
> `DISPLAY=:0` # select display (only if run remotely e.g. ssh)
`ffmpeg -hide_banner` # do not output compile flags
`-thread_queue_size 4096 -f pulse -ac 2 -i default` # audio input from PulseAudio
@@ -105,56 +111,52 @@ Use the dockerized ffmpeg with precompiled vaapi support, adjust `/dev/video2`
Adjust `/dev/video2`, `-video_size 1920x1080 -framerate 60` and stream to peertube (if it can handle that much)
> `ffmpeg -hide_banner -y -f v4l2 -input_format mjpeg -video_size 1920x1080 -framerate 60 -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i /dev/video2 -vf 'scale_vaapi=format=nv12' -c:v h264_vaapi -b:v 10M -f flv rtmp://peertube.virtonline.eu:1935/live/LIVE-STREAM-KEY`
### Setup a systemd service for the non-stop streaming
Adjust `jrottenberg/ffmpeg:4.4-alpine` if you want to use a specific ffmpeg version. For Raspberry Pi (ARM) see https://github.com/colin-nolan/docker-ffmpeg-rpi
### Setup a systemd service for a non-stop streaming
```
sudo tee /etc/systemd/system/virt-garage-webcam-streamer.service <<EOF
sudo tee /etc/systemd/system/webcam-streamer.service <<EOF
[Unit]
Description=Garage Webcam Streamer service
Requires=docker.service
After=docker.service
Description=Webcam Streamer service
Requires=network-online.target
After=network-online.target
DefaultDependencies=no
[Service]
Type=simple
Environment="HOME=/root"
Environment=WEBCAM_URI="rtsp://garagecam.virtonline.eu:554/live3.sdp"
Environment=PEERTUBE_LIVE="rtmp://peertube.virtonline.eu:1935/live/LIVE-STREAM-KEY"
Environment=FFMPEG="docker run --rm --name virt-garage-webcam-streamer jrottenberg/ffmpeg:4.4-alpine"
ExecStartPre=-/usr/bin/env sh -c '/usr/bin/env docker kill virt-garage-webcam-streamer 2>/dev/null || true'
ExecStartPre=-/usr/bin/env sh -c '/usr/bin/env docker rm virt-garage-webcam-streamer 2>/dev/null || true'
ExecStartPre=-/usr/bin/env sh -c 'killall ffmpeg 2>/dev/null || true'
# Re-stream (Audio/Video) from IP camera to peertube live
ExecStart=/bin/bash -c '${FFMPEG} -loglevel error \
-i ${WEBCAM_URI} \
-c copy \
-f flv \
ExecStart=/bin/bash -c 'ffmpeg -loglevel error \
-f v4l2 -input_format h264 \
-video_size 1920x1080 \
-framerate 20 \
-i /dev/video0 \
-c copy -f flv \
-tune zerolatency \
${PEERTUBE_LIVE}'
ExecStop=-/usr/bin/env sh -c '/usr/bin/env docker kill virt-garage-webcam-streamer 2>/dev/null || true'
ExecStop=-/usr/bin/env sh -c '/usr/bin/env docker rm virt-garage-webcam-streamer 2>/dev/null || true'
ExecStop=-/usr/bin/env sh -c 'killall ffmpeg 2>/dev/null || true'
Restart=always
RestartSec=5
SyslogIdentifier=virt-garage-webcam-streamer
SyslogIdentifier=webcam-streamer
[Install]
WantedBy=multi-user.target
EOF
```
Reload systemd manager configuration
`sudo systemctl daemon-reload`
Enable the virt-garage-webcam-streamer service at boot time and also run it
`sudo systemctl enable --now virt-garage-webcam-streamer`
Start the service
`sudo systemctl start webcam-streamer`
If it does not work check logs
`journalctl -f -u webcam-streamer`
`journalctl -f -u virt-garage-webcam-streamer`
Enable the webcam-streamer service at boot time
`sudo systemctl enable webcam-streamer`
### Transcode and scale video file (and copy audio) using VAAPI (see [HWAccelIntro](https://trac.ffmpeg.org/wiki/HWAccelIntro))
> `alias ffmpeg='docker run --rm -w $(pwd) -v $(pwd):$(pwd) --device=/dev/dri jrottenberg/ffmpeg:vaapi'`