Difference between revisions of "Cctv"

From ElphelWiki
Jump to: navigation, search
(Introduction)
(Hardware)
 
Line 17: Line 17:
 
500G of disk.
 
500G of disk.
  
4x Elphel 333.
+
4x Elphel NC353L with 3MPix sensors. (3MPix instead of standard 5MPix sensors was selected after tests as more convenient for CCTV application)
  
 
= Setup =
 
= Setup =

Latest revision as of 16:12, 5 February 2009

Introduction

Here is a couple of script to put online a CCTV environement based on Mencoder/Mplayer.

The idea is to have a cctv server with enough storage grabing rtsp unicast stream with the timestamp incrusted onscreen.

Hardware

Server:

Dell poweredge 1970

1 quad core Intel Xeon

8go of ram and

500G of disk.

4x Elphel NC353L with 3MPix sensors. (3MPix instead of standard 5MPix sensors was selected after tests as more convenient for CCTV application)

Setup

A user cctv created locally with /var/cctv as home.

All videos will be stored in /var/cctv/videos/

The goal is to have a script launched everyday that will start a new file with timestamp as name followd by camera number, stop existing recording and cleanup old videos (due to legal reason no more than 30 days must be kept).

Scripts

The tricky part is to generate osd with the same timestamp as the start of the recording.


OSD generation

This script will generate subtitles in srt format

#!/usr/bin/perl -w
use strict;

my $start=time();
#will generate a day of timestamp
my $end=$start+86400;
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $h=0;
my $m=0;
my $s=0;

my ($sec2,$min2,$hour2,$mday2,$mon2,$year2) = localtime($start);
while ($start<$end)
{
  if ($s <= 59){
    $s++;
  }
  if ($s == 60){
    $s=0;
    $m++;
  }
  if ($m == 60){
    $m=0;
    $h++;
  }
  if ($h==24){
  $h=0;
                                     }
  my ($sec,$min,$hour,$mday,$mon,$year)=($sec2,$min2,$hour2,$mday2,$mon2,$year2);
  $start++;
  ($sec2,$min2,$hour2,$mday2,$mon2,$year2) = localtime($start);

 $year2+=1900;
 printf "%02d:%02d:%02d,001-->%02d:%02d:%02d,1000\n",$h,$m,$s,$h,$m,$s;
 printf  "%02d:%02d:%02d %02d/%s/%02d\n\n",$hour,$min,$sec,$mday,$abbr[$mon],$year;
}

It will generate output like

06:38:57,001-->06:38:57,1000
02:13:42 30/Jan/2009

1.timeline from the begining of video (6 hours and 38 minutes since video started)
2.localtime and date

Starting/Stopping and incrust

#!/bin/ksh

path="/usr/local/bin";
wdir="/var/cctv";
logsd="$wdir/logs"
videosd="$wdir/videos";
subd="$wdir/subs";
rund="$wdir/run";
today=$(date +%y%m%d);

for cam in cam0 cam1 cam2 cam3; do
# Check if recording already running
if [[ -e $rund/$cam.pid ]]; then
  pid=$(cat $rund/$cam.pid)
# Wait until process killed
  while [[ $(ps -p $pid  | grep $pid) != "" ]]
   do
    print "Trying cleannig process $cam"
    kill -15 $pid
    sleep 2
   done
  rm -f $rund/$cam.pid
fi
  # Generate timestamp
  now=$(date +%Y%m%d%H%M%S);
  srt="$now-$cam.srt";
  # Generate subtitles
  /home/cctv/bin/gensubsrt.pl > $subd/$srt;
  # start recording and incrusting timestamp on screen 
  $path/mencoder rtsp://$cam -sub $subd/$srt -o $videosd/$now-$cam.avi \
  -quiet -ovc lavc -vf scale=640:480 < /dev/null > $logsd/$now-$cam 2>&1 &
  # Store the pid to next kill
  print $! > $rund/$cam.pid;
done

Cleanup

#!/usr/bin/perl -w

use strict;
use POSIX;
use Time::Local;

my $days = 30; #how many days to keep
my $max_age = $days*86400;
my @files = glob('/var/cctv/videos/*');
my $now = time();

for my $file (@files){
  if ( $file =~ /\/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})[^\/]+$/ ){
    my ($year,$month,$day,$hour,$min,$sec) = ($1,$2,$3,$4,$5,$6);
    my $file_age = $now-mktime($sec,$min,$hour,$day,$month-1,$year-1900);
    if ( $file_age > $max_age ) {
      print "$file too old deleting...\n";
      unlink $file;
    }
  }
}

--xab 02:18, 30 January 2009 (CST)