Create Slideshows with Audio on the Command Line

Screenshot of  command line showing audio and image files
Table of Contents

Background

Pre-COVID, I taught Buddhist meditation in my local prison system, in person. Of course, once the pandemic hit, volunteers were no longer allowed to go into the prisons. So in place of meeting with inmates, another volunteer and I started recommending videos each week and creating handouts to send via email to the chaplains at prisons across the state.

At first, we recommended Vimeo and YouTube videos, because that seemed like it would be easy. However, we learned that some of the facilities were not allowed to show any videos from the Internet. So in our next iteration, I started using tools such as youtube-dl in order to download and share .mp4 versions of videos under a creative commons license.

After a few months, we started to run out of videos that fit our criteria and began to look elsewhere. Websites such as Amaravati.org and dhammatalks.org have enormous collections of audio, and teachers with a local connection to Minnesota, such as Ajahn Punnadhammo and Ajahn Chandako also have recorded audio that we wanted to use. But, the chaplains in the prisons requested that we send more than audio. They told us even "an image of a burning candle" would suffice.

At this point, I realized that I needed an easy way to create a slideshow from an audio file and an assortment of images, using only free software. I did not want to spend hours a week creating these videos. I've used software such as OpenShot, Kdenlive, and Blender, and have never found a quick way to edit an hour-long video. I wanted to develop a pattern using command line tools that I could fairly easily reproduce each week.

Fast forward about 8 months and I feel like I have developed a useful, reliable process. This kind of thing certainly is not for everyone, but I thought others might find it helpful, too, so I'm sharing my system here. If you are comfortable using command-line programs, then I will assume that you know how to use man pages to find out more about what each command is doing.

Process

First, I find images to use, either in the public domain or images for which we acquire permission to use. It hasn't been difficult getting permission when so many teachers want to share their teachings. Generally, I try to find at least 20 images or so. Once I have the images that I want to use, I make them the same size. For example, if I have a directory of .jpg images that are at least 1280x720, I can make them all 1280x720 with a single mogrify command:

mogrify -resize 1280x720^ -gravity center -extent 1280x720 *.jpg

I generally stick with resizing the image from the center, but there are other options, such as North, NorthEast, West, Center, etc., which you can list with mogrify -list gravity. Occasionally, if an image looks weird I will choose a different gravity for just that image.

Second, I name them all using the same pattern, image001.jpg, image002.jpg, etc. I do that with bash in a single line:

ls | grep "\.jpg$" | cat -n | while read n f; do mv "$f" `printf "image%03d.jpg" $n`; done

Finally, I put the audio file (MP3) in the same directory and make the slideshow with a single ffmpeg command:

ffmpeg -y \
-loop 1 -framerate 1/30 -i image%03d.jpg \
-i "teacher--the-topic.mp3" -c:a copy \
-shortest -r 5 "teacher--the-topic.mp4"

For a 60-minute video, this ffmpeg command can take a few minutes, but I don't mind the wait. The result is a basic slideshow, with all of the images in a loop, changing every 30 seconds (to change them every 15 seconds, change the framerate to 1/15). That's the basic process: just those three commands.

Slightly More Complex

Recently, the Barre Center for Buddhist Studies gave us permission to use an audio file, but requested that we include an audio credit at the beginning. To accomplish that, I fired up Audacity, recorded the message, imported the original audio, and exported the whole thing as an MP3.

As a bonus, I moved image001.jpg to the end (i.e., image022.jpg) and then I downloaded a logo from the Barre Center. Because it was a logo, I couldn't just crop it. Instead of using a big application, such as the GNU Image Manipulation Program (GIMP), I used a single convert command to create the correctly-size image and fill in the background with white:

convert logo.jpg -resize 1280x720 -background white -gravity center -extent 1280x720 image001.jpg

That's it. If you found this useful, I've got lots of other tips and tricks on matthewtift.com/tricks.

The subscriber's email address.
Enjoy this article? Sign up to receive tips for a more peaceful, mindful, and compassionate life, delivered straight to your inbox.

Comments2

Anna

7 months 3 weeks ago

I have an mkv file, where I want to replace the video by a picture for the whole duration of the audio file. What would be the easiest way to do it in linux?

I would probably do something like this to extract the audio from the mkv:

ffmpeg -i input.mkv -vn only-audio.mp3

The -vn flag blocks all video streams of a file from being filtered or being automatically selected or mapped for any output.

Then I would probably combine the audio and the image doing something like this:

ffmpeg -loop 1 -i image.jpg -i only-audio.mp3 -shortest out.mp4

I haven't tested any of this code, but it should give you the basic idea.