×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Bash
Posted by: Massimo Zappino
Added: Nov 22, 2011 7:43 AM
Modified: Feb 6, 2015 6:17 PM
Views: 23
This script allows you to archive video recordings made by motion. It will delete all images (.jpeg) and keep avi files into an archive directory.

Assume you have this directory structure
-- /home/motion
---- archive (your archive directory)
------ cam1
------ cam2
-- cam1 (images and videos from cam1)
-- cam2 (images and videos from cam2)

Example: if you run this script at 00.30 AM, it will manage all files before today, images will be deleted and videos will be archived in your archive/camX directory
  1. #!/bin/bash
  2. cams="cam1 cam2 cam3"
  3. formats="avi jpg"
  4.  
  5. function move_files {
  6.         cam=$1
  7.         format=$2
  8.         input_dir=/external/motion/$cam
  9.         output_dir=/external/motion/archive/$cam/$format
  10.  
  11.         for fullfilepath in $input_dir/*.$format;
  12.         do
  13.  
  14.  
  15.         # check if is regular file
  16.         if [ ! -f "$fullfilepath" ]; then
  17.                 continue
  18.         fi
  19.  
  20.         file=$(basename "$fullfilepath")
  21.         date_string=`stat -c %y% "$fullfilepath" | cut -d ' ' -f1`
  22.         year=`echo "$date_string" | cut -d '-' -f1`
  23.         month=`echo "$date_string" | cut -d '-' -f2`
  24.         day=`echo "$date_string" | cut -d '-' -f3`
  25.         dir_path="${year}/${month}/${day}";
  26.         today=`date +"%Y/%m/%d"`
  27.         if [ $dir_path = $today ]; then
  28.                 #echo $file today skipped
  29.                 continue
  30.         fi
  31.         full_output_dir="$output_dir/$dir_path"
  32.  
  33.         # Remove old jpg
  34.         if [ $format_name = "jpg" ]; then
  35.                 echo "Deleting " $input_dir/$file
  36.                 rm $input_dir/"$file"
  37.                 continue
  38.          fi
  39.  
  40.  
  41.         if [ ! -d $full_output_dir ]; then
  42.                 mkdir -p "$full_output_dir"
  43.                 chown -R motion.motion $output_dir
  44.         fi
  45.         echo "Moving $input_dir/$file $full_output_dir/$file"
  46.  
  47.         mv "$input_dir/$file" "$full_output_dir/$file"
  48.         done;
  49. }
  50.  
  51. for cam_name in $cams;
  52. do
  53.         for format_name in $formats;
  54.         do
  55.                 move_files $cam_name $format_name
  56.         done
  57. done
  58.