Skip to content

Instantly share code, notes, and snippets.

@UdaraAlwis
Last active March 17, 2023 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UdaraAlwis/136e2abe72520eb842df4b44df4e0416 to your computer and use it in GitHub Desktop.
Save UdaraAlwis/136e2abe72520eb842df4b44df4e0416 to your computer and use it in GitHub Desktop.
Generate a slideshow video from images in a folder using ffmpeg
:: Generate a Video Slideshow from images in a folder on Windows
:: that's optimized for current system's screen resolution
:: Make sure your Images, ffmpeg executables, and this batch file are in the same folder
:: The images should be ".jpg" format, or you may change in the script below
:: Make sure to download ffmpeg: https://www.ffmpeg.org/download.html
:: Command Line executable code
:: Begin by getting the framerate. ex: 1/2
:: This determines time span for each image to display
:: And load the images in the current directory
@echo off
echo Let's get started!
set /p framerate=Enter framerate:
:: Generate a temp file that include list of images
for /r %%a in (*.jpg) do (
echo file '%%a' >> temp_imageslist.txt
)
:: Retrieve system screen resolution
for /f "delims=" %%# in ('"wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
set "%%#">nul
)
echo Screen Resolution detected:
echo [%CurrentHorizontalResolution% x %CurrentVerticalResolution%]
:: Execute ffmpeg magic!
ffmpeg.exe -y -r %framerate% -f concat -safe 0 -i temp_imageslist.txt -vf scale=(iw*sar)*min(%CurrentHorizontalResolution%/(iw*sar)\,%CurrentVerticalResolution%/ih):ih*min(%CurrentHorizontalResolution%/(iw*sar)\,%CurrentVerticalResolution%/ih),pad=%CurrentHorizontalResolution%:%CurrentVerticalResolution%:(%CurrentHorizontalResolution%-iw*min(%CurrentHorizontalResolution%/iw\,%CurrentVerticalResolution%/ih))/2:(%CurrentVerticalResolution%-ih*min(%CurrentHorizontalResolution%/iw\,%CurrentVerticalResolution%/ih))/2 -c:v libx264 -r 30 -pix_fmt yuv420p SlideshowVideo.mp4
:: We're setting the scaling of the images safely without disturbing its aspect ratio ;)
:: -y ~ override if the output file already exists
:: -safe 0 ~ Ignore unsafe file names
:: -r 30 ~ Frames Per Second during playback
:: Delete generated temp file
del /q temp_imageslist.txt
:: Used Resources:
:: Base Tutorial: https://trac.ffmpeg.org/wiki/Slideshow
:: Helped me fix image resoulution problem: https://superuser.com/questions/891145/ffmpeg-upscale-and-letterbox-a-video
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment