{"id":439,"date":"2016-09-03T20:26:08","date_gmt":"2016-09-03T20:26:08","guid":{"rendered":"http:\/\/james-batchelor.com\/?p=439"},"modified":"2016-09-03T21:44:30","modified_gmt":"2016-09-03T21:44:30","slug":"automated-time-lapse-solution","status":"publish","type":"post","link":"https:\/\/james-batchelor.com\/index.php\/2016\/09\/03\/automated-time-lapse-solution\/","title":{"rendered":"Automated Time-lapse Solution"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>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\u2019 footage, and finish by deleting the temporary images to leave just the days video.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-445\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/image104062-300x188.jpg\" alt=\"image104062\" width=\"300\" height=\"188\" srcset=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/image104062-300x188.jpg 300w, https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/image104062.jpg 640w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p><!--more--><\/p>\n<p>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\u2019s 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 <a href=\"https:\/\/eternallybored.org\/misc\/wget\/\" target=\"_blank\">WGet<\/a> utility is needed to download and save the images locally. Also the script will invoke <a href=\"https:\/\/ffmpeg.org\/download.html\" target=\"_blank\">FFMpeg<\/a> to convert the images to video at the end of the day.<\/p>\n<h3>Setup<\/h3>\n<p>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:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-441\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/files.png\" alt=\"Timelapse File Structure\" width=\"136\" height=\"146\" \/><\/p>\n<p>Here\u2019s the code for capture.bat, I placed this in the Data folder with ffmpeg.exe for neatness:<\/p>\n<blockquote><p>@echo off<br \/>\n::SET DATE TO A VARIABLE<br \/>\nset todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%<br \/>\n::RESET COUNTER TO ZERO<br \/>\nset counter=100000<\/p>\n<p>::CREATE TODAYS FOLDER AND NAVIGATE TO IT<br \/>\necho Creating Folder&#8230;<br \/>\nc:<br \/>\ncd \\cam\\Data\\temp\\<br \/>\nmd %todaydate%<br \/>\ncd %todaydate%<br \/>\n::LOOP START<br \/>\necho Capturing Images&#8230;<br \/>\n:capture<br \/>\n::GET IMAGE FROM WEBCAM CGI<br \/>\nwget -q http:\/\/xxx.xxx.xxx.xxx\/image\/jpeg.cgi<\/p>\n<p>::RENAME FILE USING COUNTER<br \/>\nren jpeg.cgi image%counter%.jpg<\/p>\n<p>::INCREMENT COUNTER BY ONE<br \/>\nset \/a counter=counter+1<\/p>\n<p>::SET TIME TO VARIABLE<br \/>\nset hour=%time:~0,2%<br \/>\nif &#8220;%hour:~0,1%&#8221; == &#8221; &#8221; set hour=0%hour:~1,1%<br \/>\nset min=%time:~3,2%<br \/>\nif &#8220;%min:~0,1%&#8221; == &#8221; &#8221; set min=0%min:~1,1%<br \/>\nset secs=%time:~6,2%<br \/>\nif &#8220;%secs:~0,1%&#8221; == &#8221; &#8221; set secs=0%secs:~1,1%<br \/>\nset thetime=%hour%%min%%secs%<\/p>\n<p>::TIME BEFORE NEXT IMAGE<br \/>\ntimeout \/t 3 &gt;nul<\/p>\n<p>::CHECK ITS NOT END OF DAY<br \/>\nif %thetime% LSS 120600 (<br \/>\ngoto capture<br \/>\n) else (<br \/>\n::START CONVERTING VIDEO<br \/>\necho Processing Timelapse Video&#8230;<br \/>\n&#8220;C:\\cam\\Data\\ffmpeg.exe&#8221; -loglevel quiet -i C:\\cam\\Data\\temp\\%todaydate%\\image1%%05d.jpg -r 28 -c:v libx264 C:\\cam\\Video\\%todaydate%.mp4<br \/>\necho Deleting temp files&#8230;<br \/>\n::TIME DELAY TO ALLOW PROCESS TO CLOSE<br \/>\ntimeout \/t 10 &gt;nul<br \/>\n::CHANGE DIRECTORY OUT OF FOLDER TO BE DELETED THEN DELETE TEMP FILE<br \/>\ncd \\cam\\Data\\temp\\<br \/>\nRD \/S \/Q &#8220;C:\\cam\\Data\\temp\\%todaydate%&#8221;<br \/>\n)<br \/>\necho Process Complete<\/p><\/blockquote>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-442\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/task-300x226.png\" alt=\"Timelapse Scheduled Task\" width=\"300\" height=\"226\" srcset=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/task-300x226.png 300w, https:\/\/james-batchelor.com\/wp-content\/uploads\/2016\/09\/task.png 644w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p>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.<\/p>\n<h3>Notes<\/h3>\n<p>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:<\/p>\n<ul>\n<li>Originally the images named with a timestamp, however FFMpeg requires images to be sequentially numbered to create the slideshow.<\/li>\n<li>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<\/li>\n<li>The leading zeros meant that the machine was reading the number as an octet, so on the 8<sup>th<\/sup> loop the counter reset to 000001, as a workaround a leading 1 was added to the counter.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>While statements don\u2019t really exist in batch files, so goto and If commands were used to create a crude while loop.<\/li>\n<\/ul>\n<p>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\u2019s doing the job just fine.<\/p>\n<h3>Update<\/h3>\n<p>Since creating the program I\u2019ve 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\u2019s the code, again, save as a bat file and double click to run immediately.<\/p>\n<blockquote><p>@echo off<\/p>\n<p>echo Establishing todays date&#8230;<br \/>\n::FIND TODAYS DATE TO ESTABLISH FOLDER NAME<br \/>\nset todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%<\/p>\n<p>::FIND TIME TO CREATE FILENAME VARIABLE<br \/>\nset hour=%time:~0,2%<br \/>\nif &#8220;%hour:~0,1%&#8221; == &#8221; &#8221; set hour=0%hour:~1,1%<br \/>\nset min=%time:~3,2%<br \/>\nif &#8220;%min:~0,1%&#8221; == &#8221; &#8221; set min=0%min:~1,1%<br \/>\nset secs=%time:~6,2%<br \/>\nif &#8220;%secs:~0,1%&#8221; == &#8221; &#8221; set secs=0%secs:~1,1%<br \/>\nset thetime=%hour%%min%%secs%<\/p>\n<p>echo Locating temp files&#8230;<br \/>\n::NAVIGATE TO IMAGE FOLDER<br \/>\nC:<br \/>\ncd \\cam\\Data\\temp\\<br \/>\ncd %todaydate%<\/p>\n<p>echo Creating preview video<br \/>\n::CREATE VIDEO AND ADD PREVIEW IN FILENAME TO AVOID DUPLICATE FILENAMES<br \/>\n&#8220;C:\\cam\\Data\\ffmpeg.exe&#8221; -loglevel quiet -i C:\\cam\\Data\\temp\\%todaydate%\\image1%%05d.jpg -r 28 -c:v libx264 V:\\Timelapse\\Video\\preview-%todaydate%-%thetime%.mp4<\/p>\n<p>echo Complete<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/james-batchelor.com\/index.php\/2016\/09\/03\/automated-time-lapse-solution\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Automated Time-lapse Solution&#8221;<\/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":[154],"tags":[133,157,156,162,158,160,161,155,159],"class_list":["post-439","post","type-post","status-publish","format-standard","hentry","category-code","tag-bat","tag-automated","tag-automatic","tag-batch-file","tag-ffmpeg","tag-ip-cam","tag-ip-camera","tag-timelapse","tag-wget"],"_links":{"self":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/comments?post=439"}],"version-history":[{"count":3,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/439\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/439\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/media?parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/categories?post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/tags?post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}