animation/fourcell.sh

119 lines
3.0 KiB
Bash

#!/bin/bash
#set -e
if [[ "${1}" == "" ]]; then
echo "Please provide a template JSON"
fi
if [[ "${2}" == "" ]]; then
echo "Please provide one video or directory of images"
exit 2
fi
if [[ "${3}" == "" ]]; then
echo "Please an output directory for pages"
exit 3
fi
if [ ! -d "${3}" ]; then
echo "${3} output directory does not exist"
exit 4
fi
if [ -f "${2}" ]; then
echo "Exporting frames from ${2}..."
OUTPUT=$(mktemp -d)
ffmpeg -y -re -i "${2}" \
-q:v 1 -qmin 1 -qmax 1 \
"${OUTPUT}/frame_%06d.tif"
else
if [ -d "${2}" ]; then
OUTPUT="${2}"
else
echo "Argument ${2} is neither video nor directory"
exit 43
fi
fi
img_name () {
filename=$(basename "${1}")
filename="${filename%.*}"
echo "${filename}.png"
}
echo "Using frames in ${OUTPUT}..."
NAME=$(basename "${OUTPUT}")
DIGITS=6
SCALE=$(echo "scale=$DIGITS; 323/404" | bc)
OUTER=$(echo "scale=$DIGITS; 374/404" | bc)
W=$(cat "${1}" | jq -r ".width")
H=$(cat "${1}" | jq -r ".height")
DPI=$(echo "${W}/8.5" | bc)
FRAMES=$(mktemp)
RATIO=$(bash fourcell/template_ratio.sh "${1}")
TMP=$(mktemp -d)
echo "DPI: ${DPI}"
ONEFRAME=$(ls -1 "${OUTPUT}"*.{jpg,jpeg,JPG,JPG,png,PNG,gif,GIF,tif,tiff,TIF,TIFF} 2> /dev/null | head -n1)
DIM=$(identify -ping -format '%w %h' "${ONEFRAME}")
WIDTH=$(echo ${DIM} | awk '{print $1}')
HEIGHT=$(echo ${DIM} | awk '{print $2}')
R=$(echo "scale=${DIGITS};${HEIGHT}/${WIDTH}" | bc)
OW=$(echo "scale=${DIGITS};${DPI}*(404/96)" | bc | awk -F'.' '{print $1}')
IW=$(echo "scale=${DIGITS};${OW}*${SCALE}" | bc | awk -F'.' '{print $1}')
IH=$(echo "scale=${DIGITS};${IW}*${R}" | bc | awk -F'.' '{print $1}')
GUIDE="${TMP}/guide_$(date +%s).png"
bash fourcell_guide.sh --dpi ${DPI} --dimensions "${IW}x${IH}" "${GUIDE}"
echo "${GUIDE}" > "${FRAMES}"
ls -1 "${OUTPUT}"*.{jpg,jpeg,JPG,JPG,png,PNG,gif,GIF,tif,tiff,TIF,TIFF} >> "${FRAMES}" 2> /dev/null
FC=$(wc -l < "${FRAMES}")
PAGES=$(echo "(${FC}/4)+1" | bc)
for ((i=0;i<${PAGES};++i)); do
f1=$(sed -n "$(((i*4)+1))p" < "${FRAMES}")
f2=$(sed -n "$(((i*4)+2))p" < "${FRAMES}")
f3=$(sed -n "$(((i*4)+3))p" < "${FRAMES}")
f4=$(sed -n "$(((i*4)+4))p" < "${FRAMES}")
args=""
if [[ "${f1}" != "" ]]; then
name=$(img_name "${f1}")
dest="${TMP}/${name}"
bash fourcell/white_fourcell.sh $DPI $RATIO "${f1}" "${dest}"
args="${args} ${dest}"
else
continue
fi
if [[ "${f2}" != "" ]]; then
name=$(img_name "${f2}")
dest="${TMP}/${name}"
bash fourcell/white_fourcell.sh $DPI $RATIO "${f2}" "${dest}"
args="${args} ${dest}"
fi
if [[ "${f3}" != "" ]]; then
name=$(img_name "${f3}")
dest="${TMP}/${name}"
bash fourcell/white_fourcell.sh $DPI $RATIO "${f3}" "${dest}"
args="${args} ${dest}"
fi
if [[ "${f4}" != "" ]]; then
name=$(img_name "${f4}")
dest="${TMP}/${name}"
bash fourcell/white_fourcell.sh $DPI $RATIO "${f4}" "${dest}"
args="${args} ${dest}"
fi
bash apply_image.sh ${NAME} ${3}/${NAME}_$i.tiff "${1}" ${args}
done
echo "Wrote ${3}/${NAME}_$i.tiff"
# x determine dpi
# convert frame to white_fourcell
rm "${FRAMES}"
rm -rf "${TMP}"