Turn your GIFs into longer videos for Instagram

Published

Converting GIFs into video is something that is essential for posting animated GIFs to Instagram and other social media.

There are several ways to do this:

  • You can use Giphy, which has an option to download your GIF as a looping video, and send it to your email as a five second clip. This outputs a nice 5 second looping clip, which is great, but it takes several steps and you can only do one at a time.
  • You can use EZGif, which is great for quick one-off exports but it doesn’t produce a repeating clip, and at the time of writing, instagram has a 4-second minimum on reels, so this solution isn’t great.
  • Creating an automator script to convert GIFs into video clips. This is my preferred method, and I’ve detailed it below. Using this method, you can also batch convert GIFs to MP4. In my script, I’ve set it to loop for 5 seconds (and you can tweak this to be longer or shorter as needed).

Converting GIFs to Video on Mac OS

Set up time: 5 minutes; the video below shows the end result. No matter where you are on your computer, you can right click and convert a GIF to a video:

Setting Up Your Environment

First, open Terminal on your mac. It’ll be in your Applications folder inside of the “Utilities” folder. You can also just type Spacebar and then type “Terminal”.

Next, install Homebrew on your Mac, if you haven’t already. To do so, copy and paste this into Terminal, and then hit enter.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Next make sure you’ve got all the dependencies installed:

brew install ffmpeg
  • Next open Automator (type Spacebar and then type “Automator”)
  • Then click “New Document”
  • Then click “Quick Action”
  • Type “Shell” in the search to pull up “Run Shell Script” and double click it to add it to your workflow.
  • Select “Files or Folders” and in “Finder.app” up at the top.
  • Select “Pass input” on the right and choose “As arguments”.
  • Paste this code below into the main box:
for f in "$@"
do
    # Get the filename without extension
    filename=$(basename "$f" | cut -d. -f1)
    
    # Set the desired duration (in seconds) for the output video
    duration=5
    
    # Set the output directory (change this to your desired location)
    output_dir="$HOME/Desktop"
    
    # Create the output file path
    output_file="${output_dir}/${filename}.mp4"
    
    # Get the frame rate of the input GIF
    input_fps=$(/opt/homebrew/bin/ffmpeg -i "$f" 2>&1 | sed -n "s/.*, \(.*\) fps.*/\1/p")
    
    # Get the number of frames in the input GIF
    num_frames=$(/opt/homebrew/bin/ffmpeg -i "$f" 2>&1 | sed -n "s/.* \([0-9]\+\) frames.*/\1/p")
    
    # Calculate the number of loops needed to reach the desired duration
    gif_duration=$(echo "scale=2; $num_frames / $input_fps" | bc)
    loops=$(echo "scale=0; ($duration + $gif_duration - 0.01) / $gif_duration" | bc)
    
    # Convert the image to an MP4 file using ffmpeg
    if /opt/homebrew/bin/ffmpeg -loglevel error -stream_loop $((loops-1)) -i "$f" -t $duration -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p" -r "$input_fps" -movflags +faststart "$output_file"; then
        echo "Conversion successful: $output_file"
    else
        echo "Conversion failed for: $f"
    fi
done

When you’re done, it should look like this screenshot below.

Finally, save it as “GIF to MP4” or something similar, and your done!

Now when you go to finder and right click on a GIF, you’ll you can hover over “Quick Actions” and see your GIF to MP4 command.

How it works: converting GIFs to MP4s

This script takes one or more image files (like GIFs or WebP animations) and converts them into MP4 videos. It ensures each video loops enough times to last for about 5 seconds. Here’s what each part of the script does, step by step:

  1. Loop Through Each File: The script starts by looking at each file you’ve selected to convert. In computer speak, it “iterates” over them, which means it takes them one by one to apply the same process.
  2. Get the Filename Without Extension: It figures out the name of each file without its ending part (like .gif or .webp) so it can use this name for the new MP4 file it’s going to create.
  3. Set Desired Video Duration: It sets aside a note that we want our final video to be 5 seconds long.
  4. Choose Where to Save the New Video: It decides to save the new MP4 video on your Desktop so you can easily find it.
  5. Create a Path for the New Video: It prepares a spot on your Desktop with the right name ready for the new video file.
  6. Find Out How Fast the Original Moves: It checks how many pictures (frames) the original file shows every second (frame rate). This helps it understand how quickly the original animation moves.
  7. Count How Many Pictures Are in the Original: It counts how many individual pictures (frames) are in the original file to figure out how long the original animation is from start to finish.
  8. Figure Out How Many Times to Loop: It does some math to decide how many times the original animation needs to loop to make a video that’s about 5 seconds long. It considers how long one loop of the animation takes and uses that to calculate the total number of loops needed.
  9. Do the Conversion: Here’s the main event where it uses a tool called FFmpeg (a very powerful video and audio processing tool) to turn the original file into a new MP4 video. It tells FFmpeg how many times to loop the animation, how long the final video should be, and some technical details about the video format. It tries to make sure the new video works well on most devices.
  10. Log the Outcome: It writes down what happened during the conversion process on your Desktop in a file called conversion.log. If everything went well, it notes that the conversion was successful and tells you where the new file is. If something went wrong, it writes that the conversion failed, helping with troubleshooting.

This script is quite handy because it automates the whole process of converting animations into video files, doing all the heavy lifting like calculating loop times and handling technical settings for you. Plus, it keeps a record of the process, which is useful for checking back on how things went or debugging if needed.

Troubleshooting

Note: You may need to adjust the location /opt/homebrew/bin/ffmpeg or if your computer has ffmpeg installed somewhere else. To check to see where ffmpeg, go back to terminal and type:

where ffmpeg

Then it’ll show the location and you can paste it into your code above.

One other note: One peculiarity with animated GIFs is that if they are set at 0.01 frames per second, they will actually play back in a browser at a much slower 0.10 frames per second. But when converted to a video, these will then play at breakneck speed. So if you have any GIFs that are set at 0.01 frames per second, you may want to manually convert them.

As noted above, this script comes with a conversion.log file that writes to the desktop, so if you run into any trouble you should be able to look in there and see why it failed, and can come paste those results in your comment below.

Conclusion: Automating is fun!

Now you’ll be able to right click on any GIF file on your computer and convert it to a 5-second MP4 video file, perfect for sharing on TikTok or Instagram.

I’m always looking for ways to improve our processes. If you’ve got a tip or question, please comment below.

Leave a comment

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