Skip to content

Instantly share code, notes, and snippets.

@zyphlar zyphlar/img2mpg.sh
Last active Dec 17, 2015

Embed
What would you like to do?
Batch convert JPEG sequences into MPEG movies
# img2mpg - Will Bradley 2012-2015
# Licensed under Creative Commons Attribution-ShareAlike (CC BY-SA 2.0)
# http://creativecommons.org/licenses/by-sa/2.0/
#
# https://gist.github.com/zyphlar/2629626/
#
# Requirements: imagemagick and ffmpeg (the convert and ffmpeg commands)
#
# This is not a very 'safe' script because it moves and deletes files. You should run it with junk data at first, maybe copying each section into a separate script for testing before trying to run the whole thing.
# This script usually runs as a CRON job; cd to the folder with all the images in it.
# Also, make sure you create a tmp folder within the images folder and chmod it as necessary.
# Example file structure:
# /home/ftpuser/public_html/camera1 (where images are FTP'd to)
# /home/ftpuser/public_html/camera1/tmp (working folder for this script)
# /home/ftpuser/public_html/videos (output folder for MPGs)
# Change this to the root folder just underneath your actual images folder:
FOLDERROOT=/home/ftpuser/public_html
# Support for multiple cameras in one script has been added, call the script with the image folder name as an argument.
if [ -z "$1" ]; then
echo "Usage: ./img2-mpg.sh <camera folder name> (i.e. camera1, camera2)"
exit 1
fi
CAMNAME=$1
if [ ! -d "$FOLDERROOT/$CAMNAME" ]; then
echo "Error, folder doesn't exist: $FOLDERROOT/$CAMNAME"
exit 1
fi
cd $FOLDERROOT/$CAMNAME
# Move files to the tmp folder
mv *.jpg tmp/
cd tmp
# Get rid of empty files so the processing doesn't choke
IFS=$'\n';
for file in $(find . -iname "*jpg"); do
file_size=$(du "$file" | awk '{print $1}');
if [ $file_size == 0 ]; then
echo "Deleting empty file $file with file size $file_size!";
rm -f $file;
fi;
done
# This section just finds all jpg's and uses imagemagick to add the modification date to the top left of the image.
file -i * | grep jpg | awk -F':' '{ print $1 }' | while read IMAGE; do
MODDATE=$(stat -c %y "$IMAGE")
echo $MODDATE >> "$CAMNAME-datelog.csv"
echo Watermarking $IMAGE
convert -pointsize 18 -weight Bold -fill black -stroke white -draw "text 0,18 '$MODDATE'" "$IMAGE" "$IMAGE"
done
# Get rid of spaces in files so ffmpeg doesn't choke
for f in $(find . -iname "* *"); do
echo "Removing spaces from file $f";
mv "$f" "`echo $f | sed -e 's/ /_/g'`";
done
# This section renames all files in the tmp folder (in order of filename, you can modify the ls command below to change this order but sorting by modification date may give unexpected results) -- ffmpeg wants nice regex-y filenames as input.
x=1;
for i in $(ls *jpg); do
counter=$(printf %05d $x);
echo "Renaming $i to proc-$counter.jpg";
mv "$i" proc-"$counter".jpg;
x=$(($x+1));
done
# This section does the jpeg to mpeg processing. It uses the current unix timestamp as a filename so that it doesn't overwrite previous output files and also you can do nifty things with the output like detect the latest video, or chain videos together chronologically.
NOW=$(date +"%s")
ffmpeg -f image2 -i proc-%05d.jpg $FOLDERROOT/videos/$CAMNAME-$NOW.mpg
# This section cleans up the tmp folder (hope nothing went wrong!)
rm proc-*.jpg
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.