Script to make mp4 video of previous day
Posted: Sat Feb 16, 2013 9:30 am
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
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