I have got an IP camera with RTSP stream. I decided to record the stream using ffmpeg (version 3.2.14-1~deb9u1) on the Odroid-N2 device with Armbian. I have created a .sh script which is launched by Croon every minute. It checks if the ffmpeg recording is active for selected camera and also deletes files older than 7 days:
#!/bin/bash
RecordPathAndFind='/home/mona/CameraRecordings/Camera1/'
SegmentTime=900
MinutesAfterDeleteOldFiles=10080
DaysSecurityLimit=31
tempoutput=$(ps aux | grep ffmpeg | grep $RecordPathAndFind)
if [ ${#tempoutput} -ge 5 ];
then
echo "FFMPEG with record command is already running. Skipping.\n"
else
echo "FFMPEG with record command is not running. Starting now...\n"
FFMPEGSTART=$(su - mona -c "cd /home/mona; /usr/bin/screen -dmS ffmpegcamera1 ffmpeg -rtsp_transport udp -i 'rtsp://admin:password@192.168.1.xxx:554/onvif1' -vcodec copy -c:a aac -map 0 -f segment -segment_time $(echo $SegmentTime) -segment_format mp4 -strftime 1 $(echo $RecordPathAndFind)%Y-%m-%d_%H-%M-%S.mp4")
fi
currenthourminutes=$(date +%M)
if [ "$currenthourminutes" == "00" ]; then
echo "Current Minute is 00. Checking for old files to delete.\n"
FILESDELETECOMMAND=$(find $(echo $RecordPathAndFind) -maxdepth 1 -type f -mmin +$(echo $MinutesAfterDeleteOldFiles) -mtime -$(echo $DaysSecurityLimit) -name '*.mp4' -ls -exec rm {} \;)
else
echo "Current Minute is NOT 00. Skipping.\n"
fi
The script works fine, but I'm afraid of the life of the SD card in this device. I detected that ffmpeg is continuously writing the mp4 file (file size grows all the time). I think it would be better if ffmpeg waited for ~1MiB before flushing it to disk.
I tried different ffmpeg settings (adding "blocksize", "flush_packets", "reorder_queue_size" and few others I can't recall now), unfortunately it didn't change anything. The mp4 file size was increasing all the time (even by few KBs).
The first question is: Should I be worried about the microSD card life when ffmpeg is all the time writing the file (current environment)?
The second question is: Are there any other ffmpeg optimization settings which I missed?
If my fears regarding the SD card life are correct, can you please recommend some other things which I could add to my script (or change in system) in order to increase the life of micro SD cards (or USB sticks) used for recording purposes? I was thinking about using the ramdisk, and then manually move the files, however this could cause files loss in case of system reboot, hang, or power outage.
EDIT: Here is the command I use to check file size changes:
root@odroidn2:/home/mona/CameraRecordings/Camera1# printf "`date +"%m-%d-%y_%H:%S:%N"` `echo "Bytes:"` `stat --printf "%s\n" 2019-07-10_18-01-00.mp4`\n"
07-10-19_18:28:616623697 Bytes: 28443805
07-10-19_18:29:497492966 Bytes: 28453943
07-10-19_18:29:811378969 Bytes: 28458099
07-10-19_18:30:162532642 Bytes: 28724960
07-10-19_18:30:513455929 Bytes: 28730146
07-10-19_18:30:832042221 Bytes: 28734694
07-10-19_18:31:204934593 Bytes: 28739202
07-10-19_18:31:587997659 Bytes: 28744450
07-10-19_18:32:056139192 Bytes: 28750415
07-10-19_18:32:490959812 Bytes: 28755253
07-10-19_18:32:836352316 Bytes: 28757934
07-10-19_18:33:209513158 Bytes: 28762371
07-10-19_18:33:595689070 Bytes: 29021552
07-10-19_18:33:970968755 Bytes: 29026202
07-10-19_18:34:412742761 Bytes: 29031995
07-10-19_18:34:825309867 Bytes: 29037579
07-10-19_18:35:219852935 Bytes: 29043515
07-10-19_18:35:598878409 Bytes: 29046993
07-10-19_18:35:994067048 Bytes: 29051547
07-10-19_18:36:383952621 Bytes: 29055235
07-10-19_18:36:822644535 Bytes: 29331875
07-10-19_18:37:233137175 Bytes: 29336695
07-10-19_18:37:617277956 Bytes: 29343653
07-10-19_18:38:022776087 Bytes: 29348487
07-10-19_18:38:630347795 Bytes: 29357002
07-10-19_18:39:304204108 Bytes: 29364381
As I can see the file size increased by 10138 bytes (10 kilobytes) within 1 second.