Overlay Weather Image on RTSP Stream Using FFmpeg

This article provides a step-by-step guide on how to capture a live RTSP camera feed, overlay a locally updated weather image onto the video in real-time, and output the processed stream using FFmpeg. You will learn the exact FFmpeg commands required to merge these sources, position the overlay, and ensure the image dynamically updates as local weather conditions change.

Prerequisites

To get started, you will need: 1. An RTSP Stream URL from your IP camera (e.g., rtsp://username:password@ip_address:554/stream). 2. FFmpeg installed on your system. 3. A transparent PNG weather image (e.g., weather.png) saved locally. To prevent stream buffering issues, keep the image dimensions reasonably small (e.g., 200x200 pixels).


The FFmpeg Command

To overlay a local image that updates in real-time onto an RTSP stream, run the following command in your terminal:

ffmpeg -rtsp_transport tcp -i "rtsp://username:password@ip_address:554/stream" \
-f image2 -loop 1 -reload 1 -i "weather.png" \
-filter_complex "[0:v][1:v] overlay=W-w-10:10 [out]" \
-map "[out]" \
-map 0:a? \
-c:v libx264 -preset ultrafast -tune zerolatency \
-c:a copy \
-f rtsp "rtsp://localhost:8554/live_output"

Command Breakdown


Positioning the Weather Overlay

You can easily adjust the position of the weather image by changing the overlay coordinates in the -filter_complex flag:

(Note: W/H refer to the main video dimensions, while w/h refer to the overlay image dimensions).


Managing Real-Time Weather Updates

To keep the weather image current, set up a cron job or a Python script in the background that fetches local weather data from an API, generates a new weather.png graphic, and overwrites the existing file. Because of the -reload 1 flag, FFmpeg will automatically display the updated image on the stream within seconds of the file being replaced.