kjen.dk

open source

Time-lapse photography

If you need a linux time-lapse script you may copy the script below that I have written for converting a series of jpg images to a video. I know that there are plenty of software packages that provides this feature but I prefer to control all the parameters directly without having to select them in a user interface each time. I have tested the script under Ubuntu 16.04.

#! /bin/bash
# mkTimeLapse creates a time-lapse move from a source of .jpg files.
# Copyright 2009-2017 Kjeld Jensen <kj@kjen.dk> http://kjen.dk
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
#
# Remember: sudo apt install ffmpeg
#
## Setup ##
DIR_IMAGES=/home/kjeld/Desktop/time_lapse_img # don't add a / here
DIR_TEMP=/home/kjeld/Desktop/time_lapse_tmp # make sure it exists

IMG_DUPLICATES=1 # was used before ffmpeg could handle variable frame rates
V_FILE=video.mp4
V_TITLE="My Time-Lapse"
V_AUTHOR="Kjeld Jensen"
V_CR="Copyright 2017 Kjeld Jensen"
V_COMMENT="http://kjen.dk"
FRAME_SIZE="1200x900"
FRAME_RATE=60
BIT_RATE=4000k
FILE_LIST=images_list.txt

## Script ##
echo Copying files...
rm -rf $DIR_TEMP
rm $V_FILE
mkdir $DIR_TEMP

# Create a list of all files sorted by modification time
echo Creating sequential numbering...
ls -t --reverse $DIR_IMAGES > $FILE_LIST

# Copy renamed files to the tmp directory to create a sequential numbering
N=1
while [ true ]
do
	# Read next file path from the list
	read LINE
	if [ $? -ne 0 ] # quit if end of list
	then
		break
	fi

	# this loop could probably be rewritten much simpler, but it works for now
	for (( C=1; C<= $IMG_DUPLICATES; C++ ))
	do
		# Generate the next image file name
		FILE_IMG="img"
		D=1
		for (( I=1; I<5; I++ )); do
		D=`expr $D \* 10`
			if [ `expr $N / $D` -eq 0 ]
			then
			FILE_IMG=${FILE_IMG}0
			fi
		done
		FILE_IMG=${FILE_IMG}${N}.jpg
		echo $FILE_IMG

		# Copy the image
		cp $DIR_IMAGES/$LINE $DIR_TEMP/$FILE_IMG

		# Now increment the counter
		N=`expr $N + 1`
	done
done < $FILE_LIST

# Encoding the movie
echo Encoding movie...

ffmpeg -framerate $FRAME_RATE -i $DIR_TEMP/img%05d.jpg -s $FRAME_SIZE -b:v $BIT_RATE -c:v libx265 -metadata title="$V_TITLE"  -metadata author="$V_AUTHOR" -metadata copyright="$V_CR" -metadata comment="$V_CR $V_COMMENT" $V_FILE

# Clean up
echo Cleaning up...
rm $DIR_TEMP/*.jpg
rm $FILE_LIST
echo Finished
## End of script ##