How to automatically create thumbnails for your videos with Node.js

The best way to create thumbnails for your videos on Instagram

Just last week I posted about how to create upload reels to Instagram using Node.js, and in the code it was possible to upload a cover image for the video too, but I didn’t explain how to create this cover image.

I mean, of course you can go to Photopea and create a cover image for your video, but what if you want to automate this process? What if you want to create a cover image for all your videos automatically?

Well, that’s what I’m going to show you today.

Finding the right package

As a frontend developer, the only thing I know about image manipulation is by using canvas, and luckly there’s a Node.js library that allows us to do that, it’s called node-canvas.

With it, we can create a canvas, draw images, text, shapes, etc. It’s pretty cool, check out the example below:

https://gist.github.com/blopa/c301401485a8b8f7a998b81b06b4817d

Coming up with a design

I’m not a designer, so I’m not going to teach you how to create a good design, but I can show you how I came up with the design for my thumbnails.

I started by creating a new file on Photopea with the same size as the Instagram Reels video, which is 1080x1920px. Then I added a background image and a text with the title of the video, and also a background color for the text with some opacity.

And for the cherry on top, I added a foreground image with a transparent background with an image of myself in pixel art style.

Reels thumbnail
Reels thumbnail

Creating the thumbnail

Now that we have the design, we can start coding. First, we need to create a function that will create the thumbnail for us, and it will receive a background image, a foreground image, a title and the path to save the thumbnail as parameters.

https://gist.github.com/blopa/26318bf1a6d932f9c6fa12a97f97f8e8

This code will load the background and foreground images, create a canvas with the same size as the background image, draw the foreground image on the canvas, and save the canvas as a PNG file. That was the easy part, now we need to add the text.

The challenge here is to add the text in the center of the canvas, and make sure it won’t overflow the canvas. Let’s take a look how we can do that.

Adding the text

First, we need to create a function that will add the text in the center of the canvas, and it will receive the text and the canvas as parameters. We’re going to do that in the addCenteredTextWithBackground function.

https://gist.github.com/blopa/fa7ba3982e19a103aaedb423d2d7b384

This function will calculate the size of the rectangle that will be the background of the text, and then it will draw the rectangle and the text on the canvas. Unfortunately, just doing text.split(' '); won’t do the job, because splitting the text by spaces will simply make the text’s height bigger, but it won’t necessarely make the text fit in the canvas.

So let’s create a function called breakTextToFitCanvas that will receive the text, a canvas context and a maximum width as parameters, and it will return an array of strings with the text broken into lines that will fit in the canvas.

https://gist.github.com/blopa/7fd2a0ecbbeb5dbfc7435d93b8c63c4d

Putting it all together

Now that we have all the functions we need, we can put it all together and create the thumbnail. Let’s create a function called generateThumbnail that will receive the title of the video and the path to save the thumbnail as parameters.

https://gist.github.com/blopa/8193ef02eaf4fb63315b8ab68431c030

And now we can call this function and create the thumbnail.

https://gist.github.com/blopa/9d6ea78707771ea159d695470fce4abf

Conclusion

And that’s it! Now you can create thumbnails for your videos automatically. I hope you enjoyed this post, and don’t forget to check how to upload Instagram Reels using Node.js.

See you next time!