DVR Video from Images Captured from IP Cameras

DVR Video from Images Captured from IP Cameras

Working with thousands of image files captured from IP Cameras can be problematic. I’ve experienced issues with Caja hanging when simply browsing the folders that contain the captured images. To work around this, I compile video with FFMpeg from the image stock into a separate folder.

First I’ll create a file list using this command

ls /home/local/Videos/Security/IPCam3_20180726*.jpg > /home/local/Scripts/IPCam3_20180726.txt

The results will then need to be processed so FFMpeg can use the file as a reference.

Before – Not ready for use as a reference

/home/local/Videos/Security/IPCam3_20180726235704.jpg
/home/local/Videos/Security/IPCam3_20180726235710.jpg

After – Now FFMpeg can reference this

file '/home/local/Videos/Security/IPCam3_20180726235716.jpg'
file '/home/local/Videos/Security/IPCam3_20180726235722.jpg'

Here are the commands I use to fix the reference file.

sed "s/\/home\/local\/Videos\/Security\/IPCam3/file \'\/home\/local\/Videos\/Security\/IPCam3/" /home/local/Scripts/IPCam3_20180726.txt > /home/local/Scripts/IPCam3_20180726_Pre.txt
sed "s/jpg/jpg\'/" /home/local/Scripts/IPCam3_20180726_Pre.txt > /home/local/Scripts/IPCam3_20180726_Process.txt
rm /home/local/Scripts/IPCam3_20180726.txt
rm /home/local/Scripts/IPCam3_20180726_Pre.txt

Now with the reference file formatted correctly, I’ll issue my FFMpeg command to compile the images into a video

ffmpeg -y -r 30 -f concat -safe 0 -i "/home/local/Scripts/IPCam3_20180726_Process.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "/home/local/Scripts/IPCam3_20180726.mp4"

I’m greeted with this error

[concat @ 0x1ed93e0] Impossible to open '/home/local/Videos/Security/IPCam3_20180726050442.jpg'
/home/local/Scripts/IPCam3_20180726_Process.txt: Invalid data found when processing input

It turns out that some of my image files are empty due to the IP camera being offline. So to fix this, I’ll run this command to remove empty image files.

find /home/local/Videos/Security/ -size 0 -delete

Then I repeat my commands from earlier

ls /home/local/Videos/Security/IPCam3_20180726*.jpg > /home/local/Scripts/IPCam3_20180726.txt
sed "s/\/home\/local\/Videos\/Security\/IPCam3/file \'\/home\/local\/Videos\/Security\/IPCam3/" /home/local/Scripts/IPCam3_20180726.txt > /home/local/Scripts/IPCam3_20180726_Pre.txt
sed "s/jpg/jpg\'/" /home/local/Scripts/IPCam3_20180726_Pre.txt > /home/local/Scripts/IPCam3_20180726_Process.txt
rm /home/local/Scripts/IPCam3_20180726.txt
rm /home/local/Scripts/IPCam3_20180726_Pre.txt
ffmpeg -y -r 30 -f concat -safe 0 -i "/home/local/Scripts/IPCam3_20180726_Process.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "/home/local/Scripts/IPCam3_20180726.mp4"

It starts to churn through all the images, creating the video. The processing took 5 minutes to create the 5 minute video.

Now I’ll run the commands for IP Camera 2

ls /home/local/Videos/Security/IPCam2_20180726*.jpg > /home/local/Scripts/IPCam2_20180726.txt
sed "s/\/home\/local\/Videos\/Security\/IPCam2/file \'\/home\/local\/Videos\/Security\/IPCam2/" /home/local/Scripts/IPCam2_20180726.txt > /home/local/Scripts/IPCam2_20180726_Pre.txt
sed "s/jpg/jpg\'/" /home/local/Scripts/IPCam2_20180726_Pre.txt > /home/local/Scripts/IPCam2_20180726_Process.txt
rm /home/local/Scripts/IPCam2_20180726.txt
rm /home/local/Scripts/IPCam2_20180726_Pre.txt
ffmpeg -y -r 30 -f concat -safe 0 -i "/home/local/Scripts/IPCam2_20180726_Process.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "/home/local/Scripts/IPCam2_20180726.mp4"

…and IP Camera 1

ls /home/local/Videos/Security/IPCam1_20180726*.jpg > /home/local/Scripts/IPCam1_20180726.txt
sed "s/\/home\/local\/Videos\/Security\/IPCam1/file \'\/home\/local\/Videos\/Security\/IPCam1/" /home/local/Scripts/IPCam1_20180726.txt > /home/local/Scripts/IPCam1_20180726_Pre.txt
sed "s/jpg/jpg\'/" /home/local/Scripts/IPCam1_20180726_Pre.txt > /home/local/Scripts/IPCam1_20180726_Process.txt
rm /home/local/Scripts/IPCam1_20180726.txt
rm /home/local/Scripts/IPCam1_20180726_Pre.txt
ffmpeg -y -r 30 -f concat -safe 0 -i "/home/local/Scripts/IPCam1_20180726_Process.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "/home/local/Scripts/IPCam1_20180726.mp4"

Now I can purge the image files, since they are now compiled into a video. This command should do perform the house cleaning function.

rm /home/local/Videos/Security/IPCam1_20180726*.jpg
rm /home/local/Videos/Security/IPCam2_20180726*.jpg
rm /home/local/Videos/Security/IPCam3_20180726*.jpg

This can be setup so I can enter in when and what camera I’d like to process. Here is the code I used to handle all of the functions.

#!/bin/bash
# cloudacm.com
# a shell script used to process images from ip cameras.
# this is executed manually and will prompt for when and what to process.


echo What camera would you like to process?
echo IPCam1 = 1
echo IPCam2 = 2
echo IPCam3 = 3

read cNum

echo What period of time would you like to process?
echo YYYYMMDD

read dNum

find /home/local/Videos/Security/ -size 0 -delete
ls /home/local/Videos/Security/IPCam"$cNum"_"$dNum"*.jpg > /home/local/Scripts/IPCam"$cNum"_"$dNum".txt
sed "s/\/home\/local\/Videos\/Security\/IPCam"$cNum"/file \'\/home\/local\/Videos\/Security\/IPCam"$cNum"/" /home/local/Scripts/IPCam"$cNum"_"$dNum".txt > /home/local/Scripts/IPCam"$cNum"_"$dNum"_Pre.txt
sed "s/jpg/jpg\'/" /home/local/Scripts/IPCam"$cNum"_"$dNum"_Pre.txt > /home/local/Scripts/IPCam"$cNum"_"$dNum"_Process.txt
rm /home/local/Scripts/IPCam"$cNum"_"$dNum".txt
rm /home/local/Scripts/IPCam"$cNum"_"$dNum"_Pre.txt
ffmpeg -y -r 30 -f concat -safe 0 -i "/home/local/Scripts/IPCam"$cNum"_"$dNum"_Process.txt" -c:v libx264 -vf "fps=30,format=yuv420p" "/home/local/Scripts/IPCam"$cNum"_"$dNum".mp4"
rm /home/local/Videos/Security/IPCam"$cNum"_"$dNum"*.jpg
rm /home/local/Scripts/IPCam"$cNum"_"$dNum"_Process.txt

The process can be fully automated. However, the resulting videos have a higher percentage of non activity in them. Reviewing these videos would require more time than is worth the effort. I’ll cover that in another post. I hope you have enjoyed this topic and look forward to having you back.

Comments are closed.