Skip to content

Live Video Editing and Markup

Koushik Dutta edited this page Feb 4, 2022 · 1 revision

ffmpeg Video Markup Using Scrypted

Build a custom version of ffmpeg:

  • To allow compatibility with HomeKit, make sure that this version supports:

    • libfdk-aac
    • libx264
    • libx265
  • mkdir ~/Applications/ffmpeg-for-scrypted && cd ~/Applications/ffmpeg-for-scrypted

  • sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev libtheora-dev libtool libvorbis-dev pkg-config texinfo zlib1g-dev

  • sudo apt-get install libfdk-aac-dev libmp3lame-dev yasm libx264-dev libx265-dev

  • https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2

  • tar xjvf ffmpeg-snapshot.tar.bz2

  • cd ffmpeg

  • PATH="$HOME/Applications/ffmpeg-for-scrypted/bin:$PATH" PKG_CONFIG_PATH="$HOME/Applications/ffmpeg-for-scrypted/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/Applications/ffmpeg-for-scrypted/ffmpeg_build" --pkg-config-flags="--static" --extra-cflags="-I$HOME/Applications/ffmpeg-for-scrypted/ffmpeg_build/include" --extra-ldflags="-L$HOME/Applications/ffmpeg-for-scrypted/ffmpeg_build/lib" --bindir="$HOME/Applications/ffmpeg-for-scrypted/bin" --enable-shared --enable-libx264 --enable-libx265 --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-nonfree --enable-gpl

  • PATH="$HOME/Applications/ffmpeg-for-scrypted/bin:$PATH" make

  • make install

  • make distclean

  • hash -r

Add the lib directory of this new build to the path:

  • sudo nano /etc/ld.so.conf
  • Add the following line at the bottom: /home/<username>/Applications/ffmpeg-for-scrypted/ffmpeg_build/lib
  • sudo ldconfig

Running Scrypted with this version of ffmpeg...

...in a terminal (for testing):

  • First stop the Scrypted service if it's running: sudo systemctl stop scrypted.service
  • SCRYPTED_FFMPEG_PATH=/home/<username>/Applications/ffmpeg-for-scrypted/bin/ffmpeg /usr/bin/npx -y scrypted serve

...always (editing the systemd service):

  • Edit /etc/systemd/system/scrypted.service and add the "Environment" line so that it looks like the following:
[Unit]
Description=Scrypted service
After=network.target

[Service]
User=<username>
Group=<username>
Type=simple
Environment="SCRYPTED_FFMPEG_PATH=/home/<username>/Applications/ffmpeg-for-scrypted/bin/ffmpeg"
ExecStart=/usr/bin/npx -y scrypted serve
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target
  • Run sudo systemctl daemon-reload
  • Run sudo systemctl start scrypted.service

Setting up Scrypted

  • In your camera of choice, ensure that the HomeKit integration is turned on
  • In the camera settings, choose the HomeKit tab, and check all the boxes for HomeKit transcoding
  • In the "H264 Encoder Arguments" box, enter the following:
-vf drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf:text='%{localtime}':fontsize=72:fontcolor=red@1.0:x=10:y=10 -vcodec libx264 -preset ultrafast -tune zerolatency -b:v 600k
  • The font size ("72") as well as the position on screen ("x=10:y=10") can be editing by changing the command above.
  • Reload the HomeKit plugin

Viewing Additional Logs in the Scrypted UI

  • In the HomeKit plugin settings, click the REPL button
  • Enter localStorage.setItem('SCRYPTED_FFMPEG_NOISY', 'true') to enable verbose ffmpeg output
  • Enter localStorage.removeItem('SCRYPTED_FFMPEG_NOISY', 'true') to disable