{"assets":[],"author_link":"author\/philstrahl\/","author_name":"Phil Strahl","cat":"LD #33","categories":["LD #33"],"comments":[],"epoch":1439579340,"event":"LD33","likes":7,"metadata":{"p_key":"82932","p_author":"Phil Strahl","p_authorkey":"0","p_urlkey":"293664","p_title":"Taking Automated Screenshots at Intervals for Multiple Monitors (Windows)","p_cat":"LD #33","p_event":"LD33","p_time":"1439579340","p_likes":"7","p_comments":"0","p_status":"WAYBACK","us_key":null,"us_name":null,"us_username":null,"event_start":"1440115200","event_key":"29","event_name":"LD33"},"source_url":"2015\/08\/14\/taking-automated-screenshots-at-intervals-for-multiple-monitors-windows\/","text":"<p><img alt=\"Taking Automated Screenshots Header\" src=\"http:\/\/blog.pixelprophecy.com\/wp-content\/uploads\/2015\/08\/2015-08-15-multiple-monitors-e1439598576136-1024x436.png\"\/><\/p>\n<p>Ohai there! Since I am planning to record a timelapse of my gamedev for the upcoming LD#33, I thought it would be nice to take a series of screenshots for creating a nice timelapse animation afterwards. I went ahead and did some testing and ran into problems with multi-monitor setups, especially when one monitor has a different pixel density (dpi) than the other(s). This little post is my solution to the problem and provide every Windows user (even on single-screen setups) with a free solution to take screenshots at a set interval.<br\/>\n<span id=\"more-472638\"><\/span><\/p>\n<h2>The Problem<\/h2>\n<p>While I tried different tools for the task at hand (screen recording via BandiCam, automated screenshot taking with boxcutter or cmdcapture, and other freeware), none was perfect or even up to the task at hand. The problem is that I am running a very uncommon setup of monitors and resolutions:<\/p>\n<p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png\"><img alt=\"displays\" class=\"aligncenter size-full wp-image-472641\" sizes=\"(max-width: 774px) 100vw, 774px\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays-300x181.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays-550x331.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png 774w\" width=\"774\"\/><\/a><br\/>\nMonitor 1 is an UHD display with a resolution of 3840\u00d72160px while monitors 2 and 3 are each 1920\u00d71200px. What complicates things further, is that the UI on screen 1 is scaled up to 125% by Windows which many programs have troubles with. Did I mention I am also running Windows 10?<\/p>\n<p>I spent the evening trying different programs and approaches and came up with a solid solution which doesn\u2019t even cost you anything!<\/p>\n<h2>The Solution<\/h2>\n<p>I found the incredibly versatile command-line tool <a href=\"http:\/\/www.nirsoft.net\/utils\/nircmd.html\" target=\"_blank\">NirCmd<\/a> to be perfect for taking a screenshot of my entire desktop \u2014 yet not without tweaking. This is how I got it to work:<\/p>\n<p>When you run <em>nircmd.exe<\/em> from a Windows command line, you can use it to perform a plethora of tasks, not only taking screenshots, but also turning off your monitor, waiting for programs to close, and a lot more. The option I was most interested is <em>savescreenshotfull<\/em> to save the entire screen which I thought would do the trick. Well, not quite: The bottom-part of my big screen was cut off while the screenshot was padded with a lot of black on the right:<\/p>\n<p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/screenshot-comparison.png\"><img alt=\"screenshot-comparison\" class=\"aligncenter size-full wp-image-472657\" sizes=\"(max-width: 1920px) 100vw, 1920px\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/screenshot-comparison.png\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/screenshot-comparison-300x150.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/screenshot-comparison-550x275.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/screenshot-comparison.png 1920w\" width=\"1920\"\/><\/a><\/p>\n<p>The trick is not to rely on the <em>savescreenshotfull<\/em> option for it produces erroneous results, instead use <em>savescreenshot<\/em> which allows you to specify the coordinates and dimensions of the capture rectangle. Getting the right coorddinates was a bit tricky. Let\u2019s refer to my monitor setup again:<\/p>\n<p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png\"><img alt=\"displays\" class=\"aligncenter size-full wp-image-472641\" sizes=\"(max-width: 774px) 100vw, 774px\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays-300x181.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays-550x331.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2015\/08\/displays.png 774w\" width=\"774\"\/><\/a><\/p>\n<p>Starting from 0,0 only would capture my screens 1 and 3, because monitor 1 is set as my \u201cprimary monitor\u201d and this means that Windows\u2019 coordinate system treats it as origin. Instead I had to use the starting coordinates of -1920,0 and \u2014 presto! \u2014 that worked. Calculating the width and height of the rectangle was a matter of basic arithmetic. This was the command that worked for me:<\/p>\n<pre>\r\nnircmd savescreenshot file.png -1920 0 7680 2160\r\n<\/pre>\n<p>Great! But how about taking a screenshot at a given interval? Glad you asked:<\/p>\n<h2>Taking Screenshots at Intervals<\/h2>\n<p>Did I mention that NirCmd has a plethora of options? It not only allows to run one option, no, you can run multiple. The attribute <em>cmdwait<\/em> followed by an amount of milliseconds defers the following command by the given delay. Neat! In order to wait 5 seconds before taking a screenshot, just run<\/p>\n<pre>\r\nnircmd cmdwait 5000 savescreenshot file.png -1920 0 7680 2160\r\n<\/pre>\n<p>The only thing left to do was to run the command over and over until I wanted it to stop. NirCmd even has an option for that (<em>loop<\/em> that is), but you can\u2019t say \u201crun this one in an infinite loop\u201d, so I thought I write a little batch-script for this which also lets you define the filename and saving location of the created screenshots. You know, for convenience sake <img alt=\":)\" class=\"wp-smiley\" src=\"http:\/\/ludumdare.com\/compo\/wp-includes\/images\/smilies\/simple-smile.png\" style=\"height: 1em; max-height: 1em;\"\/><\/p>\n<p>In the same folder where <em>nircmd.exe<\/em> resides, create a new text file and title it something like \u201cTake screenshot at interval.txt\u201d and copy\/paste the following block of code. Don\u2019t worry, I\u2019ll run you through it below.<\/p>\n<pre>\r\n@echo off\r\nrem  Set your custom options in the following four lines:\r\nset       prefix=screenshot_\r\nset     interval=5\r\nset    extension=png\r\nset  destination=G:\\temp\\screenshot\\\r\n\r\nset count=0\r\n:start\r\nrem Counts screenshot up\r\nset \/a count += 1\r\n\r\nrem Adds padding\r\nif %count% LEQ 9999 (set padding=0)\r\nif %count% LEQ 999 (set padding=00)\r\nif %count% LEQ 99 (set padding=000)\r\nif %count% LEQ 9 (set padding=0000)\r\nif %count% GTR 9999 (set padding=)\r\n\r\n:screenshot\r\nset filename=%prefix%%padding%%count%.%extension%\r\n\r\nrem Actual command to take screenshot\r\nnircmd.exe cmdwait %interval%000 savescreenshot \"%destination%%filename%\" -1920 0 7680 2160 \r\necho.%filename% saved\r\n\r\ngoto start\r\n<\/pre>\n<p>It\u2019s pretty straight forward but since I\u2019m in a nice mood, I\u2019ll explain it to those of you who never really worked with batch scripts:<\/p>\n<ul>\n<li><b>@ECHO OFF<\/b> just means not to print every command from the batch file and only the results.<\/li>\n<li>Each line starting with <b>rem<\/b> is an inline comment<\/li>\n<li>With <b>set<\/b> you can define variables and strings. Here, just change the values to your liking. By default, the resulting screenshot will be titled to look like \u201cscreenshot_0123.png\u201d and saved to \u201cG:\\temp\\screenshot\\\u201d, but you can put in whatever you like. The interval is set to 5 which means to wait 5 seconds before executing the screenshot command.\n<\/li>\n<li> A line starting with a colon, <b>:<\/b>, is a label. I use it as jump-points for the goto-command and to structure the code a little.<\/li>\n<li> <b>set \/a count += 1<\/b> adds 1 to the value of the variable \u201ccount\u201d. This is used to number the screenshots sequentially<\/li>\n<li> The <b>IF %count% LEQ 9999 (set padding=0)<\/b> line is a simple IF statement. If the variable \u201ccount\u201d is less-or-equal (that\u2019s the \u201cLEQ\u201d) to 9999, then the variable called \u201cpadding\u201d will be set to \u201c0\u201d, and so on. The reason for this is to always have the serial number in the file name amount to six digits for sorting reasons.<\/li>\n<li>In the <b>:screenshot<\/b> section the final file name is constructed from the prefix, the padding, the current serial number (\u201ccount\u201d) and the file extension.<\/li>\n<li>And finally, I call <b>NirCmd<\/b> with my settings from above. If you don\u2019t have a multi-monitor setup (or one that doesn\u2019t get cut off), you can just write \u201csavescreenshotfull\u201d instead of \u201csavescreenshot\u201d and delete the numbers at the end.<\/li>\n<li><b>%interval%000<\/b> looks odd, doesn\u2019t it? Because the <em>cmdwait<\/em> option expects milliseconds as parameter, I just add three zeroes after the interval-variable and \u201c5\u201d becomes \u201c5000\u201d<\/li>\n<li>Then, the <b>echo.<\/b> command just outputs the current file name in the command window so we know that everything is working like it should.<\/li>\n<li>And last but not least a <b>goto<\/b> command instructs the script to run again from the <em>:start<\/em> label. This means that the script will run until closed.<\/li>\n<\/ul>\n<p>Now simply change the file extension of the text file to .bat and double click it. This will run the script and it begins taking screenshots.<\/p>\n<h3>Great, but how do I close it?<\/h3>\n<p>Just click the X on the window\u2019s top corner or hit Ctrl+C to pause the script.<\/p>\n<h2>Closing Notes<\/h2>\n<p>What\u2019s cool is that this doesn\u2019t cost any money and the memory footprint is very low, nircmd.exe uses around 1.5 megs of RAM on my system while running. But note that depending on how much is going on on your screen, the resulting image-sequence of screenshots can amount to several gigabytes! So make sure that you save them on a drive where you have enough space left.<\/p>\n<p>I hope anybody still is reading because it took me quite some time to type this up! Happy jamming and all the best for the upcoming LD#33!<\/p>\n<p>Tags: <a href=\"http:\/\/ludumdare.com\/compo\/tag\/batch\/\" rel=\"tag\">batch<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/interval\/\" rel=\"tag\">Interval<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/screenshot\/\" rel=\"tag\">screenshot<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/script\/\" rel=\"tag\">script<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/timelapse\/\" rel=\"tag\">timelapse<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/tools\/\" rel=\"tag\">tools<\/a><\/p>","time":"August 14th, 2015 7:09 pm","title":"Taking Automated Screenshots at Intervals for Multiple Monitors (Windows)","title_was_empty":false}