Giter Club home page Giter Club logo

tiny-nvr's Introduction

I've been using several scripts to record RTSP streams with ffmpeg, which have worked well over the years, but lately I thought they would be better served in a Docker container. So, here I will show you how to run an Alpine-based container that captures your camera's live stream, saves the video in segments, and deletes the old ones as time goes on.

First, you'll need to gather the following info from your IP camera:

  • Credentials (username and password)
  • IP address and port
  • RTSP stream URL syntax (for example, some Hikvision cameras follow this syntax rtsp://username:password@address:port//Streaming/Channels/1).

If your camera allows it, I recommend enabling TCP for the live stream. UDP is fine for view-only purposes but not saving, since frames are usually dropped to keep up with the packets.

From your terminal, cd to the directory where you wish to store the recordings and run the following command. (The first parameter is the RTSP URL, which you should format using the info you have gathered from your camera; while the second parameter is used to name the recording folder--the latter is optional.)

docker run \
       -v $(pwd):/usr/data/recordings \
       -e TZ=America/Chicago \
       hpaolini/tiny-nvr \
       rtsp://username:password@address:port//Streaming/Channels/2 \
       my_camera

With the default settings, the container creates a folder in the current directory (in the example the folder is named "my_camera"), saves the stream in 15 minute segments,1 and initiates a daily cron job to delete recordings older than 3 days.

I added the following environment variables for additional customization. (Remember, environment variables are changed using the -e flag.)

ENV Default Description
TZ Europe/Rome timezone data
DIR_NAME_FORCE false use the folder name you pass as parameter during docker run even if it exists, otherwise it generates a new folder name
HOUSEKEEP_ENABLED true cron job to delete old recordings
HOUSEKEEP_DAYS 3 delete files older than this number of days, if HOUSEKEEP_ENABLED is enabled
VIDEO_SEGMENT_TIME 900 seconds of each recording1
VIDEO_FORMAT mp4 save output as MKV or MP4 file

Combine this with Kubernetes or Docker Swarm and you've got a simple NVR with a small footprint. Happy hacking!

Footnotes

  1. I recommend saving streams in segments of 30 minutes or less. If your camera fails, most likely only your latest recording would result in a corrupted file, so you still have access to recordings that are closer to the point of failure. Also, the most recent recordings are synced faster to a backup solution. 2

tiny-nvr's People

Contributors

hpaolini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tiny-nvr's Issues

Multiple directories continually created, no video files

I tried to run this twice, the first time after I came back after a while I had no video files but numerous subdirectories named Camera_cA1Q Camera_6nrf etc. I stopped it, cleared all the folders, and tried again. This time I saw a single video file created that was less than half a MB in size with no actual audio/video and then it started created folders again. I'm sure I'm doing something wrong, just not sure what that might be.

How to record discontinuous rtsp stream?

I am trying to use this docker image with kubernetes. However the pod repeatedly goes into CrashLoopBackOff. I think this is due to the fact that my camera doesn't sends continuous feed (can be configured to, but don't want to). It only sends events video feed through rstp, i.e only when any activity is detected. When there is no activity, there is no feed and the pod errors out after some time leading to restart. The pod however records and saves the file when it is running and if there is any activity at that time.

My assessment may be incorrect. Any clue why the pod keeps restarting continuously. Following is the deployment yaml file.

kind: Deployment
apiVersion: apps/v1
metadata:
  name: rtsp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rtsp
  template:
    metadata:
      labels:
        app: rtsp
    spec:
      containers:
      - name: rtsp
        image: hpaolini/tiny-nvr
        # command: ["/docker-entrypoint.sh"]
        args: ["rtsp://192.168.1.127:554//live0", "My Room"]
        env:
        - name: PGID
          value: "1000"
        - name: PUID
          value: "1000"
        - name: TZ
          value: "America/New_York"
        - name: DIR_NAME_FORCE
          value: "true"
        - name: HOUSEKEEP_DAYS
          value: "30"
        - name: VIDEO_SEGMENT_TIME
          value: "900"
        - name: VIDEO_FORMAT
          value: mkv
        volumeMounts:
        - name: rtsp-volume
          mountPath: /usr/data/recordings
      volumes:
      - name: rtsp-volume
        hostPath:
          path: /mnt/recordings

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.