Skip to content

Instantly share code, notes, and snippets.

@thomasba
Created December 25, 2019 23:59
Show Gist options
  • Save thomasba/7af891599b7b7fb03baeab004882741d to your computer and use it in GitHub Desktop.
Save thomasba/7af891599b7b7fb03baeab004882741d to your computer and use it in GitHub Desktop.
Create a slideshow with countdown from jpegs
#!/bin/bash
################################################################################
# Description #
################################################################################
# This script converts all jpg-Files inside the directory 'pictures' into a #
# slideshow with a countdown. #
# The countdown will appear in the top right corner and changes it’s position #
# to the center for the last ten seconds. #
################################################################################
################################################################################
# Settings #
################################################################################
# Time in seconds to display a picture
secs=4
# Resolution
# hd1080=1920x1080, hd720=1280x720, 4k=4096x2160
# more: https://ffmpeg.org/ffmpeg-utils.html#Video-size
resolution="hd1080"
# Filename for slideshow w/o countdown
slideshow="plain.mp4"
# Filename for slideshow w/ countdown
countdown="countdown.mp4"
################################################################################
# Step 1: Create slideshow from pictures #
################################################################################
# Use ffmpeg to create a slideshow... #
# - Get all the images from a directory using glob to extend the name #
# - zoompan: https://ffmpeg.org/ffmpeg-filters.html#zoompan #
# - d: Duration in number of frames #
# - s: Output size #
# - fps: Frames per second (1 -> duration in seconds ;-) ) #
# - Framerate: https://ffmpeg.org/ffmpeg-filters.html#framerate #
################################################################################
if [ ! -f "$slideshow" ] ; then
ffmpeg -pattern_type glob -i "pictures/*.jpg" -vf "zoompan=d=$secs:s=$resolution:fps=1,framerate=25:interp_start=0:interp_end=255:scene=100" -c:v mpeg4 -q:v 2 "$slideshow"
fi
################################################################################
# Step 2: Create countdown overlay #
################################################################################
# duration: Get the duration of the slideshow and save it to a variable, used #
# for calculations later... #
# #
# drawtext: #
# - font: set monospace as font #
# - text: The text to be displayed, calculated from the current video position #
# - eif: Evaluate the expression’s value and output as formatted integer. #
# - Minutes: Floor ( Duration-CurrentPosition ) / 2 #
# - Seconds: Modulo( Duration-CurrentPosition, 60) #
# - fontcolor: color of the font #
# - fontsize: the size of the font 40, last 10 seconds 120) #
# - x,y: Calculated position #
# - box: Display a box #
# - boxcolor: the color of the box, black with 50% transparency #
# - border of the box: 10px #
# format: yuv420p #
################################################################################
duration=$(ffprobe -loglevel error -show_entries format=duration -of default=nw=1:nk=1 "$slideshow")
ffmpeg -i "$slideshow" -vf "drawtext=font=monospace:text='%{eif\:floor(($duration-t)/60)\:d\:2}\:%{eif\:mod($duration-t,60)\:d\:2}':fontcolor=white:fontsize=if(lte($duration-t\\,10)\\,120\\,40):x=if(lte($duration-t\\,10)\\,(w-text_w)/2\\,w-tw-20):y=if(lte($duration-t\\,10)\\,(h-text_h)/2\\,20):box=1:boxcolor=black@0.5:boxborderw=10,format=yuv420p" -c:v libx264 -c:a copy -movflags +faststart "$countdown"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment