Skip to content

Instantly share code, notes, and snippets.

@tyhenry
Created March 21, 2024 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyhenry/fb66bcc2f98c63737f712ad4b27002f5 to your computer and use it in GitHub Desktop.
Save tyhenry/fb66bcc2f98c63737f712ad4b27002f5 to your computer and use it in GitHub Desktop.
Stich PNGs in directories to MP4
#!/bin/bash
mindepth=0
maxdepth=0
original_dir=$(pwd)
print_help() {
echo "Usage: $0 [OPTION]..."
echo "Create videos from PNG files in directories."
echo
echo "Options:"
echo "-r search subdirectories recursively"
echo "-s only search subdirectories (not this directory)"
echo "-h display this help and exit"
}
while getopts ":srh" opt; do
case ${opt} in
s )
mindepth=1
maxdepth=1
;;
r )
maxdepth=10
;;
h )
print_help
exit 0
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
print_help
exit 1
;;
esac
done
while IFS= read -r -d '' D; do
cd "${D}"
if [ "$(ls -A *.png 2>/dev/null)" ]; then
if [ "${D}" = "." ]; then
video_name=$(basename "${original_dir}")
else
video_name=$(basename "${D}")
fi
if [ -f framerate ]; then
framerate=$(cat framerate)
else
framerate=1/5
fi
find . -maxdepth 1 -name "*.png" -print0 | sort -zV | xargs -0 cat | ffmpeg -framerate "${framerate}" -i - -r 10 "${video_name}.mp4"
fi
cd "${original_dir}"
done < <(find . -mindepth ${mindepth} -maxdepth ${maxdepth} -type d -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment