How to create a slideshow with subtitles and music with ffmpeg

Introduction

FFmpeg is an open-source video and audio manipulator tool. Through this tutorial, you will learn how to create a slideshow with subtitles and music with ffmpeg.

Doing things from scratch is an enlightening experience. You may want to write a python script on top of ffmpeg to create a slideshow.

Collection of photos for creating a slideshow with subtitles and music with ffmpeg

You need a collection of photos. Serialize their names.

photo01 photo02 photo03 photo04 photo05 ... ... photoxx

This naming helps ffmpeg to loop through the photos and perform manipulation.

It’s important to have the images on the same format. To convert type:

ffmpeg -i photo0.webp photo0.jpeg

Specify the extension of the output image and ffmpeg takes care of the rest.

Audio tracks for creating a slideshow with subtitles and music with ffmpeg

The slideshow becomes more attractive with music. You need some music files to add to the video. I use rap beats.

To download a beat from YouTube type:

yt-dlp  --extract-audio --audio-format aac video_beat_url_here

yt-dlp is an open-source video downloader that supports multiple sites. Make sure you respect the author’s copyrights.

Learn how to set up yt-dlp on Windows 10.

Create a playlist with some instrumentals. To download the whole playlist type:

yt-dlp --extract-audio --audio-format aac --yes-playlist  -P "C:\Users\User\Desktop\background\"  -o "%(title)s.%(ext)s" playlist-url 

This command downloads the whole playlist as audio files and saves each one with title and extension. The -p option specifies the path.

To repeat the audio many times create a text file as below.

file 'same_audio0.m4a'
file 'same_audio1.m4a'

To join the audio type:

ffmpeg -f concat -i concat_files.txt -c copy final.m4a -y 

concat_files.txt contains the audio files to join.

If you want the audio longer just add more files to the concat_files.txt. It’s a good idea to have the audio stream longer than the video.

Subtitles for creating a slideshow with subtitles and music with ffmpeg

Aegisub is an open-source program to create, manipulate, and style subtitles.

Open the program, go to Subtitle, Styles Manager.

To create a new style click on New.

create a slideshow with ffmpeg

Create a name for your profile. Click on Apply.

create a slideshow with ffmpeg
Create a new style to apply to your subtitles

Tick on the Opaque box to create a transparent box. Play with the Outline and Shadow values and view the preview.

create a slideshow with ffmpeg
A transparent box for the subtitle


My style is applied to every one of the subtitles in the table.

subtitle file ready to be used with ffmpeg

Create your subtitles, apply the style, and save the file with the .ass extension. For example subtitles.ass.

To use my style for your subtitles copy the following and save it with .ass extension. Just make sure to create your Dialogue under Events. It’s a format similar to those used for srt subtitles.

[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: None

[Aegisub Project Garbage]
Active Line: 7

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Consolas,27,&H00FFFFFF,&H000000FF,&H00060607,&H00000000,0,0,0,0,100,100,0,0,3,0,0.5,2,10,10,10,1
Style: crux,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&HE2000000,0,0,0,0,100,100,0,0,3,0,3.3,2,10,10,10,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,it's time to push yourself the limits
Dialogue: 0,0:00:05.00,0:00:07.00,Default,,0,0,0,,this is not the time to give up
Dialogue: 0,0:00:07.00,0:00:09.00,Default,,0,0,0,,and this is not about women, this is about your.

Create a slideshow from a collection of photos with ffmpeg

To create a slideshow from photos with ffmpeg type:

ffmpeg -y -framerate 1/7 -i "C:\Users\User\Desktop\slideshow\photo%2d.jpg" -filter:v \
"scale=eval=frame:w=1280:h=720:force_original_aspect_ratio=decrease" -pix_fmt yuv420p slideshow.mp4
  • -framerate specifies the time a photo lasts, in this case, 7 seconds.
  • -i specifies an input, photo%2d becomes photo01, photo02 and so on.
  • -filter:v here applies a filter to resize the images and keep the original aspect ratio.
  • -The pix_fmt option makes the video compatible with different video players.

How to burn the subtitles to the video with ffmpeg

To add the subtitles to the slideshow type:

ffmpeg -i slideshow.mp4  -vf  "ass=subtitles.ass" -c:a copy -c:v libx264 -crf 23 -preset veryfast subtitled_slideshow.mp4

The -vf applies a video filter which in this case is the ass=subtitles.ass. It hardcodes the subtitles to the video.

How to create a slideshow with subtitles and music with ffmpeg

To add music to your slideshow type:

ffmpeg -i subtitled_slideshow.mp4 -i bg1.m4a -shortest -c:v copy -map 0:v -map 1:a -y final_slideshow.mp4

In case one of the streams is longer than the other the -shortest option keeps the output video to the shortest.

The -c:v copy option just copies the video stream without re-encoding

Check your slideshow with ffprobe

Media players may fail to play the last frame. To check the number of frames type:

ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format csv subtitled_slideshow.mp4

ffprobe performs information gathering on multimedia streams

create a slideshow with ffmpeg
12 photos in my photo collection, 12 frames in the video

Play the video with ffplay

To play the video type:

ffplay final_slideshow.mp4

ffplay is a media player part of the FFMpeg framework.

Final thoughts

With a few commands, you can create a slideshow with ffmpeg. To automate the whole process read the second part.

Leave comment

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