MacOS: Removing Video Backgrounds

September 26, 2024 - 10 min read

Introduction

If you've ever tried to film a short movie, you've probably had to do some cropping and/or masking at one point or another in order to get rid of an unwanted object or background. If you own a green screen, the process is somewhat simpler, though it can be tricky to get a good lighting setup (which is really important when chroma keying). Chroma keying, by the way, is when you key out a specific chroma, or color. But I digress.

In a situation where you have a actor in front of a pesky, unwanted background, and you cannot reshoot the scene in front of a green screen, you may have turned to sites that advertise "AI Background Removal." Often these sites are either scams, the app only works on a Windows machine, or they just don't produce a quality result. The ones that do produce actual videos in resolutions higher than 240p are most often locked behind a paywall, ever tempting and ever out of reach.

Well, no more!

Starting with MacOS Ventura, users could leverage the new Shortcuts app to create powerful workflows by using built-in actions from the Shortcut Action Library. Among these actions is one innocently titled, "Remove Image Background."

If you're thinking what I'm thinking, feel free to go play around with it and see what you can come up with. If you have absolutely no idea what I'm jabbering about, just keep reading.

The Tech(y) Stuff

I could explain every step of how I did this, but I'll just cut right to the chase. Basically, my workflow is as follows:

  1. Use ffmpeg to convert the video with the original background into an image sequence.
    ffmpeg -i path/to/your/video.mp4 -pix_fmt rgba path/to/your/output_%04d.png
    • Replace the "path/to/your/" parts with the paths to your input video and output image sequence folder.
    • Replace the "video.mp4" part with your video name, but keep in mind that the video must be in mp4 format. To convert from another format to mp4, use a tool like HandBrake.
    • Important: Do not remove "_%04d.png" part of the command. This is essantial for image sequence ordering.
  2. Iterate through each image using the "Remove Image Background" action in the Shortcuts app to produce a new image sequence, this one with a transparent background.
    • The Shortcut I built is available here to download. Click the "Add Shortcut" button to add it to your library.
    • Right-click the folder containing your image sequence, go down to the "Quick Actions" section, and click "Remove Video Background."
    • When asked for an output destination, click "Ok", then select a folder for the new image sequence to be output to.
  3. Use the built-in QuickTime Player app to convert the new image sequence back into a video.
    • Open QuickTime player from the app launcher, then select File > Open Image Sequence.
    • Choose the folder containing your new image sequence, then set the resolution to "Actual Size", and set the frame rate to that of the original video (the one with the pesky background).
    • After QuickTime is done preparing your video, use 'Command-S' or 'File > Save' to save it wherever you want.

Congratulations! You're done! However, there are some potential problems and limitations that I'll discuss below.

Troubleshooting

  1. If ffmpeg is not installed, install it through brew using:
    brew install ffmpeg
    If brew is not installed, you can get it here.
  2. If you don't have a transparent background after importing the clip into iMovie, FCP, etc, make sure that the clip is still in .mov format. Because of the way alpha channels work, an .mp4 or .mkv file may not have a transparent background.