{"id":457,"date":"2014-02-22T10:14:55","date_gmt":"2014-02-22T15:14:55","guid":{"rendered":"http:\/\/www.cloppermillweather.org\/blog\/?p=457"},"modified":"2014-02-22T10:15:42","modified_gmt":"2014-02-22T15:15:42","slug":"web-cam-madness-scripts","status":"publish","type":"post","link":"https:\/\/www.cloppermillweather.org\/blog\/2014\/02\/22\/web-cam-madness-scripts\/","title":{"rendered":"Web Cam Madness Scripts"},"content":{"rendered":"<p>I have decided to post my Powershell scripts that extract and upload the images, and build and upload the time lapse video.\u00a0 These were some of my very first Powershell scripts, so there will probably be a lot of room for improvement.\u00a0 But here they are.<\/p>\n<p>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.<\/p>\n<pre>\r\n########################################################\r\n# Create a sequence of images retrieved from a Web server\r\n#\r\n# Author: Seth Cohen\r\n# Date: 02\/17\/2014\r\n#\r\n# Input: An http server to retrieve the images.\r\n#\r\n# Output: a folder named YYYY-MM-DD containing a set of jpeg\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 images.\u00a0 The images are named nnnnn-image.jpg where\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nnnnn is a sequential number.\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If the folder doesn't exist, it will be created.\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 When the day changes, a new folder will be created\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if need be.\r\n#\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Note: This script must be called each time a new image\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is to be created.\u00a0 It can be run from the Windows task\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 scheduler periodically.\u00a0 I currently run it daily\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 repeated every minute.\r\n#\r\n########################################################\r\n\r\n# set destination folder the where images will be stored\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Use the full path to reference it\r\n$folderPath = \"f:\\Work\"\r\n\r\n# set the source URL to retrieve\r\n$url = \"http:\/\/192.168.123.150:8888\/out.jpg\"\r\n\r\n# create folder name to store individual images, based on the date\r\n$folderName = get-date -format yyyy-MM-dd\r\n\r\n# get full path to folder using the folder path specified at the top\r\n$fullFolderPath = join-path -path $folderPath -childpath $folderName\r\n\r\n# create folder if it doesn't already exists\r\nif (!(Test-Path -path $fullFolderPath))\r\n{\r\nNew-Item $fullFolderPath -type directory\r\n}\r\n\r\n# create file name using a sequence number.\u00a0 The sequence number is\r\n#\u00a0\u00a0 calculated by counting the number of jpg files in the destination folder\r\n#\u00a0\u00a0 and adding one to it.\r\n$count = (get-childitem $fullFolderPath -filter *-image.jpg).count + 1\r\n$fileName = \"{0:D5}\" -f $count\r\n# $fileName = get-date -format HHmmss\r\n$fileName = \"$fileName-image.jpg\"\r\n\r\n# Build the file name to grab\r\n$sourceFile = join-path -path $fullFolderPath -childpath \"out.jpg\"\r\n\r\n# retrieve the image from web server using HTTP\r\ninvoke-webrequest $url -outfile $sourceFile\r\n\r\n# move and rename file\r\n$destPath = join-path -path $fullFolderPath -childPath $fileName\r\nmove-item $sourceFile $destPath\r\n<\/pre>\n<p>The next script creates the time lapse video and uploads the file to an FTP site<\/p>\n<pre>\r\n########################################################\r\n# Create a time lapse movie from a set of images using ffmpeg\r\n#\r\n# Author: Seth Cohen\r\n# Date: 02\/17\/2014\r\n#\r\n# Input: a folder named YYYY-MM-DD containing a set of jpeg\r\n#       images.  The images are named nnnnn-image.jpg where\r\n#       nnnnn is a sequential number.\r\n#\r\n# Output: a mpeg movie with a file name of YYYY-MM-DD.mpg\r\n#\r\n########################################################\r\n# set full path to the executable to run (ffmpeg)\r\n$executable = \"path_to_ffmpeg.exe_goes_here\"\r\n\r\n# set destination path where images will be stored\r\n$folderPath = \"f:\\Work\"\r\n\r\n# build yesterday's folder name\r\n$yesterday = [DateTime]::Now.AddDays(-1)\r\n$foldername = $yesterday.toString(\"yyyy-MM-dd\")\r\n\r\n# get full path to folder\r\n$fullFolderPath = join-path -path $folderPath -childpath $folderName\r\n\r\n# make sure yesterday's folder exists\r\nif (Test-Path -path $fullFolderPath)\r\n{\r\n        # we have a folder to work with, so run ffmpeg\r\n        $commandLineArgs = \"-i $fullFolderPath\\%05d-image.jpg $folderPath\\$folderName.mpg\"\r\n        invoke-expression \"$executable $commandLineArgs\"\r\n\r\n        # after the movie is created we can perform any clean up deemed necessary\r\n        #   like deleting the source directory.  Do this by hand for now or use\r\n        #   another clean up script to prune old folders\r\n\r\n        # copy the video to the  weather web site via ftp\r\n        $server = \"your_ftp_site_goes_here\"\r\n        $user = \"your_username_goes_here\"\r\n        $password = \"your_password_goes_here\"\r\n\r\n        echo \"open $server\r\n                user $user $password\r\n                binary\r\n                lcd $folderPath\r\n                cd public_html\/images\r\n                put $folderName.mpg time-lapse.mpg\r\n                dir time-lapse.mpg\r\n                quit\" | ftp -n -v\r\n}\r\n<\/pre>\n<p>Finally, here is the script that grabs the image from the Webcam and uploads it to Weather Underground via FTP<\/p>\n<pre>\r\n#################################################\r\n# Powershell script to retrieve the current webcam image\r\n# from Yawcam and upload it to Weather Undeground\r\n# The image is at http:\/\/localhost:8888\/out.jpg\r\n#\r\n# Author: Seth Cohen\r\n# Date: 02\/16\/2014\r\n#\r\n#################################################\r\n\r\n#Import-Module PSFTP\r\n\r\n# first retrieve the image from yawcam\r\ninvoke-webrequest http:\/\/192.168.123.150:8888\/out.jpg -outfile c:\\temp\\wxwebcam.jpg\r\n\r\n# if it was successful then ftp it to the remote site\r\n$server = \"webcam.wunderground.com\"\r\n$file = \"wxwebcam.jpg\"\r\n$user = \"wxunderground_user_name_goes_here\"\r\n$password = \"wxunderground_password_goes_here\"\r\n\r\necho \"open $server\r\nuser $user $password\r\nbinary\r\nlcd c:\\temp\r\nput $file image.jpg\r\ndir\r\nquit\" | ftp -n -v\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have decided to post my Powershell scripts that extract and upload the images, and build and upload the time lapse video.\u00a0 These were some of my very first Powershell scripts, so there will probably be a lot of room &hellip; <a href=\"https:\/\/www.cloppermillweather.org\/blog\/2014\/02\/22\/web-cam-madness-scripts\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-457","post","type-post","status-publish","format-standard","hentry","category-weather"],"_links":{"self":[{"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/posts\/457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/comments?post=457"}],"version-history":[{"count":0,"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/posts\/457\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/media?parent=457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/categories?post=457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloppermillweather.org\/blog\/wp-json\/wp\/v2\/tags?post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}