Shell script for burning events to CD-ROM

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
Baylink
Posts: 338
Joined: Sun Jun 19, 2005 3:19 am

Shell script for burning events to CD-ROM

Post by Baylink »

I'm not entirely complete with this yet, but I'm posting it anyway because the hard 90% is done, and all that's left is the other 90%, with which I could use some help. :-)

Code: Select all

#!/bin/bash
# burnevents -- burn a CD with ZoneMinder event files on it
#
# Written Fri Jun 24 10:30:05 EDT 2005 by jra@microsys.us

# extract the events for a day, by default "yesterday", find the event directories
#  convert the JPEGs to a pair of MPEG's (which might be a bitch, since only certain
#  frames get analyze counterparts), and rename to Name-Timestamp(-analyze).mpg

# zeroth, set magic variables
ZMHOME=/appl/zm1211
MPEGDIR=/appl/zm1211/mpeg
CDRDEV=/dev/hdc
FFMPEG=
ZMPASS=mumble

# first, calculate yesterday
# YESTERDAY=`date -d "-1 day" +%Y-%m-%d`        # "normal": do yesterday
YESTERDAY=`date +%Y-%m-%d`                      # test: do today
STARTTIME="$YESTERDAY 00:00:00"
ENDTIME="$YESTERDAY 23:59:59"


# set -vx

cd $ZMHOME/events

mysql -u zm --password=$ZMPASS -D zm -B \
        -e "SELECT MonitorId,Id,Name FROM Events \
        WHERE StartTime > \"$STARTTIME\" and StartTime <= \"$ENDTIME\" \
        ORDER BY MonitorID,Id" | sed 1d |
while read MONITOR EVENT NAME
do
        pushd $MONITOR || continue      # skip the line without getting confused
                                        # if monitor directory not there

        pushd $EVENT || (popd;continue) # same thing for the event directory

        # Now, we're in the right place.  Grab the timestamp off the first jpeg,
        # then build the mpeg and name it appropriately

        TIMESTAMP=`date -r \`ls | head -1\` -Iminutes`

        echo ffmpeg -i %d-capture.jpg $MPEGDIR/$NAME-$TIMESTAMP.mpg
        ffmpeg $FFMPEG -i %d-capture.jpg $MPEGDIR/$NAME-$TIMESTAMP.mpg

        popd    # event dir
        popd    # monitor dir
done

# now burn them
cd $MPEGDIR
# mkisofs -rJ * | cdrecord -immed $CDRDEV

# and remove them.
mv * archive/
Clearly, I'm not entirely done with it -- I haven't gotten to tuning the burning commands yet because I haven't quite figured out what to tell ffmpeg to get Windows-playable video files that the users will be able to navigate around in. This machine is on-net at the moment, but they don't necessarily need it to be, and the wireless (only practical option) is being cranky.

So, first, any suggestions on $FFMPEG?

Once I get that done, I'll look into how hard doing a companion clip with the analysis frames will be; annoying, I suspect (involving making symlinks for all analyze frames that don't exist)...
Baylink
Posts: 338
Joined: Sun Jun 19, 2005 3:19 am

Post by Baylink »

I seem to reply to myself a lot around here...

After extensive debugging, I seem to have a *very* strange problem with my script, which now looks like this:

Code: Select all

#!/bin/bash
# burnevents -- burn a CD with ZoneMinder event files on it
#
# Written Fri Jun 24 10:30:05 EDT 2005 by jra@microsys.us

# extract the events for a day, by default "yesterday", find the event directories
#  convert the JPEGs to a pair of MPEG's (which might be a bitch, since only certain
#  frames get analyze counterparts), and rename to Name-Timestamp(-analyze).mpg

# zeroth, set magic variables
ZMHOME=/appl/zm1211
MPEGDIR=/appl/recordings/mpeg
CDRDEV=/dev/hdc

# first, calculate yesterday
# YESTERDAY=`date -d "-1 day" +%Y-%m-%d`        # "normal": do yesterday
YESTERDAY=`date +%Y-%m-%d`                      # test: do today
STARTTIME="$YESTERDAY 00:00:00"
ENDTIME="$YESTERDAY 23:59:59"


# set -vx

cd $ZMHOME/events

mysql -u zm --password=zomeminter -D zm \
        -e "SELECT MonitorId,Id,Name FROM Events \
        WHERE StartTime > \"$STARTTIME\" and StartTime <= \"$ENDTIME\" \
        ORDER BY MonitorID,Id" | sed 1d |
while read xMONITOR xEVENT xNAME
do
        # Sign on
        echo "Processing $xMONITOR/$xEVENT/$xNAME" >&2

        # check to make sure the event directory exists, or skip the event
        [ -d "$xMONITOR/$xEVENT" ] || {
                echo "=== Skipping $xMONITOR/$xEVENT -- no directory ===" >&2
                continue
        }
        cd $xMONITOR/$xEVENT

        # Now, we're in the right place.  Grab the timestamp off the first jpeg,
        # then build the mpeg and name it appropriately
        TIMESTAMP="`date -r \`ls | head -1\` -Iminutes`"

        CLIPNAME="${MPEGDIR}/${xNAME}-${TIMESTAMP}.mpg"
        echo "Processing $xMONITOR/$xEVENT/$xNAME" >&2

        ffencode $CLIPNAME
        echo "Processing $xMONITOR/$xEVENT/$xNAME" >&2

        cd ../..
        echo >&2

        sleep 2
done

# now burn them
cd $MPEGDIR
#mkisofs -rJ * | cdrecord -immed $CDRDEV

# and remove them.
#mv * archive/

and ffencode is just

Code: Select all

ffmpeg -vcodec msmpeg4 -acodec mp3 -r 3 -i %05d-capture.jpg $1
and in case anyone's wondering *why* that is in a subscript, it's because the main script's loop breaks if I call ffmpeg from it. Even if I call it from a subscript. If I comment out the call, it counts through the event info returned to the while loop from mysql just perfectly. If I call ffmpeg, then that stream of data gets screwed up, dropping some lines completely, and others partially.

I, and my shell programmer cow-orker, are both flummoxed. This is the Last Bug; anyone got any ideas?

Incidentally, even when the data gets hosed, those second and third "echo Processing" statements both return the same thing.
b.addis
Posts: 2
Joined: Mon Aug 08, 2005 10:00 am
Location: www.spit.gen.nz

useful

Post by b.addis »

Hey

This looks mighty useful, have you managed to find a fix at all?

I don't need the cd writing part, but the rest would be most useful.

Cheers!
nickcol
Posts: 26
Joined: Tue Sep 06, 2005 10:08 am
Location: devon,uk

Post by nickcol »

Is the above script working yet ?.

I have tried the

ffmpeg -vcodec msmpeg4 -acodec mp3 -r 3 -i 001-capture.jpg

line by itself to encode 1 jpg to mpg and just get

001-capture.jpg unknown format.

Nick
slide
Posts: 2
Joined: Wed Dec 27, 2006 2:48 pm

mmh... Something goes wrong

Post by slide »

Why i get this error ?

/usr/lib/zm/html/events/4 /usr/lib/zm/html/events
/usr/lib/zm/html/events/4/7567 /usr/lib/zm/html/events/4 /usr/lib/zm/html/events
ffmpeg -y -r 2.19 -i %04d-capture.jpg -s 480x360 -r 25 /home/executive/VideoRec//Event-7567-2007-04-04T06:00+0200.mpg
ffmpeg version CVS, build 3277056, Copyright (c) 2000-2004 Fabrice Bellard
configuration: --prefix=/usr --libdir=/usr/lib --mandir=/usr/share/man --enable-mp3lame --enable-libogg --enable-vorbis --enable-theora --enable-faad --enable-faac --enable-xvid --enable-a52 --enable-a52bin --enable-pp --enable-shared-pp --enable-shared --enable-pthreads --enable-gpl --disable-opts --disable-strip
built on Feb 17 2006 14:32:43, gcc: 3.4.4 20050721 (Red Hat 3.4.4-2)
Input #0, image2, from '%04d-capture.jpg':
Duration: 00:09:28.9, start: 0.000000, bitrate: N/A
Stream #0.0, 2.19 fps: Video: mjpeg, gray, 320x240
Output #0, mpeg, to '/home/executive/VideoRec//Event-7567-2007-04-04T06:00+0200.mpg':
Stream #0.0, nan fps: Video: mpeg1video, yuv420p, 480x360, q=2-31, 200 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop encoding
frame=14212 q=0.0 Lsize= 14358kB time=568.4 bitrate= 206.9kbits/s
video:291kB audio:0kB global headers:0kB muxing overhead 4840.104900%
/usr/lib/zm/html/events/4 /usr/lib/zm/html/events
/usr/lib/zm/html/events
/home/executive/ZM_video2.sh: line 37: pushd: Event-7689: No such file or directory
/usr/lib/zm/html/events/5 /usr/lib/zm/html/events

there's something wrong but i cant understand what in your script made this

for ffmpeg command i prefer this:

$FFMPEG -y -r 2.19 -i %04d-capture.jpg -s 480x360 -r 25 $MPEGDIR/$NAME-$TIMESTAMP.mpg
gab74
Posts: 21
Joined: Tue May 08, 2007 1:29 pm

Post by gab74 »

Anyone has the above scritp funcional ??
i try the above scripts but it is bugged......
________
VERMONT DISPENSARIES
Last edited by gab74 on Fri Feb 11, 2011 2:01 pm, edited 1 time in total.
Baylink
Posts: 338
Joined: Sun Jun 19, 2005 3:19 am

Post by Baylink »

I never came back to it... but I may shortly, I'm back on this horse again.
Post Reply