Wednesday, September 17, 2014

Video / Automation Fun

The latest work on this project has been focused on automation and the last few posts have been a result of that. The next few days should see two posts each day of two slightly different views. Eventually that should change to two or three posts a week showing a compiled video of the past 7 days of footage. In the long run I would love to include some basic automated analysis: level of activity/change in images, brightness, changes in color, temperature & moisture. So far all of this is being done on a Raspberry Pi.

For the moment the automated steps are:
  1. Take the scan (scanimage)
  2. Modify images as needed (cropping, resizing, timestamping via imagemagick)
  3. Consolidate the original images (tar)
  4. Compile the current day's images into a video (mp4 via avconv)
  5. Combine the current day of video with all previous days of video (avconv)
  6. Upload the resulting video to youtube and make a blog post (googlecl)
So far I've written 7 or 8 short bash scripts that I compile into two separate scripts. If you've stumbled across this, please don't use any of this with any expectation that it's "the right way". Suggestions/advice are very, very welcome : )

The first three steps from above run every 15 minutes
#!/bin/bash
#start a 300 dpi scan, convert it to a jpg and flip it vertically
#start scan
sudo /usr/bin/scanimage -l 0mm -t 0mm -x 215mm -y 297mm --format tiff --resolution 300 | sudo /usr/bin/convert - -flip /var/www/tmp/sc_$(date -d "today" +"%Y%m%dT%H%M%S").jpg 
#put files in /var/www/tmp
cd /var/www/tmp/ 
#loop through jpgs in directory
for file in *.jpg; do 
#save cropped version
convert -crop 1920x1080+310+285 $file zleaf_$file; 
#grab date and time from file name
ndate="${file:3:4}\/${file:7:2}\/${file:9:2}"
ntime="${file:12:2}:${file:14:2}:${file:16:2}"
tdate="${file:3:4}_${file:7:2}_${file:9:2}" 
#set appropriate width
width=$(identify -format %W zleaf_${file})
width=$((${width} * 2 / 10 )) 
#apply time stamp to image
convert                  \
     -background '#0008'    \
     -gravity center        \
     -fill white            \
     -size ${width}x50     \
      caption:"${ndate} ${ntime}"      \
      zleaf_"${file}"              \
     +swap                  \
     -gravity south         \
     -composite             \
        "b${file}"
done 
#cleanup / move files to appropriate directories.
mv b$file /var/www/testcaption
if [ -f /var/www/300/300_$tdate.tar ]
then
tar -rvf /var/www/300/300_$tdate.tar $file
        else
tar -cvf /var/www/300/300_$tdate.tar $file
fi
if [ -f /var/www/300Zoom/300Zoom_$tdate.tar ]
        then
                tar -rvf /var/www/300Zoom/300Zoom_$tdate.tar zleaf_$file
        else
                tar -cvf /var/www/300Zoom/300Zoom_$tdate.tar zleaf_$file
fi
sudo rm *.jpg
The last three steps run a few minutes after midnight
#!/bin/bash
testdir=/var/www/testcaption/
prev=$(date -d "yesterday" +"%Y%m%d")
version="0.1"
echo $version >> $testdir/$prev.txt  
echo "begin rename $(date)" >> $testdir/$prev.txt
x=1; for i in b*.jpg; do counter=$(printf %04d $x); mv "$i" ${prev}-"$counter".jpg; x=$(($x+1)); done
echo "end rename: $(date)" >> $testdir/$prev.txt

echo "begin encode $(date)" >> $testdir/$prev.txt
ffmpeg -r 30 -i ${prev}-%04d.jpg -r 30  -vcodec libx264 -crf 20 -g 15 $prev.mp4
echo "end encode $(date)" >> $testdir/$prev.txt

#twodago=$(date -d "2 days ago" +"%Y%m%d")
ongoing=current
onedago=$(date -d "yesterday" +"%Y%m%d")
echo "begin combine $(date)" >> $testdir/$onedago.txt
avconv -y -i $ongoing.mp4 -f mpegts -c copy -bsf:v h264_mp4toannexb $ongoing.mpeg.ts
avconv -y -i $onedago.mp4 -f mpegts -c copy -bsf:v h264_mp4toannexb $onedago.mpeg.ts
avconv -y -isync -i "concat:"$ongoing".mpeg.ts|"$onedago".mpeg.ts" -c copy $ongoing.mp4
echo "end combine $(date)" >> $testdir/$onedago.txt

echo "begin youtube upload $(date)" >> $testdir/$prev.txt
google youtube post --category Tech current.mp4 &> upload.txt
movie=$(grep -o -P '(?<==).*(?=&)' upload.txt)
google blogger post --title "Video Upload: $prev" --src "<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/$movie\" frameborder=\"0\" allowfullscreen></iframe>"
echo "end youtube upload $(date)" >> $testdir/$prev.txt 
echo "begin cleanup $(date)" >> $testdir/$prev.txt
sudo rm *.jpg
mv ${prev}*.* $testdir/prevdata/
echo "end cleanup $(date)" >> $testdir/prevdata/$prev.txt 
echo "begin notify $(date)" >> $testdir/prevdata/$prev.txt
cat $testdir/prevdata/$prev.txt | mail -s "Video upload "$prev"" joshdont@gmail.com
echo "end notify $(date)" >> $testdir/prevdata/$prev.txt

No comments:

Post a Comment