Automatically create a thumbnail-preview of your live-stream every X minutes using FFmpeg

2 February 2021

Automatically create a thumbnail-preview of your live-stream every X minutes using FFmpeg

When you're showing your stream on a website, you may also want to use a thumbnail of the video-stream that is being streamed to the server at that very moment. We can accomplish this by instructing FFmpeg to create a new thumbnail every xx minutes. In this example, I show how to create a new thumbnail every minute. It's possible to create a new file for each thumbnail, but I choose to overwrite the one file every minute, because I want to refer to the thumbnail image from the website so it's easier to use a static name for this. In your nginx.conf file you will have to add this line to the application block that you want to grab.

sudo mkdir /var/livestream/thumbs
sudo chown -R www-data:www-data /var/livestream/thumbs

sudo nano /etc/nginx/nginx.conf

exec ffmpeg -i rtmp://localhost/$app/$name -vf fps=1/60 -s 480x270 -an -f image2 -y -vframes 1 /var/livestream/thumbs/$name_tmb.jpg;

This example will create a 480x270 thumbnail for every incoming stream on any application once every minute, and store a thumbnail file named after the stream key that was used, in /var/livestream/thumbs/

exec ffmpeg -i rtmp://localhost/$app/$name -vf fps=1/600 -s 480x270 -an -f image2 -y -vframes 1 /var/livestream/thumbs/$name_tmb.jpg;

This does the same as the first example, except it creates a new thumbnail every 10 minutes instead.

To make it accessible on the website, we'll add a virtual folder mapping to this folder in our virtual host file for our domain.

sudo nano /etc/nginx/sites-available/yourdomain.com.conf

location /thumbs {
	alias /var/livestream/thumbs;	# Edit this if needed #
	autoindex on;
	expires -1;
	add_header Strict-Transport-Security "max-age=63072000";
	add_header Cache-Control no-cache;
	add_header 'Access-Control-Allow-Origin' '*' always;
	add_header 'Access-Control-Expose-Headers' 'Content-Length';
	if ($request_method = 'OPTIONS') {
	add_header 'Access-Control-Allow-Origin' '*';
	add_header 'Access-Control-Max-Age' 1728000;
	add_header 'Content-Type' 'text/plain charset=UTF-8';
	add_header 'Content-Length' 0;
	return 204;
	}
}

It should look something like this in your nginx.conf file:

sudo nginx -t
sudo systemctl restart nginx

When you start streaming now, a thumbnail should appear after a while in the folder that can be reached at https://youdomain.com/thumbs/, and a new thumbnail file will overwrite the current one every 10 minutes. Add this thumbnail image to your website, and it will always show the most recent thumbnail when the page is loaded.

<img src="https://yourdomain.com/thumbs/stream_tmb.jpg" alt="thumbnail preview">
Pick a star to rate this
Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

Unrelated Content

26 June 2025
Why Self-Hosting Your Livestream Is Safer, Cheaper, and More Flexible Than Commercial Platforms

With livestreaming more popular than ever, platforms like YouTube Live, Twitch, Vimeo, and Facebook Live have become household names. But are they really the best option for everyone? If you're a tech-savvy creator, a privacy-conscious broadcaster, or someone who just wants full control, self-hosting your own livestream server might be a better choice. In this […]

READ MORE
27 November 2020
Motivation behind this website

For every service that is offered for a lot of money on the internet, there's always a free or much cheaper alternative, especially if you embrace open-source software. For live-streaming this is also very much the case. We love that,

READ MORE
23 January 2021
Introduction to the new how-to article in the Knowledge Base.

I wrote a new guide on setting up your own live-stream server. Mostly due to your requests. I took the opportunity to simplify and shorten the guide a bit. If you follow this guide step by step, and use your brain a tiny bit, you'll be fine. This guide has so far been tested on […]

READ MORE
25 January 2021
Optimizing First-Frame Bitrate for HLS

When responding to an HLS request, the streaming server determines which video quality (i.e., ts file) the client will attempt to play before switching to a lower or higher quality video. This switch depends on available bandwidth and device type. Switching to the wrong quality first degrades the user experience considerably. For instance, sending a […]

READ MORE
25 January 2021
Adaptive Bit-rate Streaming

“What is adaptive streaming?” This question often leads to answers filled with technical terms like “HLS”, “MPEG-DASH”, “segments” and “codec“. This article is written for people who need to understand the principles, but don’t need to actually implement the technology. If you are looking for specific technical information on adaptive streaming then I recommend you read up on it on this website and elsewhere.

READ MORE
2 February 2021
About Bradmax Video Player WordPress plugin

Let's talk about our favorite video player plugin for WordPress. Here are some reasons why Bradmax Player is our favorite. And below that are detailed instructions on how to set it up. Setting it up may at first seem a bit awkward, but it really isn't. First you must obviously install the Bradmax Player plugin […]

READ MORE
Copyright © 2025. 
All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram