Skip to content

Instantly share code, notes, and snippets.

@mark-kubacki
Created September 21, 2021 13:32
Show Gist options
  • Save mark-kubacki/7b0be1494057f0b2de2d9704cc6e2e7a to your computer and use it in GitHub Desktop.
Save mark-kubacki/7b0be1494057f0b2de2d9704cc6e2e7a to your computer and use it in GitHub Desktop.
webcam to gpu encoding
[Unit]
Description=Local surveillance using the webcam
Requisite=pulseaudio.service
[Service]
WorkingDirectory=%h/Aufnahmen
# Might want to skip restarting if all you need are the first 2h.
Restart=on-success
TimeoutStopSec=10s
ExecStart=%h/record-cam.sh vaapi
SuccessExitStatus=255
Nice=-2
; PrivateNetwork=true
#!/bin/bash
# Takes the raw data stream from your webcam,
# denoises it using the CPU,
# to finally have the stream encoded by your GPU (APU in my case).
# Zen1+ is limited to 8bpp, which is "nv12"
# Zen2 and later is able to encode 10bpp, which is "p010"
#
# Without the trailing "scale_vaapi" you'd get a duplicate bw image.
# For a list of video formats supported by your webcam see:
# ffmpeg -f video4linux2 -list_formats all -i /dev/video0
exec \
ffmpeg -y -hide_banner -nostdin -nostats \
-vaapi_device /dev/dri/renderD128 \
-f v4l2 -framerate 30 -video_size 960x540 -input_format yuyv422 -i /dev/video0 \
-itsoffset 0.35 \
-f pulse -i default -ac 1 \
-vf 'hqdn3d,format=nv12,hwupload,scale_vaapi' -c:v hevc_vaapi -tag:v hvc1 -b:v 1400K \
-c:a libopus -b:a 128K -metadata:s:a:0 language=deu \
-map 0:v -map 1:a \
-t $(( 7200 - 10 )) \
"$(date --iso=minutes | cut -d + -f 1 | tr -d ':')-hwaccel.mkv"
[Unit]
Description=Record what is going on during Ruhezeiten
[Timer]
OnCalendar=Mon..Sat 5,13,20:01:00
AccuracySec=1m
[Install]
WantedBy=timers.target
[Unit]
Description=Stop recording
Conflicts=record-cam.service
[Service]
Type=oneshot
ExecStart=/usr/bin/true
@mark-kubacki
Copy link
Author

-itsoffset 0.35 and accompanying -map 0:v -map 1:a offset the latency video encode introduces. In other words, audio shall be mixed with a delay to be in-sync with the video.

Once the file has been closed (after 2h or when stop-record-cam.service has been started) another process

  • … generates subtitles and attaches them to the video file. Those subtitles display date and time.
  • … creates a FFMETADATAFILE with tracks. Tracks start whenever there is a jump in loudness, e. g. from my neighbours operating machines or jumping (!!) around at night.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment