Skip to content
denelopPublic

About

Video transcoding solution converting live webcam streams to HLS (HTTP Live Streaming) that browsers and players can consume.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

Repository files navigation


potok

Video transcoding solution converting live webcam streams to HLS (HTTP Live Streaming) that browsers and players can consume. It also supports often needed requirements like watermarking and scaling the video streams.

Requirements

  • Go 1.27 or newer (for development only)
  • ffmpeg 9.1 or newer, built with libx264 (the server refuses to start with an older version, git master builds reporting ffmpeg version N-... are accepted)
    • Install latest release for macOS
      brew install ffmpeg
    • Install a static build for Linux (glibc >= 2.28), the GPL variant includes libx264
      wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz
      tar xvf ffmpeg-master-latest-linux64-gpl.tar.xz
      sudo mv ffmpeg-master-latest-linux64-gpl/bin/ff* /usr/local/bin/
    • Check the installed version
      ffmpeg -version

Configuration

All settings and streams live in one YAML file. The server reads the file at the path in the POTOK_CONFIG environment variable, or potok.yml in the current working directory when POTOK_CONFIG is not set. Without a config file the server fails on start. Unknown keys also fail on start, so typos are caught.

Full example:

server:
  port: 80
  tls_port: 443
  domains: [potok.example.com]
  cert_dir: /var/lib/potok/certs
  allowed_origins: [https://www.example.com, https://example.com]

streaming:
  dir: /var/lib/potok/streams
  delete_contents_on_start: true

timelapse:
  dir: /var/lib/potok/timelapse
  keep_frames: 48h
  keep_videos: 9600h
  max_size: 80
  min_free: 10

dvr:
  dir: /var/lib/potok/dvr
  max_size: 25

streams:
  - name: My Camera
    url: rtsp://user:password@camera.example.com:554/stream
    scale: 1920:-2
    watermarks:
      - file: /var/lib/potok/watermark.png
        opacity: 0.3
        position: center
    snapshot: true
    timelapse:
      hours: "07:00-22:00"
Key Description
server.port HTTP port, or the port of the HTTP to HTTPS redirect and ACME challenges when tls_port is set (default 53030)
server.tls_port HTTPS port with automatic Let's Encrypt certificates (optional, HTTPS is off when not set)
server.domains Domains the certificates are requested for
server.cert_dir Directory caching the certificates
server.allowed_origins Origins allowed by CORS (optional, CORS is off when empty)
streaming.dir Directory for the HLS files and snapshots (required)
streaming.delete_contents_on_start Delete each stream directory's contents except snapshot.jpg once on server start (default false)
timelapse.dir Directory for timelapse frames and films (required if any stream has a timelapse)
timelapse.keep_frames How long frame days are kept, Go duration (default 48h)
timelapse.keep_videos How long films are kept, Go duration (default 9600h, 400 days)
timelapse.max_size Maximum total size of all films in GB (default 50)
timelapse.min_free Minimum free disk space in GB on the disk of timelapse.dir (default 10)
dvr.dir Directory for the DVR segments (optional, DVR is off when not set)
dvr.max_size Maximum total size of all DVR segments in GB (default 25)
streams The streams, see below

Defining Streams

Streams are listed under streams in the config file following the format:

streams:
  - name: <Stream Name> (required)
    url: <URL of the IP camera> (required)
    rtsp_transport: <"tcp" or "udp"> (optional, "tcp" is default)
    scale: <width:height> (optional)
    watermarks:
      - file: <path to the image> (required)
        opacity: <0.1 - 1> (required)
        position: <"top-left" or "center" or "bottom-right"> (required)
    snapshot: <true or false> (optional, false is default)
    timelapse: (optional, implies snapshot)
      hours: <HH:MM-HH:MM capture window in Europe/Sarajevo time> (required)

Definitions will be transcoded and the main streaming playlist (video) will each be served over HTTP at:

http://localhost:53030/streaming/<Stream Name>/playlist.m3u8

Snapshot

With snapshot: true the same ffmpeg process also writes a JPEG of the final video (watermarks and scale applied) every 30 seconds. It is served with Cache-Control: no-cache at:

http://localhost:53030/streaming/<Stream Name>/snapshot.jpg

If the camera goes down, the last snapshot stays available.

Timelapse

With timelapse configured, the snapshot is copied once a minute while the current Europe/Sarajevo time is inside hours. Snapshots older than 45 seconds (camera down) are skipped. Frames are stored at timelapse.dir/frames/<Stream Name>/YYYY-MM-DD/HH-MM-SS.jpg.

Once an hour (and on server start) a film is built for every day whose window is over and that has no film yet: 25 fps, 1920 px wide, H.264, written to timelapse.dir/videos/<Stream Name>/YYYY-MM-DD.mp4. Days with fewer than 25 frames get no film. Films are built with nice -n 19, one at a time.

The same hourly job applies retention to everything found on disk (also cameras no longer in the config file):

  • frame days older than timelapse.keep_frames are deleted
  • films older than timelapse.keep_videos are deleted
  • while the films take more than timelapse.max_size or the disk holding timelapse.dir has less than timelapse.min_free free, the oldest film across all cameras is deleted

Films and a JSON array of available dates (ascending, e.g. ["2025-01-14","2025-01-15"]) are served for streams with a timelapse at:

http://localhost:53030/timelapse/<Stream Name>/index.json
http://localhost:53030/timelapse/<Stream Name>/YYYY-MM-DD.mp4

Each day also keeps one snapshot at timelapse.dir/snapshots/<Stream Name>/YYYY-MM-DD.jpg. It is updated once a minute from midnight until the middle of hours (07:00-22:00 stops at 14:30), so it exists all day and ends up with a daylight image. A day that has no snapshot by then (server started after the middle) gets the next fresh one, which then stays. Daily snapshots are never deleted automatically, streaming.delete_contents_on_start and the retention above leave them alone. They are served at:

http://localhost:53030/timelapse/<Stream Name>/YYYY-MM-DD.jpg

DVR

With dvr.dir set, every stream with a timelapse is also recorded during its hours window so viewers can seek back through the day. The recording is one more output of the same ffmpeg process (no second camera connection): the final video (watermarks and scale applied) at 5 fps and 960 px wide, H.264 with a keyframe every 2 seconds, no audio. Streams without a timelapse get no DVR.

Segments of 6 seconds are written to dvr.dir/<Stream Name>/YYYY-MM-DD/HH-MM-SS_<duration in microseconds>.ts, named by their Europe/Sarajevo start time. dvr.dir should not be inside streaming.dir, so streaming.delete_contents_on_start never wipes the recording.

Once a minute (and on server start) retention runs over everything in dvr.dir (also cameras no longer in the config file):

  • days before today are deleted, 2 minutes after midnight
  • segments outside the stream's hours window are deleted (ffmpeg records around the clock, so live is never interrupted)
  • while all segments take more than dvr.max_size, the oldest segment across all cameras is removed from the playlist right away and deleted 2 minutes later

The 2 minute grace keeps players that hold a slightly older playlist from requesting segments that are already gone. The DVR folder can therefore exceed dvr.max_size by about 2 minutes of recording.

The real bitrate depends on the camera image, check the size of a full day to tune dvr.max_size.

The DVR playlist of today is built from the segments on disk on every request and served with Cache-Control: no-cache at:

http://localhost:53030/streaming/<Stream Name>/dvr.m3u8

It is an EVENT playlist with #EXT-X-INDEPENDENT-SEGMENTS (every segment starts on a keyframe), #EXT-X-PROGRAM-DATE-TIME on every segment and #EXT-X-DISCONTINUITY wherever the recording has a gap (camera down, ffmpeg restart). #EXT-X-ENDLIST is added once the window of the day is over. It returns 404 for streams without a DVR and while nothing has been recorded today. The live playlist also carries #EXT-X-PROGRAM-DATE-TIME, so a player can match the live edge with the DVR timeline.

Stats

With the POTOK_STATS_TOKEN environment variable set, a status page and its data are served at:

http://localhost:53030/stats?token=<token>
http://localhost:53030/stats.html?token=<token>
http://localhost:53030/stats.json?token=<token>
http://localhost:53030/stats.yaml?token=<token>
http://localhost:53030/stats.yml?token=<token>

Every route returns 404 when the token is missing or wrong, and when POTOK_STATS_TOKEN is not set. Responses are never cached, and each request takes about half a second because CPU usage is sampled over that window.

The JSON and YAML contain the same keys. Every size is an object with bytes and human (SI units, e.g. {"bytes": 8140000000, "human": "8.1 GB"}):

  • disks: every distinct disk holding streaming.dir, timelapse.dir or dvr.dir, with its mount point as path, the configured dirs on it as dirs (name and path), total, used and free space, and status ok, warning (80% used) or critical (90% used)
  • usage: space taken by streaming.dir, dvr.dir and timelapse.dir (also split into frames, videos and snapshots), null for a dir that is not configured
  • cpu, memory: CPU usage of all cores (0-100), core count, load average and memory of the host
  • ffmpeg: CPU (per core, like top, so it goes above 100) and memory of all ffmpeg processes of the server, including timelapse film builds
  • streams: per stream whether it is live (live playlist rewritten within the last 10 seconds), the age of the live playlist, whether the DVR is recording, the CPU and memory of its ffmpeg, and links (name and url) to everything served for it (live playlist, snapshot, today's daily snapshot, DVR playlist and timelapse index, in that order, depending on its config), and timelapses (date and url) for every finished film, the same dates as its timelapse index (null without a timelapse). URLs are absolute, built from the scheme and host the stats were requested on (e.g. https://potok.example.com/streaming/My%20Camera/playlist.m3u8)

The HTML page shows the same data and refreshes itself every 30 seconds.

stats page

Development

mise installs the ffmpeg version pinned in mise.toml (9.1.0, with libx264 and ffprobe). Run the checks through mise x so the tests use it:

mise i
mise x -- go build ./...
mise x -- go vet ./...
mise x -- go test ./...

The tests in pkg/streaming/ffmpeg_test.go run the real ffmpeg and ffprobe binaries against generated video, so the live stream arguments and the timelapse film build are checked end to end. They are skipped when ffmpeg or ffprobe is not on PATH.

Web Player

Since only Apple products natively support HLS, you're recommended to use HLS.js as an open-source browser player.

Meaning of "potok"

The literal transalation of "stream" in Bosnian/Croatian/Serbian language is "potok" (Google Translate). It is actually a river stream.

About

Video transcoding solution converting live webcam streams to HLS (HTTP Live Streaming) that browsers and players can consume.

Resources

Stars

1 star

Watchers

0 watching

Forks

Used by

Contributors

Languages