Web Cam Madness Scripts

I have decided to post my Powershell scripts that extract and upload the images, and build and upload the time lapse video.  These were some of my very first Powershell scripts, so there will probably be a lot of room for improvement.  But here they are.

The first script retrieves the current image from an HTTP server and save it in a folder named with the current date. The images are given sequence numbers.

########################################################
# Create a sequence of images retrieved from a Web server
#
# Author: Seth Cohen
# Date: 02/17/2014
#
# Input: An http server to retrieve the images.
#
# Output: a folder named YYYY-MM-DD containing a set of jpeg
#       images.  The images are named nnnnn-image.jpg where
#       nnnnn is a sequential number.
#       If the folder doesn't exist, it will be created.
#       When the day changes, a new folder will be created
#               if need be.
#
#       Note: This script must be called each time a new image
#       is to be created.  It can be run from the Windows task
#       scheduler periodically.  I currently run it daily
#       repeated every minute.
#
########################################################

# set destination folder the where images will be stored
#       Use the full path to reference it
$folderPath = "f:\Work"

# set the source URL to retrieve
$url = "http://192.168.123.150:8888/out.jpg"

# create folder name to store individual images, based on the date
$folderName = get-date -format yyyy-MM-dd

# get full path to folder using the folder path specified at the top
$fullFolderPath = join-path -path $folderPath -childpath $folderName

# create folder if it doesn't already exists
if (!(Test-Path -path $fullFolderPath))
{
New-Item $fullFolderPath -type directory
}

# create file name using a sequence number.  The sequence number is
#   calculated by counting the number of jpg files in the destination folder
#   and adding one to it.
$count = (get-childitem $fullFolderPath -filter *-image.jpg).count + 1
$fileName = "{0:D5}" -f $count
# $fileName = get-date -format HHmmss
$fileName = "$fileName-image.jpg"

# Build the file name to grab
$sourceFile = join-path -path $fullFolderPath -childpath "out.jpg"

# retrieve the image from web server using HTTP
invoke-webrequest $url -outfile $sourceFile

# move and rename file
$destPath = join-path -path $fullFolderPath -childPath $fileName
move-item $sourceFile $destPath

The next script creates the time lapse video and uploads the file to an FTP site

########################################################
# Create a time lapse movie from a set of images using ffmpeg
#
# Author: Seth Cohen
# Date: 02/17/2014
#
# Input: a folder named YYYY-MM-DD containing a set of jpeg
#       images.  The images are named nnnnn-image.jpg where
#       nnnnn is a sequential number.
#
# Output: a mpeg movie with a file name of YYYY-MM-DD.mpg
#
########################################################
# set full path to the executable to run (ffmpeg)
$executable = "path_to_ffmpeg.exe_goes_here"

# set destination path where images will be stored
$folderPath = "f:\Work"

# build yesterday's folder name
$yesterday = [DateTime]::Now.AddDays(-1)
$foldername = $yesterday.toString("yyyy-MM-dd")

# get full path to folder
$fullFolderPath = join-path -path $folderPath -childpath $folderName

# make sure yesterday's folder exists
if (Test-Path -path $fullFolderPath)
{
        # we have a folder to work with, so run ffmpeg
        $commandLineArgs = "-i $fullFolderPath\%05d-image.jpg $folderPath\$folderName.mpg"
        invoke-expression "$executable $commandLineArgs"

        # after the movie is created we can perform any clean up deemed necessary
        #   like deleting the source directory.  Do this by hand for now or use
        #   another clean up script to prune old folders

        # copy the video to the  weather web site via ftp
        $server = "your_ftp_site_goes_here"
        $user = "your_username_goes_here"
        $password = "your_password_goes_here"

        echo "open $server
                user $user $password
                binary
                lcd $folderPath
                cd public_html/images
                put $folderName.mpg time-lapse.mpg
                dir time-lapse.mpg
                quit" | ftp -n -v
}

Finally, here is the script that grabs the image from the Webcam and uploads it to Weather Underground via FTP

#################################################
# Powershell script to retrieve the current webcam image
# from Yawcam and upload it to Weather Undeground
# The image is at http://localhost:8888/out.jpg
#
# Author: Seth Cohen
# Date: 02/16/2014
#
#################################################

#Import-Module PSFTP

# first retrieve the image from yawcam
invoke-webrequest http://192.168.123.150:8888/out.jpg -outfile c:\temp\wxwebcam.jpg

# if it was successful then ftp it to the remote site
$server = "webcam.wunderground.com"
$file = "wxwebcam.jpg"
$user = "wxunderground_user_name_goes_here"
$password = "wxunderground_password_goes_here"

echo "open $server
user $user $password
binary
lcd c:\temp
put $file image.jpg
dir
quit" | ftp -n -v