.bat – James Batchelor https://james-batchelor.com Useful I.T & VoIP Ramblings Sat, 03 Sep 2016 21:44:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 https://james-batchelor.com/wp-content/uploads/2025/05/cropped-cropped-logo-jb-202505-32x32.png .bat – James Batchelor https://james-batchelor.com 32 32 Automated Time-lapse Solution https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/ https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/#comments Sat, 03 Sep 2016 20:26:08 +0000 http://james-batchelor.com/?p=439 Continue reading "Automated Time-lapse Solution"]]> A while ago I worked on a simple CCTV system for work, which involved using software to capture images every second then batch convert them to video every five minutes. It was crude but did the job.

I also dabbled in the past with time lapse videos, however this was a more manual process with images capturing to a folder, then personally loading them into Windows Movie Maker to create the video. With the tedium of creating the videos, the software I used for capturing (YAWCam) would hang after a few weeks constant running, not to mention without upkeep, the hundreds of thousands of image files populating the hard drive.

As a project it was time to combine the learnings from these and create an automated time lapse video creator, a program that would capture images, then create a 5-minute video that contains the days’ footage, and finish by deleting the temporary images to leave just the days video.

image104062

As with the CCTV project, a batch file will be used to create the process as what I need can be achieved by DOS commands plus it’s able to be called by Windows Task Scheduler. This time I will have the advantage of using an IP camera instead of USB, so negating the need for software to capture images, however the WGet utility is needed to download and save the images locally. Also the script will invoke FFMpeg to convert the images to video at the end of the day.

Setup

Getting started, place the WGet utility in the System32 folder so it can be called from dos without specifying a path. Then create this folder hierarchy on the root of C:\ drive:

Timelapse File Structure

Here’s the code for capture.bat, I placed this in the Data folder with ffmpeg.exe for neatness:

@echo off
::SET DATE TO A VARIABLE
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%
::RESET COUNTER TO ZERO
set counter=100000

::CREATE TODAYS FOLDER AND NAVIGATE TO IT
echo Creating Folder…
c:
cd \cam\Data\temp\
md %todaydate%
cd %todaydate%
::LOOP START
echo Capturing Images…
:capture
::GET IMAGE FROM WEBCAM CGI
wget -q http://xxx.xxx.xxx.xxx/image/jpeg.cgi

::RENAME FILE USING COUNTER
ren jpeg.cgi image%counter%.jpg

::INCREMENT COUNTER BY ONE
set /a counter=counter+1

::SET TIME TO VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

::TIME BEFORE NEXT IMAGE
timeout /t 3 >nul

::CHECK ITS NOT END OF DAY
if %thetime% LSS 120600 (
goto capture
) else (
::START CONVERTING VIDEO
echo Processing Timelapse Video…
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 C:\cam\Video\%todaydate%.mp4
echo Deleting temp files…
::TIME DELAY TO ALLOW PROCESS TO CLOSE
timeout /t 10 >nul
::CHANGE DIRECTORY OUT OF FOLDER TO BE DELETED THEN DELETE TEMP FILE
cd \cam\Data\temp\
RD /S /Q “C:\cam\Data\temp\%todaydate%”
)
echo Process Complete

Next is to create a Scheduled Task in Windows to launch the program each day at midnight, this process varies by system but ensure that user privileges are high enough to write and execute on disk.

Timelapse Scheduled Task

I found Windows Server 2012 R2 had a lot more options over Server 2003, so I set the task to run when even when not logged on, to run hidden, and now the time can be specified to the second, so chose to start task at 00:00:03 to make sure the date is correct.

Notes

Hopefully the annotation will be helpful enough if you would like to apply your own tweaks, but here are some of notes about the issues I faced when creating the script:

  • Originally the images named with a timestamp, however FFMpeg requires images to be sequentially numbered to create the slideshow.
  • The counter was set at 000000, but on the first loop the arithmetic to increase the counter changed the value to 1, so lines were added to include the leading zeros
  • The leading zeros meant that the machine was reading the number as an octet, so on the 8th loop the counter reset to 000001, as a workaround a leading 1 was added to the counter.
  • Capturing images every 10 seconds equates to 8640 images a day, the counter it set to five decimals to ensure no looping of counter numbers.
  • In addition to storing the date as a variable to allow multiple calls, it allows the program to keep the same date after time moves into the next day. Since the video conversion starts at 23:59 each day its very likely the process will move into the next day when temp files are deleted.
  • Initially the time variables were inside the IF statement, but learned that the variable with an IF statement are created when the program is parsed, not run, therefore would not update.
  • However, the time needs to be in the loop to update before the IF statement is reached, the date is purposely kept out to it retains the value set at the start of the script.
  • While statements don’t really exist in batch files, so goto and If commands were used to create a crude while loop.

I dare not say that the code is perfect, or if the choice of programming language is suitable, the aspiration would be to create a self contained executable with a configuration file to specify target folders, change finish time and time between image captures. But for now it’s doing the job just fine.

Update

