FFMPEG for Image Slideshow

Posted: 2020-10-10 | Updated: 2020-10-10 | Read Time: 4 minute(s)
Terminal window of the script that was built to generate slideshow video from images.

Background

I needed to create a YouTube channel trailer for another channel that I manage for an organization. The channel already existed for some time, there was not many videos uploaded to the channel in past years and we were trying to increase the subscriber count.

One of the ways to increase the subscriber count is to meet the best practices that YouTube recommends. One of those recommendations is to have a channel trailer.

Get The Images

Over the years, there have been a number of images uploaded to the Instagram account for the organization. Instagram makes it rather difficult for you to download the images that you have previously posted once you upload them. In order for me to get the content that had been previously uploaded, I had to go into the Settings and then request a download of the Instagram Data.

Depending on the amount of content that you have previously uploaded, it may take some time for Instagram to provide you with the data. In this particular scenario, it took about 3 hours for me to receive the email notification that the download was ready. Once you have received the email notification, you have to go back to Instagram and it will present you with the option of downloading the data. The data download is provided as a zip file.

Screenshot of data download zip file

As you see, there are a number of files. All of your captions, comments, and more are provided in the download. The part I am most interested in is the Photos folder. Inside of this folder, the photos are grouped by the year and month that they were posted.

FFMPEG

While I could have used another tool for this process, I wanted to figure out a fast way to do this. Thus it immediately came to mind to use ffmpeg to make the video from the photos.

How did I come to this thought? When setting up automations with ffmpeg previously, I found out that it is a very powerful tool when it comes to outputting things as video. Just have to know what commands to use, or know what commands somebody else has used, to get it to do what you want it to do.

Thus to get the images that I wanted into a video file, I created the below script.

The Script

#!/bin/bash

# create a video slide show from a folder of images
# information sourced from https://trac.ffmpeg.org/wiki/Slideshow

## remove existing files
rm output.mp4 list.txt

## loop through the files and subfiles in the directory
for filename in $(find . -name '*.jpg')
do
echo "file '${filename}'" >> list.txt
echo duration 2 >> list.txt
done

## run create the video file
ffmpeg -f concat -safe 0 -i list.txt -vsync vfr -pix_fmt rgb24 output.mp4

Latest version of this script that I am using is available in my Ubuntu Automation repository.

Script Breakdown

I got some of the commands from the FFMPEG Documentation. Leveraging the documentation, also known as RTFM, can be helpful.

First, the script removes the existing output.mp4 and list.txt files. The previous files are removed because ffmpeg will either prompt to overwrite or automatically exit, depending on the options you select, when it sees an output file that already exists.

Next, it will loop through all the files that are in the directory or child directories looking for files that end with ".jpg". If you have files in a different format, you can change the extension to match the files in your directory.

Next, each image filename is added to list.txt and the duration that the picture should be displayed is added to the list.txt file between each image. One thing that I did note with this, is that the first image tends to stay up longer than the specified amount of seconds. In my scenario, the first image was displayed for about 3.5 seconds. Not sure why that is the case, but it was.

Finally, after looping through all of the images, ffmpeg is called with various arguments to generate the video file from the images.

Additional Editing

I wanted to have some text overlays in the video, such as organization branding and social media information. Thus after the video had been rendered by ffmpeg, I used Kdenlive to add these additional elements to the video.

Then that output was uploaded to the organization's YouTube channel as the channel trailer. If you would like to check out the final version of that video, then you can watch it on YouTube.

Published: 2020-10-10
Updated: 2020-10-10
Author: Kenny Robinson, @almostengr
Category: Technology
Keywords: ffmpeg, photos, images, slide show, slideshow, photo video, video from photos
Read Time: 4 minute(s)