A
A
Anton2015-09-12 22:57:50
linux
Anton, 2015-09-12 22:57:50

How to loop video from camera to small files using ffmpeg?

Not good at scripting. Tell me how to implement the functionality of a video recorder on a PC. There is an IP camera. I pull out the RTSP stream using ffmpeg and write it to a file lasting 15 minutes, but the function works only once:

ffmpeg -t 00:15:00 -i "rtsp://192.168.1.10:554/user=admin&password=&channel=1&stream=0.sdp" -vcodec copy -b:v 64k "/home/anton/video/$(date +%Y-%m-%d+%H-%M-%S).mkv"

So, without waiting for an answer and breaking my head on my own, as well as reading a bunch of literature, I nevertheless made a script. It seems that it can be improved, but for now it will do. The value of the free mass threshold is selected experimentally. Waiting for suggestions to improve the script.
ZY: It turned out that find works incorrectly after deleting the old file.
#!/bin/bash
i='2700' #параметр обозначающий порог свободного места на диске в МБ
FREE=$(df -m | grep "/dev/sdb1" | awk '{print $4}')
while [ "$i" -le "$FREE" ] #запись пока есть свободное место на диске
do
  echo "Запись нового файла: " $(date +%Y-%m-%d+%H-%M-%S)".mkv"
  ffmpeg -t 00:05:00 -i "rtsp://192.168.1.10:554/user=admin&password=&channel=1&stream=0.sdp" -vcodec copy -b:v 64k "/media/anton/48D6-E2E1/$(date +%Y-%m-%d+%H-%M-%S).mkv"
  FREE=$(df -m | grep "/dev/sdb1" | awk '{print $4}') #проверим сколько осталось свободного места после записи файла
  if [ "$i" -gt "$FREE" ];
  then
    until [ "$i" -gt "$FREE" ]
    do		
      #ищим самый старый файл, за последние пять дней
      var_d=$(find /media/anton/48D6-E2E1 -type f -name '*.mkv' -mtime 5 -daystart | head -n 1 | rm)
      echo "Удаляем старый файл: " $var_d
      FREE=$(df -m | grep "/dev/sdb1" | awk '{print $4}') #проверим сколько осталось свободного места после удаления
      var_d=""
    done
  fi
done

Answer the question

In order to leave comments, you need to log in

4 answer(s)
3
3do, 2017-03-12
@3do

It turned out a working solution in a couple of lines - maybe someone will come to mind.
1. Run ffmpeg in screen named record.
Running ffmpeg with the -f segment option writes the rtsp stream continuously, splitting it automatically into files with a duration of 1800 seconds ( -segment_time 1800 option), i.e. 30 min. The fa name
-rtsp_transport tcp had to be added because without it packets were lost.
2. Add a line to /etc/crontab
which once a minute checks the given directory for the number of files and leaves only the 96 most recent files, and deletes the rest (96 for half an hour = 2 days).

A
Alejandro Esquire, 2015-09-13
@A1ejandro

Cycles

V
Victor, 2015-09-21
@only-victor

I recommend putting motion. Can only record motion. And it works with IP, usb cameras.

B
BAF285, 2021-09-26
@BAF285

I recommend a script for automatically deleting files so that the disk does not overflow
https://bafista.ru/skript-udaleniya-staryh-fajlov-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question