Since creating the program I’ve reused parts of the code to create a preview program that compiles the images taken so far that day into a video, without deleting any files. Here’s the code, again, save as a bat file and double click to run immediately.

@echo off

echo Establishing todays date…
::FIND TODAYS DATE TO ESTABLISH FOLDER NAME
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%

::FIND TIME TO CREATE FILENAME VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

echo Locating temp files…
::NAVIGATE TO IMAGE FOLDER
C:
cd \cam\Data\temp\
cd %todaydate%

echo Creating preview video
::CREATE VIDEO AND ADD PREVIEW IN FILENAME TO AVOID DUPLICATE FILENAMES
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 V:\Timelapse\Video\preview-%todaydate%-%thetime%.mp4

echo Complete

]]>
https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/feed/ 1
2 Servers 1 UPS, Windows 2012 Edition https://james-batchelor.com/index.php/2016/05/29/2-servers-1-ups-windows-2012-edition/ Sun, 29 May 2016 22:47:53 +0000 http://james-batchelor.com/?p=404 Continue reading "2 Servers 1 UPS, Windows 2012 Edition"]]> In a previous post I showed how to shutdown two servers safely using just one UPS with a single communications port. It was pretty straight forward with the comms port connected to a Windows Server 2003 machine.

But doing the same with Windows Server 2012 is much more difficult, since Microsoft decided to remove the ability to run a program on a low battery event from its power management settings. To make things worse I discovered that a bug in Server 2008 and later meant that issuing a Shutdown command from the native power settings would not perform a clean shut down, instead killing the power in a few seconds. This is not good news for RAID arrays and data integrity.

Time for a new solution, and since Microsoft are of no use, help would need to come from a 3rd party. After research and testing answer came from Shutter, a small program that runs as a trigger and event type program for a variety of different scenarios, with battery discharging status being one. Luckily two instances of the program could be run, one to shut down the remote servers and another for the host machine. Importantly the program can also be run as a Windows service, but more on this in the walk through.  here is how it is done:

Download the portable version of Shutter from den4b.com.

In the Windows directory, create a new folder, here it will be named ups.

In that folder create another 2 folders, one will handle the local server shutdown and the other for remote servers.

Copy the contents of the downloaded Shutter zip to both of the folders.

Before opening the program for the first time, create a .bat that will issue commands to shut down servers. Open Notepad and enter the following text:

shutdown /s /f /t 10 /c “Battery Backup Low”

This is for shutting down the host server. To save, select the folder to be used for host shutdown, in the file type menu select All files, and create a file name with it ending in .bat

Do the same for remote server shutdown using this script, and save to the remote folder:

shutdown /s /f /m \\SERVER /t 10 /c “Battery Backup Low”

Replace SERVER with the name or IP address of the other server running on the UPS.

Now go to the local shutdown folder and open Shutter.

In the events box, add battery and choose a trigger percentage and add. In the action box, choose run a program, and locate the .bat file you created for shutting down the host server.

Shutter

Important: Go to the settings window and copy the following options:

Shutter Options GeneralShutter Settings Advanced

Save settings and the program can now be closed. Repeat these steps to shut down the remote server, it’s essential to set the battery percentage trigger higher for remote server as they will not get the signal if the host server has already shut down, I recommend at least 5% higher as a safety net especially if the UPS battery drains quickly.

As a power outage is an unpredictable event, Shutter needs to be running at all times on the host server, therefore it needs to be ran as a system service. Microsoft did release a tool that could allow any program to run as a service, however it is very basic and will not restart a program if it happened to crash. Lucky a superior utility by the name of NSSM is available that takes this into account, it also has a more user friendly interface to set up services.

Download the latest release of NSSM and place in the main folder created previously, as it needs to be run alongside the Shutter instances.

From a Command Prompt, navigate to the folder holding the program, by using cd, then a space and the full path to the folder, e.g: cd C:\Windows\ups

Type nssm install and enter, and the new service setup screen will appear.cmd nssm

Choose the Shutter program to run, and create a name to the process, additional details can be made in the Description tab.

nssm install

Move to the login tab and select Log on as, then use an account capable of administrator access and password details.

Click create service and its installed, repeat the process for the other shutter program, giving it a different service name.

By default, these newly created services will start when Windows starts, but need to be started manually when first installed. To start them, the simple option is to restart the machine, but much better would be to press the Windows key & R, then type services.msc and click Run. Scroll down the list to the created services, right click on each and choose start.

Testing

It’s worth testing to see if it works rather than just hoping it does. And to save the wait for the batteries to discharge its worth raising the trigger values on Shutter to shut down the machines earlier, this also gives the benefit more charge left if the shutdown does not work.

Before opening Shutter to change values, the services need to be stopped. Open services.msc like before and chose stop against the service.

Make the changes in the action box, its recommended to choose somewhere between 70 and 90 percent. Close the programs and start the services back up. Then testing is as simple as unplugging the UPS from the wall, for security the host machine can be left logged on to monitor the remaining battery percentage so you know when the trigger points are happening.

]]>