Script to make mp4 video of previous day

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
clipo
Posts: 101
Joined: Sat Sep 06, 2008 9:55 am
Location: Manchester, UK
Contact:

Script to make mp4 video of previous day

Post by clipo »

Hi,

I found a script on these forums some years ago to make a video of the previous hour and leave this in a folder of your choice.

I have taken that script and modified it to make an mp4 video of the previous full day, the scrip works perfect and the video is almost perfect.

The problem is the video will get mixed up with frames from earlier or later in the day and so gets garbled, the script works by querying the sql database for the events required for the period then makes a link to the jpeg files
sorted in the events folder, once it has a large sequential list of jpgs it uses ffmpeg to compress the series into a single mp4 file.

The scrip is as follows, can anyone spot the error that causing my problems and suggest a solution.

Thanks in advance.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

#!/bin/bash
MUSER="zmuser"
MPASS="zmpass"
MHOST="localhost"
MDB="zm"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
PATHTOEVENTS="/var/www/html/zm/events"
MonitorId="$1"
tmpdir="/tmp"

if [ "$1" == "$null" ]
then
echo You Must Specify The Monitor ID i.e \'$0 4\' would specify monitor id 4
exit
fi

EVENTS="$($MYSQL -D$MDB -u$MUSER -p$MPASS -Bse 'select id from Events WHERE (StartTime BETWEEN DATE_SUB(CURDATE( ) ,INTERVAL 1 DAY ) AND CURDATE( )) AND (MonitorId = '$MonitorId') order by id asc')"

echo $EVENTS

mkdir $tmpdir/$MonitorId
x=1
for event in ${EVENTS[@]}
do
if [ $x -eq 1 ]
then
mysqldate="$($MYSQL -D$MDB -u$MUSER -p$MPASS -Bse 'select StartTime from Events WHERE id = '$event'')"
date=`date +%b%d-%Y --date="$mysqldate"`
firstevent="$event"
echo Making Images and Placing them in $tmpdir/$MonitorId .... Please Be Patient this could take a while.


fi

for i in $(ls $PATHTOEVENTS/$MonitorId/$(TZ=EST24EDT date +%y/%m/%d)/.$event/*jpg)
do counter=$(printf %06d $x)
ln -s "$i" $tmpdir/$MonitorId/img"$counter".jpg
x=$(($x+1))
done

done

ffmpeg -f image2 -i $tmpdir/$MonitorId/img%06d.jpg -vcodec libx264 -vpre libx264-normal -threads 0 /var/www/html/Monitor-$1-$(date +%Y-%m-%d).mp4

rm -rf $tmpdir/$MonitorId
Last edited by clipo on Sat Dec 14, 2013 8:05 pm, edited 1 time in total.
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Re: Script to make mp4 video of previous day

Post by whatboy »

I have it like this...

Create a folder like /home/user/bin, inside bin create a file like zmmkmov with

#!/bin/sh
#Next line is where you store the list of files to append to a movie.
rm /home/weatherman/bin/list.txt
#do a simple ls command, but for all files that were created "yesterday" and numerical sort rigth "-v" and place it in a file!
ls -Ca --format=single-column -v /where_the_folder/events/1/$(TZ=EST24EDT date +%y/%m/%d)/.*/*capture.jpg > /home/user/bin/list.txt
#Do your stuff Mr. Mencoder... fps = Frames per second!
/usr/bin/mencoder "mf://@/home/user/bin/list.txt" -mf fps=2 -o /home/user/bin/mon1-$(TZ=EST26EDT date +%F).avi -ovc lavc -lavcopts vcodec=mpeg4
#Move the created video to the Videos folder
mv /home/user/bin/mon1-$(TZ=EST26EDT date +%F).avi /home/user/Videos

Then create a cron job for it, like run it every day at 12:30 or sumtin!
whatboy
Posts: 304
Joined: Mon Aug 31, 2009 10:31 pm

Re: Script to make mp4 video of previous day

Post by whatboy »

But then again, to avoid the Argument too long error... try find instead!

Replace this line...
ls -Ca --format=single-column -v /where_the_folder/events/1/$(TZ=EST24EDT date +%y/%m/%d)/.*/*capture.jpg > /home/user/bin/list.txt

With...
find /where_the_folder/events/1/$(TZ=EST24EDT date +%y/%m/%d)/ -type f -name *capture.jpg | sort > /home/user/bin/list.txt
compdave7681
Posts: 1
Joined: Tue Jul 29, 2014 5:26 am

Re: Script to make mp4 video of previous day

Post by compdave7681 »

I know this is old but I found the same script and used it for a while. I have this one now and it works.

Code: Select all

#!/bin/sh

# Change to suit your needs

###  MUST RUN AS ROOT #######

# If you do not want to delete events after video is made
# Comment out lines 'xargs -I{} sh -c 'rm {}' < $TMPDIR/$TMPFILE' and 'find $EVENTSDIR/$MONID -empty -exec rm -r {} \;'

# Monitor ID - MUST BE NUMERICAL ID OF MONITOR WILL NOT FOLLOW SYMLINK
MONID=3

# TMP DIR - change if you have to
TMPDIR=/home/$USER/tmp

# Events dir (look where you specified ZM to store events eg:/var/cache/zoneminder/events)
EVENTSDIR=/var/cache/zoneminder/events

# Output dir
OUTDIR=/home/$USER/Videos/Events/$MONID/$(TZ=EST26EDT date +%F)

# What user/group we save videos as
USER=my user
GROUP=my user

#### DO NOT MODIFY BELOW #######
# Create Output dir if it doesnt exist
mkdir $OUTDIR

# TMP file name
TMPFILE=$MONID.txt

# DON'T CHANGE 
EVENTS=$EVENTSDIR/$MONID

# Output filename
OUTFILE=$MONID-$(TZ=EST26EDT date +%F).avi

# Dont remove dir just make so we dont delete running jobs
# rm -vfr $TMPDIR
mkdir $TMPDIR

# Find each jpg modified in 24hrs and sort numerically (eg 100 before 10000)
find $EVENTS -type f -name *capture.jpg -mmin -$((60*24)) -printf "%C@ %p\n" | sort -M | awk '{print $2;}' > $TMPDIR/$TMPFILE

# Mencoder time... fps = Frames per second! NOTE my cameras are captured at 15fps so 60 means each minute lasts only ~5 seconds!!!
/usr/bin/mencoder "mf://@$TMPDIR/$TMPFILE" -mf fps=60 -o $OUTDIR/$OUTFILE -ovc lavc -lavcopts vcodec=mpeg4:threads=2

# Clean up jpgs of what we just made so zmaudit.pl will remove events
xargs -I{} sh -c 'rm {}' < $TMPDIR/$TMPFILE

# Find all empty files and dir and remove them (More clean up)
find $EVENTSDIR/$MONID -empty -exec rm -r {} \;

# Change permission of outfile to user
chown -R $USER:$GROUP $OUTDIR
I have used this for a while now and it does work decentily. I created a cronjob to run at 12am, 2am, and 4am for each camera I have. Yes only 3 but it works. Playback in VLC and you can increase/decrease speed (useful if you know around when an event happened). My cams record 24x7 and zmaudit runs every 6 hours to cleanup the previous day events. Also I changed the 'USE_DEEP_STORAGE' to off. BE CAREFUL BECAUSE IT WILL CLEAR ALL CURRENT EVENTS IF YOU SELECT IT!!!!

Hope it helps and any improvments I am open for suggestions.
Post Reply