Skip to content

Instantly share code, notes, and snippets.

@GrennKren
Last active June 13, 2024 12:05
Show Gist options
  • Save GrennKren/b15d0be1b1e22c9759adaf96b70c90ab to your computer and use it in GitHub Desktop.
Save GrennKren/b15d0be1b1e22c9759adaf96b70c90ab to your computer and use it in GitHub Desktop.
Convert and Rescale Many Images Using FFMPEG
:: FOR WINDOWS batch
: using for /f "tokens=*" %i in ('dir /aa /b /o-d')
for /f "tokens=*" %%i in ('dir /aa /b /o-d') do ffmpeg -i "%%i" -vf "scale=min'(2000,iw):-1'" "converted_jpg\%%~ni.jpg"
#!/bin/bash
##########################################################
# To convert and resize image files, basically:
### ffmpeg -i "INPUT FILE" -vf "scale=min'(2000,iw):-1'" "OUTPUT FILE"
#
# -i "INPUT FILE"
# This is the input file parameter, specifying the location of the image you want to resize.
#
# -vf "scale=min'(2000,iw):-1'"
# This resizes the image so that its longest side is 2000 pixels, while maintaining the aspect ratio `-1`.
# The min() function ensures that only images with a width of at least 2000 pixels are resized down, not up.
# More details at https://ffmpeg.org/ffmpeg-filters.html#scale-1
#
# "OUTPUT FILE"
# This is where the resized image will be saved.
#
#
############################################################
#
# Looping:
# There are two ways to loop through the files.
# 1) find . -maxdepth 1 -type f | while read i
# 2) ls -tp | grep -v / | while read i
#
##############################################################
### find . -maxdepth 1 -type f | while read i
#
# find .
# This command searches for files and directories.
# The dot (.) means the current directory where your images are located.
#
# -maxdepth 1
# This limits the search to the current directory only, not subdirectories.
#
# -type f
# This filters the results to include only files, not directories.
#
#
###############################################################
### ls -tp | grep -v / | while read i
#
# ls
# This command lists the contents of the directory.
#
# -tp
# These are two options combined:
# -t sorts the files by modified time.
# -p adds a slash (/) to directories in the output.
#
# grep -v /
# This filters out directories from the `ls` output.
# -v / excludes lines that contain a slash (/) from the output.
#
#
###############################################################
### while read i
# This is a while loop that reads each file name into the variable `i`.
#
#
###############################################################
# About the ffmpeg command I will use:
# $i
# This is the current file name from the loop.
#
# -i "$(basename "$i")"
# This takes just the file name, excluding any leading `./`.
#
# "converted_jpg/$(basename "${i%.*}.jpg")"
# This specifies the output location. `converted_jpg` is a subdirectory you should create beforehand.
# "${i%.*}.jpg" changes the file extension to jpg.
#
# < /dev/null
# This redirects input to `/dev/null`, making sure no input is passed to ffmpeg.
# (Specifically needed for `ls`.)
#
#
###############################################################
# Usage:
# Using find . -maxdepth 1 -type f
# find . -maxdepth 1 -type f | while read i; do ffmpeg -i "$(basename "$i")" -vf "scale=min'(2000,iw):-1'" "converted_jpg/$(basename "${i%.*}.jpg")"; done
# Using ls -tp | grep -v /
ls -tp | grep -v / | while read i; do ffmpeg -i "$(basename "$i")" -vf "scale=min'(2000,iw):-1'" "converted_jpg/$(basename "${i%.*}.jpg")" </dev/null; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment