Motion - Motion Daily Video
-- RodolfoLeibner - 18 Feb 2017

As a Motion software user, I prefer to get a big movie per day rather than a lot of small ones.


Although it is always possible to do a cron job that runs once a day joining all the avi files in one, I propose to do it on the fly.
To do it we write a bash script (the daily.sh) that will be triggered every time a new avi file is saved.

1.- For a single camera

We have only the motion.conf (no thread _n.conf)

The daily.sh script reads like this:

#!/bin/bash
# Appends <newfile.avi> to an unique daily avi file.
# use: daily.sh <newfile.avi> <Y-m-d>

set -e
DIR="/home/rodolfo/motion/movies"
DAILY=$DIR/day-$2.avi

if [ -f $DAILY ]; then
      avimerge -o $DIR/temp.avi -i $DAILY $1
      mv $DIR/temp.avi $DAILY
      rm $1
else
      mv $1 $DAILY
fi

# Optional: log the event
# (the following 2 lines can be commented)
STAMP='[ ] [ ] [ ] ['
echo "$STAMP$(date +'%b %d %H:%M:%S')] $DAILY updated." >> $DIR/motion.log

Your /usr/local/bin dir is a good place to put it. Do not forget to give the file the executable permission.
Of course, you must replace the DIR="/home... line with the path you actually use (according the declared values of target_dir and movie_filename).


Now open your motion.conf , search the line
; on_movie_end value
and replace it with:

on_movie_end daily.sh %f %Y-%m-%d

That's all.

2.- For 2 or more cameras

Although we can have a script for each camera and invoke them from the corresponding thread, is more efficient - and more elegant - make a unique script to be called by the motion.conf.

Suppose that the movies in each camera are directed to different directories, for example <target_dir> / Cam1, <target_dir> / Cam2, etc.
That is, in motion.conf we have something like:

movie_filename Cam%t/%T
....
thread /home/rodolfo/motion/thread1.conf
thread /home/rodolfo/motion/thread2.conf
thread /home/rodolfo/motion/thread3.conf
thread /home/rodolfo/motion/thread4.conf

Then the only thing we have to do is pass the thread number %t to the daily.conf as a parameter:

movie_filename Cam%t/%T
....
on_movie_end daily.sh %t %f %Y-%m-%d
....
thread /home/rodolfo/motion/thread1.conf
thread /home/rodolfo/motion/thread2.conf
thread /home/rodolfo/motion/thread3.conf
thread /home/rodolfo/motion/thread4.conf

And we modify the daily.sh script so that it takes care of everything in this way:

#!/bin/bash
# Appends <newfile.avi> to an unique daily avi file.
# use: daily.sh <newfile.avi> <Y-m-d> <thread>

set -e
DIR="/home/rodolfo/motion"
DAILY=$DIR/Cam$3/day-$2.avi

if [ -f $DAILY ]; then
     avimerge -o $DIR/Cam$3/$2.avi -i $DAILY $1
     mv $DIR/Cam$3/$2.avi $DAILY
     rm $1
else
     mv $1 $DAILY
fi

# Optional: uncomment the following 2 lines to log the event:
# STAMP='[ ] [ ] [ ] ['
# echo "$STAMP$(date +'%b %d %T')] Thread $3: day-$2.avi updated." >> $DIR/motion.log

Again, do not forget to give the executable permission to the file and change the DIR path to your own.

Perhaps the script can be optimized (sugestions are wellcome).
Only I can say is that it works for me (have 2 cams) as it is.

Enjoy it!

Rodolfo
rleibner@gmail.com
Topic revision: r4 - 19 Feb 2017, RodolfoLeibner
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Please do not email Kenneth for support questions (read why). Use the Support Requests page or join the Mailing List.
This website only use harmless session cookies. See Cookie Policy for details. By using this website you accept the use of these cookies.