Skip to content

Instantly share code, notes, and snippets.

@ertdfgcvb
Last active February 21, 2023 08:06
Show Gist options
  • Save ertdfgcvb/6c1980f6e44c19c17a65bcaf59104d07 to your computer and use it in GitHub Desktop.
Save ertdfgcvb/6c1980f6e44c19c17a65bcaf59104d07 to your computer and use it in GitHub Desktop.
#!/bin/sh
# ------------------------------------------------------------------------------
# Uses FFmpeg to create an H.264 encoded MPEG-4 movie at 30 fps from
# an image sequence; filesize/quality can be tuned with the second parameter.
#
# ------------------------------------------------------------------------------
# Usage:
# > sh makemovie.sh imageFolder 20
# - imageFolder is a folder containing the images
# - 20 is an optional constant rate factor value,
# lower values mean better quality but bigger file sizes, defaults to 23
# - outputs a file like “imageFolder_20.mp4”
#
# Note:
# The images inside the folder should be generated with leading zeroes
# to avoid reordering:
# 000.png, 001.png, 002.png, etc.
#
# ------------------------------------------------------------------------------
# Resources:
# https://trac.ffmpeg.org/wiki/Encode/H.264
FOLDER=$1 # arg 1
QUALITY=${2:-23} # arg 2, defaults to 23
ffmpeg \
-framerate 30 \
-pattern_type glob \
-i "$FOLDER/*.png" \
-c:v h264 \
-pix_fmt yuv420p \
-tune animation \
-preset:v slow \
-profile:v baseline \
-crf $QUALITY \
-movflags +faststart \
-an \
"$FOLDER_$QUALITY.mp4"
# ----------- codecs: ----------------------------------------------------------
# -c:v h264
# -c:v h264_videotoolbox (hardware accelerated but worse results?)
# -c:v libx265 (see usage below)
# -c:v h265_videotoolbox
#
# ----------- tune: -----------------------------------------------------------
# -tune animation
# -an (disable audio)
#
# ----------- color: -----------------------------------------------------------
# -vf format=yuv420p (alias: -pix_fmt yuv420p)
# -vf format=gray (alias: -pix_fmt gray)
#
# ----------- bitrate: ---------------------------------------------------------
# -crf 23 (vconstant rate factor)
# -b:v 7M (variable)
#
# ----------- (up)scale: VF 8k, 4k, HD -----------------------------------------
# -vf scale=4320x7680:flags=neighbor (uses neighbor sampling)
# -vf scale=2160x3840:flags=neighbor
# -vf scale=1080x1920:flags=neighbor
#
# ----------- libx265 examples: ------------------------------------------------
# ffmpeg -framerate 60 -pattern_type glob -i "$1/*.png" -c:v libx265 -crf 30 -tag:v hvc1 -pix_fmt yuv400p -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart -an "$1.mp4"
# ffmpeg -framerate 60 -pattern_type glob -i "$1/*.png" -c:v libx265 -crf 25 -tag:v hvc1 -pix_fmt gray -tune animation "$1.mp4"
# ffmpeg -framerate 60 -pattern_type glob -i "$1/*.png" -vf scale=4320x7680:flags=neighbor -c:v libx265 -crf 25 -tag:v hvc1 -pix_fmt gray -tune animation "$1.mp4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment