Automatic Borders for Images & Videos
Automatic Borders for Images & Videos

Automatic Borders for Images & Videos

1. Intro

Here we are again! This time I bring you a little script that will let us add colored borders to any image or video, even multiple ones at a time. We do this with the help of ffmpeg. If you don’t know what ffmpeg is, here’s a quote from its site:

“FFmpeg is the leading multimedia framework, able to decodeencodetranscodemuxdemuxstreamfilter and play pretty much anything that humans and machines have created”

What this basically means is that it’s a cool tool that lets you mess around with video and images. A lot of the software and programs that you use or have used in the past probably rely on ffmpeg for some tasks. For our little script to work. If you think you’ve installed ffmpeg in the past -or some app might have done that for you- feel free to skip to the script part directly.

If you don’t have ffmpeg installed, when running the script you will see this message:

image

If you’re not sure or you know you don’t have ffmpeg installed, I made a little guide for you and it will take about two minutes really:

a) Installing ffmpeg

This essentially consists on downloading a folder, putting it somewhere in our PC and then adding an Environment Variable. And environment variable is a way to tell everything in our PC “Hey, this is where ffmpeg is!”.

Firstly, Download ffmpeg from here:

Either
Either of these two will work. Just unzip it once you’ve downloaded it.

Rename the extracted folder to
Rename the extracted folder to ffmpeg and move it into your C:/Program Files folder.
Go into the
Go into the Bin folder and copy the path to the folder like above

Press the Windows Key and look for “[Env]ironment variables. The “Edit the System Environment Variables” match should pop up. Hit enter or click on it.
Press the Windows Key and look for “[Env]ironment variables. The “Edit the System Environment Variables” match should pop up. Hit enter or click on it.

Click on Edit Environment Variables
Click on Edit Environment Variables

Select “Path” and click on “Edit”. A new dialogue will come up.
Select “Path” and click on “Edit”. A new dialogue will come up.

Here, we can create a
Here, we can create a New environent variable, and Paste the path that we had copied previously.

That’s it. Ffmpeg is installed and set up as an environment variable. Now we can get to the fun part!

2. Creating the script

Now that we have ffmpeg, we just need to create a basic batch script that tells ffmpeg to add a border. We will use variables and a prompt for the user (us) to select the thickness and color. To do this, we make a .txt file and we type the code:

@ECHO OFF

set /p winput=Thickness (px):
set /a width= %winput%*2
set /a xz= %width%/2
set /a yz= %width%/2

set /p "colorpick=Pick a color (HEX) value or press ENTER for White (#FFFFFF): "
if not defined colorpick (
    set "colorpick=#FFFFFF"
)
echo %colorpick%

for %%i in (%*) do if not exist "%%~i.png" (
	ffmpeg -i  "%%~i" -filter_complex "[0]pad = w=%width%+iw : h=%width%+ih: x=%xz% : y=%yz% : color=%colorpick%" "%%~ni_border_%winput%px%~x1"
)

Then we change the .txt extensio to “.bat” and our script is ready to run. You can drag any images and videos onto it.

How tf does this work?

The main takeaway is we use “set XXXXX” to make a variable. We use “set /a = XXXX * 2” to make operations with variables (In this case Im dividing width and height by 2). And we use “set /p” to add a variable that will hold information that the user will type. So when I put

set /p winput=Thickness (px):

What I’m saying is: Make a variable called “winput”, but ask the user to enter a value. This value is now the value of the variable.

We do the same for the color of the border, this time we store it in the “colorpick” variable.

The last paragraph is all the ffmpeg interaction, where we tell it what to do when you select multiple files, where to put the output files, what extension to use, and utilize our width, height, and color variables to dictate the properties of the border we’re adding.

That’s it, that does it. Now you can take your images and videos and drag them onto the bat script. I also made an executable file that you can download, too.