Page 1 of 2

Shell Script: creating one video file per day

Posted: Sat Nov 07, 2009 3:55 pm
by trendkill
Hi guys,
I found a php script here: http://www.zoneminder.com/forums/viewtopic.php?t=13868
But I can't run it, so I decided to make a simple bash script without mysql connection.
This is my first complex script, I hope it works fine! Tested in Ubuntu Karmic and ZM 1.24.2

Code: Select all

#!/bin/bash

# Use this script for creating one video-file per day for ZoneMinder 
# created by TrendKill 

# Main Configuration 
MOVIEDATE="2009-11-07" # Use the same format as what you see in 'ls -all --time-style=long-iso' 
MONITOR="1"
FPS="10"

# Paths
ZM_EVENTS_DIR="/usr/share/zoneminder/events/" 
TEMP_DIR="/tmp" 
VIDEO_OUTPUT_DIR="/var/www" 
FFMPEG="/usr/bin/ffmpeg"

# Check directories and files 
if [ -d $ZM_EVENTS_DIR ] 
    then 
	echo "Found ZM Events Directory: $ZM_EVENTS_DIR -> OK!" 
    else
	echo "ZM Events Directory not found, exiting!" 
	exit 1
fi
if [ -d $VIDEO_OUTPUT_DIR ] 
    then 
	echo "Found Video Output Directory: $VIDEO_OUTPUT_DIR -> OK!" 
    else 
	echo "Video Output Directory not found, exiting!" 
	exit 1
fi 
if [ -d $ZM_EVENTS_DIR/$MONITOR ] 
    then 
	echo "Found Monitor: $MONITOR -> OK!" 
    else 
	echo "Monitor $MONITOR not found, exiting!" 
	exit 1 
fi 
ISDATEAVAILABLE=`ls $ZM_EVENTS_DIR/$MONITOR/ -Al --time-style=long-iso |grep -l $MOVIEDATE` 
if [ -z "$ISDATEAVAILABLE" ] 
    then 
	echo "The requested day - $MOVIEDATE - not found, exiting!" 
	exit 1 
    else 
	echo "Found requested day: $MOVIEDATE -> OK!" 
fi 
if [ -f $FFMPEG ]
    then 
	echo "Found FFmpeg binary: $FFMPEG -> OK!" 
    else 
	echo "FFmpeg not found, exiting!" 
	exit 1 
fi 
if [ -f $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list ] 
    then
	echo "Purging previous temorary file: $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list" 
	rm $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list
fi
if [ -d $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR ] 
    then
	echo "Purging previous temporary symblinks from $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR"
		rm $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR/*
    else 
	mkdir -p $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR
fi 

# Collecting wanted event files 
echo -n "Processing wanted events..."
/bin/ls $ZM_EVENTS_DIR/$MONITOR -Al --time-style=long-iso |grep $MOVIEDATE|awk '{ print $8 }'|sort -n > $TEMP_DIR/events.$MOVIEDATE--Monitor-$MONITOR.list

while read EVENT 
    do 
    /bin/ls $ZM_EVENTS_DIR/$MONITOR/$EVENT/*capture.jpg >> $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list 
done <$TEMP_DIR/events.$MOVIEDATE--Monitor-$MONITOR.list 

EVENTNUMBER=1 
while read EVENTFILE 
    do 
    EVENTNUMBERPADDING=`seq -f %09.f $EVENTNUMBER $EVENTNUMBER`
    /bin/ln -s $EVENTFILE $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR/$EVENTNUMBERPADDING.jpg
    let "EVENTNUMBER = $EVENTNUMBER + 1" 
done <$TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list 
echo "...$EVENTNUMBER events found" 

# Building a video
echo -n "Building video: $VIDEO_OUTPUT_DIR/$MOVIEDATE--Monitor-$MONITOR.avi"
$FFMPEG -y -f image2 -i $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR/%09d.jpg -r $FPS -b 800k $VIDEO_OUTPUT_DIR/$MOVIEDATE--Monitor-$MONITOR.avi 2> /$TEMP_DIR/ffmpeg-output-$MOVIEDATE--Monitor-$MONITOR.log
echo " -> FINISHED!"

# Removing temp files 
echo "Removing temporary files, bye!" 
rm -rf $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR
rm $TEMP_DIR/eventsfiles.$MOVIEDATE--Monitor-$MONITOR.list 
rm $TEMP_DIR/events.$MOVIEDATE--Monitor-$MONITOR.list 
exit 0


Posted: Mon Nov 09, 2009 11:46 am
by arielf
thanks,

I'll try and see how I worked.

Posted: Mon Nov 09, 2009 1:58 pm
by trendkill
Okey, please let me know if anything wrong.
arielf wrote:thanks,

I'll try and see how I worked.

Posted: Mon Nov 09, 2009 6:35 pm
by arielf
I'm trying and do not understand what the date, you must manually enter the day you want to generate or automatically generated videos day by day?

Posted: Mon Nov 09, 2009 10:52 pm
by trendkill
Yes, you have to edit the file:
# Main Configuration
MOVIEDATE="2009-12-31" # Use the same format as what you see in 'ls -all'
MONITOR="1"
FPS="10"

MOVIEDATE="YEAR-MONTH-DAY" in my date format
but try to 'ls -all' anywhere, see the date format in the output and use that.
The script is using the ls command and grep the date.

arielf wrote:I'm trying and do not understand what the date, you must manually enter the day you want to generate or automatically generated videos day by day?

Posted: Tue Nov 10, 2009 11:07 am
by arielf
sorry, but there are two things I do not understand
I have to run the script from day to day, to generate the videos, or generated without changing the date the vídeos

I show you the output of my ls -all

-rw-r--r-- 1 apache apache 14637 nov 9 03:20 001-capture.jpg
-rw-r--r-- 1 apache apache 14702 nov 9 03:20 002-capture.jpg
-rw-r--r-- 1 apache apache 14832 nov 9 03:20 003-capture.jpg
-rw-r--r-- 1 apache apache 14889 nov 9 03:20 004-capture.jpg
-rw-r--r-- 1 apache apache 14890 nov 9 03:20 005-capture.jpg

so should I use?
MOVIEDATE="11-05-2009"

thanks,

Posted: Thu Nov 12, 2009 1:50 pm
by trendkill
Yes, there was a bug :)
I edited the script, copy/paste again

First of all, you can't run this script from crontab, you always have to edit the file in Main Configuration, and set the the day, what you want to see.
my format is YEAR-MONTH-DATE in ls.
try to ls -all --time-style=long-iso
and see the date format output, and use that.


arielf wrote:sorry, but there are two things I do not understand
I have to run the script from day to day, to generate the videos, or generated without changing the date the vídeos

I show you the output of my ls -all

-rw-r--r-- 1 apache apache 14637 nov 9 03:20 001-capture.jpg
-rw-r--r-- 1 apache apache 14702 nov 9 03:20 002-capture.jpg
-rw-r--r-- 1 apache apache 14832 nov 9 03:20 003-capture.jpg
-rw-r--r-- 1 apache apache 14889 nov 9 03:20 004-capture.jpg
-rw-r--r-- 1 apache apache 14890 nov 9 03:20 005-capture.jpg

so should I use?
MOVIEDATE="11-05-2009"

thanks,

Posted: Thu Nov 12, 2009 2:19 pm
by arielf
thanks, another question: it generates the videos from the events or imagesnes in a video generated from the generated video?

Posted: Thu Nov 12, 2009 6:24 pm
by trendkill
It generates video from events files. It collects needed *capture.jpg files, creates a temporary symbolic links, and sort it (000000001.jpg or 000005728.jpg). And after ffmpeg creates one video.
arielf wrote:thanks, another question: it generates the videos from the events or imagesnes in a video generated from the generated video?

Posted: Thu Nov 12, 2009 7:33 pm
by arielf
ooopppss not work

my ls

-rw-r--r-- 1 apache apache 15657 2009-11-07 20:19 022-capture.jpg
-rw-r--r-- 1 apache apache 15737 2009-11-07 20:19 023-capture.jpg
-rw-r--r-- 1 apache apache 15645 2009-11-07 20:19 024-capture.jpg
-rw-r--r-- 1 apache apache 15732 2009-11-07 20:19 025-capture.jpg
-rw-r--r-- 1 apache apache 15896 2009-11-07 20:19 026-capture.jpg
-rw-r--r-- 1 apache apache 15947 2009-11-07 20:19 027-capture.jpg
-rw-r--r-- 1 apache apache 15828 2009-11-07 20:19 028-capture.jpg


the script

ZM_EVENTS_DIR="/var/www/html/zm/events/"
TEMP_DIR="/tmp"
VIDEO_OUTPUT_DIR="/var/ZM-Videos"
FFMPEG="/usr/bin/ffmpeg"

error output script
The requested day - 2009-11-07 - not found, exiting!

Posted: Sat Nov 14, 2009 5:20 pm
by trendkill
I edited the post again, try now. I hope it will works... let me know
the normal output is:


root@dxn-dvr:~# /etc/scripts/zmmovie.sh
Found ZM Events Directory: /usr/share/zoneminder/events/ -> OK!
Found Video Output Directory: /var/www -> OK!
Found Monitor: 1 -> OK!
Found requested day: 2009-11-07 -> OK!
Found FFmpeg binary: /usr/bin/ffmpeg -> OK!
Processing wanted events......5553 events found
Building video: /var/www/2009-11-07--Monitor-1.avi -> FINISHED!
Removing temporary files, bye!

arielf wrote:ooopppss not work

my ls

-rw-r--r-- 1 apache apache 15657 2009-11-07 20:19 022-capture.jpg
-rw-r--r-- 1 apache apache 15737 2009-11-07 20:19 023-capture.jpg
-rw-r--r-- 1 apache apache 15645 2009-11-07 20:19 024-capture.jpg
-rw-r--r-- 1 apache apache 15732 2009-11-07 20:19 025-capture.jpg
-rw-r--r-- 1 apache apache 15896 2009-11-07 20:19 026-capture.jpg
-rw-r--r-- 1 apache apache 15947 2009-11-07 20:19 027-capture.jpg
-rw-r--r-- 1 apache apache 15828 2009-11-07 20:19 028-capture.jpg


the script

ZM_EVENTS_DIR="/var/www/html/zm/events/"
TEMP_DIR="/tmp"
VIDEO_OUTPUT_DIR="/var/ZM-Videos"
FFMPEG="/usr/bin/ffmpeg"

error output script
The requested day - 2009-11-07 - not found, exiting!

Posted: Mon Nov 16, 2009 11:59 am
by arielf
now if it works perfect, thanks,

would be fine, it can be adapted to a html file to access from a browser and create the videos from there

Posted: Tue Jan 12, 2010 2:30 pm
by Flav
Hello.
Good job
i have juste changed the first line for automation

Code: Select all

#!/bin/bash

# Use this script for creating one video-file per day for ZoneMinder
# created by TrendKill

# Main Configuration
MOVIEDATE="$(date +%Y-%m-%d)" # Use the same format as what you see in 'ls -all --time-style=long-iso'
MONITOR="1"
FPS="10"
like this you can ad a line in the cron tab

Code: Select all

30 23 * * * * root  /root/moviescript1 

help -- error

Posted: Mon Feb 08, 2010 10:56 pm
by arielf
Found ZM Events Directory: /var/www/html/zm/events/ -> OK!
Found Video Output Directory: /root/videos/ -> OK!
Found Monitor: 6 -> OK!
Found requested day: 2010-02-08 -> OK!
Found FFmpeg binary: /usr/local/bin/ffmpeg -> OK!
Purging previous temporary symblinks from /tmp/eventsfiles.2010-02-08--Monitor-6
rm: no se puede borrar «/tmp/eventsfiles.2010-02-08--Monitor-6/*»: No existe el fichero o el directorio
Processing wanted events...genera.sh: line 69: syntax error near unexpected token `newline'
genera.sh: line 69: `/bin/ls $ZM_EVENTS_DIR/$MONITOR -Al --time-style=long-iso |grep $MOVIEDATE|awk '{ print $8 }'|sort -n > '

Posted: Tue Feb 23, 2010 12:14 am
by whatboy
tryied your script, didn't work... here's what I did to get a full day of video...


First I installed MPlayer, because it will install mencoder, mencoder is the program I used to make the video file... then create a list of all events from a single date...

like... this will generate the list from /events/camera1/date -1 day/

Code: Select all

ls -Ca --format=single-column /events/1/$(TZ=EST24EDT date +%y/%m/%d)/.*/*capture.jpg > list.txt
Change the exact location of events folder, I move it to /events on another HDD using symlink...

Then run mencoder with the list... this will create a file Year-Month-Day - 1 day.avi

Code: Select all

mencoder "mf://@list.txt" -mf fps=5 -o $(TZ=EST26EDT date +%F).avi -ovc lavc -lavcopts vcodec=mpeg4