How to Make Instagram's Boomerang Effect with FFmpeg

Instagram's Boomerang effect can add an interesting twist to videos. Learn how to create and offer this fun feature to your users using FFmpeg.
by Josephine Loo ·

Contents

    “Boomerang” is a feature on Instagram that lets users record videos that repeat themselves in a back-and-forth manner, creating a looping effect. Although this feature was launched back in 2015, it is still popular among users as it adds a fun and playful element to the videos.

    If you’re building a video editing tool, why not incorporate the boomerang effect into your tool? It is proven that users enjoy using it! In this article, we’re going to learn how to make the boomerang effect with FFmpeg, so that your users can turn a normal video like this:

    …into a fun video like this:

    What is FFmpeg

    FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It can decode, encode, transcode, mux, demux, stream, filter, and play media files in any format. It is also highly portable—it compiles and runs in a wide variety of build environments, machine architectures, and configurations like Linux, Mac OS X, Microsoft Windows, etc.

    FFmpeg contains multiple tools for end-users to convert, play, and analyze media files and libraries for developers to use in different applications, such as libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale, and libswresample. When you download the FFmpeg packages/executable files to your machine, these libraries will be downloaded automatically.

    Installing FFmpeg

    Before we begin, make sure to download FFmpeg from the official website. You can follow this step-by-step guide to install FFmpeg on your Mac, Windows, or Ubuntu/Linux.

    Note: This article is based on FFmpeg v6.0.

    Making Instagram's Boomerang Effect with FFmpeg

    A video with the boomerang effect is a video that repeats itself in a back-and-forth manner. Therefore, to apply this effect to a video, we will first reverse the video and then merge it with the original video.

    Step 1. Reverse the Input Video

    Run the command below in your terminal/command prompt to reverse the input video using the reverse video filter and save it as “reversed.mp4”:

    ffmpeg -i input.mp4 -vf reverse reversed.mp4
    

    The command above reverses only the video stream. As audio is insignificant for the boomerang effect, we do not need to reverse the audio stream.

    Here's the video reversed:

    Step 2. Merge the Original and Reversed Videos

    We will merge the original and reversed videos to create the back-and-forth motion. This can be done with two different methods:

    Using the “concat” Demuxer

    The concat demuxer reads a list of video files from a text file and demuxes them in sequence. To use the concat demuxer, the video files must have the same streams. However, they can be wrapped in different container formats such as .mp4, .mov, .wmv, etc.

    First, create a .txt file (eg. join.txt) and list the video files’ paths/names:

    file input.mp4
    file reversed.mp4
    

    Then, run the FFmpeg command below to merge the videos listed in the file:

    ffmpeg -f concat -i join.txt -c copy output.mp4
    

    The -c copy option copies the streams from the input files to the output file without re-encoding it. This makes the merging efficient and preserves the original quality.

    Using the “concat” Filter

    Another way to merge the videos is by using the concat filter. As we have two input videos, we will use the -filter_complex option to apply the concat filter to them:

    ffmpeg -i input.mp4 -i reversed.mp4 -filter_complex "[0:0][1:0]concat=n=2:v=1[outv]" -map "[outv]" output.mp4
    

    -filter_complex "[0:0][1:0]concat=n=2:v=1[outv]" uses the [0:0] and [1:0] stream references to refer to the video streams of both input files, apply the concat filter to concatenate them, and specify the output stream as [outv]. The n=2 and v=1 options indicate the number of input and output streams respectively.

    You can also use [0:v] instead of [0:0] to select the video stream:

    ffmpeg -i input.mp4 -i reversed.mp4 -filter_complex "[0:v][1:v]concat=n=2:v=1[outv]" -map "[outv]" output.mp4
    

    Using either of the methods, the result should look like this:

    🐻 Bear Tips: There are more ways to merge video files. Refer to How to Merge Video Files Using FFmpeg to learn more.

    One-Line Command

    The previous section breaks down the process of creating a boomerang effect with FFmpeg into two steps but you can also combine them into one:

    ffmpeg -i input.mp4 -filter_complex "[0:v]reverse[r];[0:v][r]concat=n=2:v=1[outv]" -map "[outv]" output.mp4
    

    filter_complex "[0:v]reverse[r];[0:v][r]concat=n=2:v=1[a]" is where all the work is done. Let’s break it down:

    Reversing - [0:v]reverse[r] takes the original video stream [0:v], reverses it, and stores the result in [r].

    Merging - [0:v][r]concat=n=2:v=1[outv] concatenates the original video stream [0:v] and the reversed stream [r], with n=2 and v=1 specifying the number of input and output streams respectively. As a result, a new video stream [outv] is created.

    When you run the command, the input video will be transformed into a video where the content is played back and forth.

    Adjusting the Speed and Number of Loops

    The previous commands merge the original and reversed video without adjusting their speeds. Therefore, the length of the output video is double that of the input video. To retain the length of the original input video, we can double the speed using the setpts filter. Here’s the FFmpeg command:

    ffmpeg -i output.mp4 -vf "setpts=1/2*PTS" output_fast.mp4
    

    The command above changes the PTS (presentation timestamp) of the input frames to half of the original, creating an output video with double the speed:

    You can also adjust the number of the boomerang effect loop. To do so, create a new .txt file and insert file output.mp4 as many times as you want to loop the video. For example, a .txt file with the content below will loop the output video three times:

    file output.mp4
    file output.mp4
    file output.mp4
    

    Run the command below to merge the videos using the “concat” demuxer, like what we have done previously:

    ffmpeg -f concat -i loop.txt -c copy output_loop.mp4
    

    The final result will look like this:

    Using Bannerbear API

    Building video editing features from scratch can be challenging and time-consuming. When you have limited resources and time, using a third-party solution can significantly simplify your work. One such solution is the Bannerbear API, which provides an easy and efficient way to integrate automated video editing features into your application.

    For example, you can integrate Bannerbear’s auto-transcription feature into your application to help users generate and add subtitles for their videos more efficiently, creating a video like this automatically:

    This is only one of the features that you can integrate into your application easily using the API (API Reference). To learn more, you can refer to these articles:

    … and register a free account to try it out yourself!

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    Template Set: Batch-Generate Images in Different Dimensions Using Bannerbear (Node.js)

    Ever found yourself manually resizing images for different platforms? Let’s automate that! With Bannerbear, you can easily generate images in multiple dimensions in one go. This step-by-step tutorial shows you how to use Bannerbear’s template set to create perfectly sized images for any platform.

    Bannerbear Beginner Guide: How to Start Generating Images Dynamically in JavaScript with Bannerbear

    If you want to integrate dynamic image generation into your JavaScript project, this article is for you! Learn how to do it effortlessly with Bannerbear—we’ll walk you through the entire process, from setup and implementation to achieving the final result.

    How to Instantly Generate Certificates Online with Bannerbear and Airtable

    One way to generate a large batch of certificates on-demand is with a direct Airtable integration. Here's how to connect your base to Bannerbear and generate dozens of graphics in seconds!

    Automate & Scale
    Your Marketing

    Bannerbear helps you auto-generate social media visuals, banners and more with our API and nocode integrations

    How to Make Instagram's Boomerang Effect with FFmpeg
    How to Make Instagram's Boomerang Effect with FFmpeg