DEV Community

Cover image for How to Delete Background from Videos and Add a New Background via CLI
Gokula Krishna
Gokula Krishna

Posted on Originally published at gokulakrishna.co on

How to Delete Background from Videos and Add a New Background via CLI

Recently, I was looking for tools to delete the background from a video I needed for a presentation. Unfortunately, the quality of the free online tools I tried wasn’t up to par. After some exploration, I found some open-source alternatives that delivered much better results. In this post, I’ll walk you through the process I followed and what I learned along the way.

I’ll introduce an open-source library called Rembg and demonstrate how to delete the background from a single image. Next, I’ll explain how to extract frames from a video and apply Rembg for background removal. Finally, I’ll show you how to add a new background to those images and combine them back into a video.

Prerequisites

Before we start, let’s make sure we have the following tools installed:

  • Python : For running AI models.
  • FFmpeg : A powerful tool for video processing. We will use it to extract video frames and combine images back to video.
  • ImageMagick : Tool for manipulating images. We will use it to overlay the foreground on a new background.
  • Rembg : An AI-powered tool to remove image backgrounds.

Ubuntu installation:

# Ubuntu procedure
sudo apt update

# Install Python
sudo apt install python3

# Install FFmpeg
sudo apt-get install ffmpeg

# Install rembg Python library
pip install rembg[gpu,cli]

# Install ImageMagick
sudo apt-get install imagemagick
Enter fullscreen mode Exit fullscreen mode

Mac installation:

# Mac procedure
brew install python3
brew install ffmpeg
pip install rembg[gpu,cli]
brew install imagemagick
Enter fullscreen mode Exit fullscreen mode

Step 1: Background Removal from a Single Image

Let’s start with removing the background from a single image using Rembg:

rembg i input.png output.png
Enter fullscreen mode Exit fullscreen mode

Single image background removal

Background removal result

You can also choose different AI models. I prefer the isnet-anime model for anime-style images:

rembg i -m isnet-anime anime.png output.png
Enter fullscreen mode Exit fullscreen mode

Step 2: Extracting Frames from Video

To process a video, we first need to extract individual frames using FFmpeg:

mkdir frames
ffmpeg -i input_video.mp4 frames/frame_%04d.png
Enter fullscreen mode Exit fullscreen mode

Extracted video frames

Step 3: Remove Background from All Frames

Now apply Rembg to all extracted frames:

mkdir frames_no_bg
for f in frames/*.png; do
  rembg i -m u2net "$f" "frames_no_bg/$(basename $f)"
done
Enter fullscreen mode Exit fullscreen mode

Frames with background removed

Performance comparison:

  • CPU processing: ~12.7 seconds per frame
  • GPU processing: ~3.6 seconds per frame

Using a GPU dramatically speeds up the process.

Step 4: Add New Background and Combine

Use ImageMagick to overlay each frame on a new background:

mkdir frames_final
for f in frames_no_bg/*.png; do
  convert background.png "$f" -gravity center -composite "frames_final/$(basename $f)"
done
Enter fullscreen mode Exit fullscreen mode

Frames with new background

Finally, combine the frames back into a video:

ffmpeg -framerate 30 -i frames_final/frame_%04d.png -c:v libx264 -pix_fmt yuv420p output_video.mp4
Enter fullscreen mode Exit fullscreen mode

Final result

Comparison

Output video

Conclusion

Using open-source tools like Rembg, FFmpeg, and ImageMagick, you can achieve professional-quality background removal from videos entirely via the command line. The GPU acceleration makes it practical even for longer videos.

Top comments (0)