Improving cheap DVR camera systems

dvr camera

Did you purchase a cheap DVR camera system off ebay or the likes and want to do more with the videos it’s saving?

You definitely need one that has FTP upload support. If someone breaks into your place after they notice you have cameras they certainly will steal or destroy it. So let’s go ahead and upload those to some ftp site using a cheap VPS with several GB of storage.

Becasue FTP transfers login and passwords in plaintext it’s a good idea to create a new account that doesn’t have access to any important information on your server. I also go a step further and disable list support as well as read support.

Once you upload some videos you’ll probably has a weird directory stricture with you device’s unique id (probably mac address) followed by a date and then a camera number followed by a “rec” directory. Mine looks something like this:
10.64.10.64_001211021221/2016-02-19/08/rec/10.64.10.64_001211021221/

The files getting uploaded here are often h264 encoded however the files are like a stream and contain no header information. This is why they have a small .exe movie player to play them back.

I chose to upload my files to /var/www/backup to keep all my files together and with easy web access. The first thing we need to do is convert those raw h264 files into something useful. For that we’ll use ffmpeg. The convert command for that is:

ffmpeg -y -f h264 -i my-source-vid-file.h264 my-source-vid-file.mkv

Once all of those are converted, I use ffmpeg again to create the thumbnails. This command will skip into the video 2 seconds and take that frame as the thumbnail. If you turned on “pre recording” where it records a couple of seconds before movement, you’re going to want to adjust this accordingly.

ffmpeg -i my-source-vid-file.mkv -ss 00:00:02 -vframes 1 my-source-vid-file.jpg

Last I loop through all my .jpg and create an html file with links to the video. You can hack this to include a player of some port with a popup. Comment back anything cool you’re doing like that if you don’t mind sharing.

Here’s the first script that make this happen. Note I also copy it from my server to my house for long term storage. You may want to modify or remove the bottom lines as you see if, they are deleting the *h264 which is safe to do, but I also delete the *.mkv after 1 day since I keep a copy as my house. You may want to leave the mkv longer than 1 day.
convert.sh:

#!/bin/bash

cd /var/www/backup
count=`find -name '*h264' | wc -l`
i=0;

for x in `find -name '*h264'`; do
 ffmpeg -y -f h264 -i "$x" "$x".mkv && rm "$x" || echo "
=========================
CONVERT FAILED
Not removing $x
=========================
"
i=$(($i + 1))

bc=`echo "scale=1;($i *100 / $count.0)" | bc`

echo " ================= "
echo " =====$bc%======== "
echo " ================= "
done
echo "
==========================
| Creating thumbnails... |
==========================
"
/var/www/backup/createthumbs.sh


echo "
==========================
| Running rsync... |
==========================
"
rsync -avz --exclude '*h264' /var/www/backup/* user@server:/path/
find -name '*h264' -mtime +3 -exec rm {} \;
find -name '*mkv' -mtime +1 -exec rm {} \;
rmdir -p --ignore-fail-on-non-empty 10.64.10.69_001211021b3a/*/*/*

You can see the createthumbs.sh script being called above. That’s the script that creates the .jpg and index.html.
createthumbs.sh:

#!/bin/bash
cd /var/www/backup


for x in `find -name '*.mkv'`; do 
        f=`echo "$x" | sed 's#/#_#g' | sed 's/^\._//'`
        d=`echo $f|cut -d_ -f3`
        if [ ! -d $d ]; then
                mkdir $d
        fi
        if [ ! -f $d/"$f".jpg ]; then
                ffmpeg -i "$x" -ss 00:00:02 -vframes 1 $d/"$f".jpg
        fi
done


for d in 201*; do
        if [ -f $d/index.html ]; then rm $d/index.html; fi
        rmdir $d
        if [ ! -d $d ]; then continue; fi
        cd $d
        echo "<html>" > index.html
        i=-1;
        for x in *.jpg; do
                i=$(($i + 1))
                mod=`expr $i % 6`;
                if [[ $mod == 0 ]]; then
                        echo "<br>" >> index.html
                fi
                echo "<img src='$x' width='15%'>" >> index.html
        done
        echo '</html>' >> index.html
        cd ..
done

Goodluck! Feel free to leave a comment below with feedback and corrections. Also if you found this helpful and would like occasional updates of things happening in this arena subscribe to the newsletter.
If you need additional help with these topics and more, we do offer professional services and more than willing to help you with your needs.

No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *