Skip to content

Instantly share code, notes, and snippets.

@bpholt
Last active June 6, 2019 23:39
Show Gist options
  • Save bpholt/465b64b20faf6bea3579abea5c047915 to your computer and use it in GitHub Desktop.
Save bpholt/465b64b20faf6bea3579abea5c047915 to your computer and use it in GitHub Desktop.
Creating a wide-color HDR time lapse using FFMpeg
# ffmpeg encoder Makefile to build a consolidated video from multiple
# image sequences with potentially varying frame rates
#
# Create subdirectories containing the image sequence files and a
# special file called FPS, which should just contain the desired
# frame rate. For example, this structure:
#
# TOPDIR
# |-- Makefile
# |-- final.txt
# |-- mountains/
# | |-- FPS
# | |-- image001.tiff
# | |-- image002.tiff
# | |-- ...
# |-- cityscape/
# | |-- FPS
# | |-- image001.tiff
# | |-- image002.tiff
# | |-- ...
#
# would create a final.m4v made up of two scenes. Scene order is
# controlled by the contents of the final.txt file, which is an
# ffmpeg concat demuxer input file (see https://trac.ffmpeg.org/wiki/Concatenate#demuxer)
# that might look like this:
#
# --- final.txt ---
# file './mountains.m4v'
# file './cityscape.m4v'
# -------------------
#
# The final M4V will contain the image sequence consisting of the
# files from the mountains/ directory, at the framerate specified by
# mountains/FPS, and then similarly the image sequence from the
# cityscape/ directory.
SHELL := /usr/local/bin/bash
SUBDIRS := $(patsubst %/.,%,$(wildcard */.))
SCENES := $(patsubst %,%.m4v,$(SUBDIRS))
INPUT_TYPE := tiff
all: final.m4v
clean:
rm -f $(SCENES) final.m4v
final.m4v: $(SCENES) final.txt
ffmpeg \
-y \
-safe 0 \
-f concat \
-i final.txt \
-c copy \
-vtag hvc1 \
-f mp4 \
-movflags +faststart \
final.m4v
%.m4v: %/*.${INPUT_TYPE} %/FPS
$(call compile_image_sequence,$*)
$(SUBDIRS): % : %.m4v
.PHONY: all $(SUBDIRS)
define compile_image_sequence
cd $(1) && ffmpeg \
-y \
-framerate $(shell cat ${1}/FPS) \
-pattern_type glob \
-i "*.${INPUT_TYPE}" \
-vcodec libx265 \
-crf 14 \
-pix_fmt yuv420p \
-vtag hvc1 \
-f mp4 \
-movflags +faststart \
-r 60 \
"../$(1).m4v"
endef
ffmpeg \
  -framerate 30 \
  -pattern_type glob \
  -i "*.tif" \
  -vcodec libx265 \
  -vtag hvc1 \
  -crf 14 \
  -pix_fmt yuv420p10le \
  -vf scale=-1:2160 \
  -f mp4 \
  -x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc"
  output-filename.m4v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment