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 decode, encode, transcode, mux, demux, stream, filter 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:
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:
Builds - CODEX FFMPEG @ gyan.dev
FFmpeg is a widely-used cross-platform multimedia framework which can process almost all common and many uncommon media formats. It has over 1000 internal components to capture, decode, encode, modify, combine, stream media, and it can make use of dozens of external libraries to provide more capabilities.
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 "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.
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.