Image by Becca Clark from Pixabay
Creating a slideshow video with background music is an effective way to display images in a dynamic, engaging way. Whether you’re creating a video for a presentation, social media, or just for fun, FFmpeg makes it incredibly easy to merge images and audio into a professional-looking video.
In this blog, we’ll walk you through how to generate a slideshow video from a series of images, and add background music to make it more captivating—all using Google Colab, Python, and FFmpeg!
Step 1: Setting Up the Environment
To get started, we’ll first need to install FFmpeg in our Google Colab environment. FFmpeg is a powerful multimedia processing tool that we will use to combine images and audio into a video. We can install FFmpeg using the following command:
!apt-get install ffmpeg
This ensures that we have the necessary tool to handle video processing tasks. Once FFmpeg is installed, we can proceed to upload the images and the background music file.
Step 2: Uploading Images and Background Music
In Google Colab, we can easily upload files directly into the environment using the files.upload()
method from the google.colab
library. This allows us to upload our images and music files without having to host them elsewhere.
Uploading the Images
from google.colab import files uploaded = files.upload()
This will prompt you to upload your image files. You can select multiple image files from your local machine that you’d like to include in the slideshow.
Uploading the Background Music
uploaded = files.upload()
Similarly, upload the background music file (e.g., MP3 or WAV) that will be added to the video.
Step 3: Setting Up Parameters
Now, we need to set up some important parameters. These include the paths to the images, the background music file, and the desired video output paths.
import os # Parameters image_folder = '/content/' # Path to your image folder output_video = '/content/slideshow.mp4' # Path for the intermediate video file final_output = '/content/slideshow_with_music.mp4' # Path for the final video with music background_music = '/content/calm_music.mp3' # Path to your background music file time_per_image = 10 # Time each image will be displayed (in seconds)
Here, time_per_image
is set to 10 seconds, meaning each image will be displayed for 10 seconds in the final video.
Step 4: Getting the List of Images
Next, we get the list of image files from the uploaded folder and check if there are any images. If no valid images are found, we raise an error.
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))] num_images = len(image_files) if num_images == 0: raise ValueError("No image files found in the specified folder.")
This code filters out only the image files that are .png
, .jpg
, or .jpeg
.
Step 5: Creating the FFmpeg Command to Generate the Video
FFmpeg requires a specific command to process the images and create the video. We need to loop through all the images, specify the time each image should be shown, and apply effects like fading and zooming.
input_images = [] filter_complex = [] for i, image in enumerate(image_files): image_path = os.path.join(image_folder, image) input_images.append(f"-loop 1 -t {time_per_image} -i {image_path}") filter_complex.append( f"[{i}:v]scale=1280:720,zoompan=z='zoom+0.002':" f"d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)',fade=t=in:st=0:d=0.5,fade=t=out:st={time_per_image-0.5}:d=0.5[slide{i}]" )
In this part of the code:
- We loop over all images and prepare them for the slideshow.
- The
-loop 1
flag tells FFmpeg to loop the image for the duration we specify (-t {time_per_image}
). - The
filter_complex
applies transformations like scaling, zooming, and fading effects to each image.
Step 6: Concatenating All Images into a Video
Once the individual images are processed, we concatenate them together into a single video stream:
filter_complex.append( f"{''.join(f'[slide{i}]' for i in range(num_images))}concat=n={num_images}:v=1:a=0,format=yuv420p[v]" )
This tells FFmpeg to concatenate the images and output them as a single video. The concat=n={num_images}
specifies the number of images.
Step 7: Running the FFmpeg Command
Now that we have set up the input and output for the images, it’s time to run the FFmpeg command to generate the video:
ffmpeg_command = f""" ffmpeg -y \ {' '.join(input_images)} \ -filter_complex "{';'.join(filter_complex)}" \ -map "[v]" -r 30 {output_video} """ print("Generating video with images...") subprocess.run(ffmpeg_command, shell=True, check=True)
This command will take all the images, apply the transformations (scale, zoom, fade), and generate the intermediate video (output_video
). The -r 30
flag ensures the video is generated at 30 frames per second (FPS).
Step 8: Adding Background Music to the Video
Once the video with images is ready, we can add the background music to the video using another FFmpeg command. The music will be looped and mixed with the video.
ffmpeg_command = f""" ffmpeg -y \ -i {output_video} \ -i {background_music} \ -filter_complex "[1:a]aloop=loop=-1:size=2e+09,volume=0.3[audio2]; [audio2]amix=inputs=1:duration=first[a]" \ -map 0:v -map "[a]" -c:v libx264 -c:a aac -shortest {final_output} """ print("Adding background music...") subprocess.run(ffmpeg_command, shell=True, check=True)
Here, we:
- Loop the background music (
aloop=loop=-1
). - Mix the audio with the video, ensuring the duration of the video and audio match (
duration=first
).
Step 9: Displaying the Final Video
# Display the final video in the notebook from IPython.display import Video Video(final_output)
And there you have it! With just a few lines of code, you can transform a set of images into a captivating slideshow with background music using Google Colab and FFmpeg. Experiment with different music tracks, image effects, and timings to create your own stunning videos.
Ready to start creating your own? Let us know what you come up with!