Add color for warning and errors
[fgen.git] / fgen.sh
diff --git a/fgen.sh b/fgen.sh
index 385e211..0003fcd 100755 (executable)
--- a/fgen.sh
+++ b/fgen.sh
@@ -20,7 +20,13 @@ trames=1
 dry_run=0
 
 SED=sed
-FFMPEG_OPTS="-hide_banner -loglevel error -y"
+
+F_FMT="%04d"
+
+# When reading a file line by line, if a command inside the loop
+# also reads stdin, it can exhaust the input file.
+# -nostdin: disable ffmpeg interaction on standard input:
+FFMPEG_OPTS="-hide_banner -loglevel error -y -nostdin"
 
 # -limit memory 5000mb -limit disk 5gb
 IM_OPS=""
@@ -31,12 +37,28 @@ case ${OSTYPE} in
         ;;
 esac
 
+COLOR_ERR="\033[0;31m"
+COLOR_WARN="\033[0;33m"
+COLOR_NONE="\033[0m"
+
+function log_info() {
+    echo -e "${COLOR_NONE}${*}"
+}
+
 function log_dbg() {
     if [ x"${debug}" = x"1" ]; then
         echo "${*}"
     fi
 }
 
+function log_warn() {
+    echo -e "${COLOR_WARN}${*}${COLOR_NONE}"
+}
+
+function log_err() {
+    echo -e "${COLOR_ERR}${*}${COLOR_NONE}"
+}
+
 # Arg1: layer
 function get_size()
 {
@@ -57,11 +79,12 @@ function get_label()
     echo "${token}"
 }
 
-# Arg1: line
+# Arg1: label
 function get_frame_id()
 {
-    frame=$(echo "${1}" | grep -e "label:[0-9]\+" | ${SED} "s/.*label:\([0-9]\+\).*,geometry.*/\1/g")
-    echo "${frame}"
+    local token
+    token=$(echo "${1}" | grep -e "^[0-9]\+" | ${SED} "s/^\([0-9]\+\).*/\1/g")
+    echo "${token}"
 }
 
 # Arg1: line
@@ -72,11 +95,12 @@ function get_scene_id()
     echo "${token}"
 }
 
-# Arg1: line
+# Arg1: label
 function get_group_id()
 {
-    group=$(echo "${1}" | grep -e "label:groupe.*" | ${SED} "s/.*label:groupe-\(.*\),geometry.*/\1/g")
-    echo "${group}"
+    local token
+    token=$(echo "${1}" | grep -e "^groupe" | ${SED} "s/^groupe-\(.*\)/\1/g")
+    echo "${token}"
 }
 
 # Arg1: group name
@@ -87,30 +111,73 @@ function get_group_ref()
     echo "${token}"
 }
 
-# Get duplicate from. Ex: "label:dup1-7 planXYZ,geometry..." will return 1
-# Arg1: line
+# Get duplicate from. Ex: "dup1-7 planXYZ,geometry..." will return 1
+# Arg1: label
 function get_dup_from()
 {
-    dup=$(echo "${1}" | grep -e "label:dup.*" | ${SED} "s/.*label:dup\([0-9]\+\)-.*,geometry.*/\1/g")
-    echo "${dup}"
+    local token
+    token=$(echo "${1}" | grep -e "^dup" | ${SED} "s/^dup\([0-9]\+\)-.*/\1/g")
+    echo "${token}"
 }
 
-# Get duplicate to. Ex: "label:dup1-7 planXYZ,geometry..." will return 7
-# Arg1: line
+# Get duplicate to. Ex: "dup1-7 planXYZ,geometry..." will return 7
+# Arg1: label
 function get_dup_to()
 {
-    dup=$(echo "${1}" | grep -e "label:dup.*" | ${SED} "s/.*label:dup[0-9]\+-\([0-9]\+\).*,geometry.*/\1/g")
-    echo "${dup}"
+    local token
+    token=$(echo "${1}" | grep -e "^dup" | ${SED} "s/^dup[0-9]\+-\([0-9]\+\).*/\1/g")
+    echo "${token}"
+}
+
+# Generate frame index name with leading zeroes:
+# Arg1: frame number
+function get_frame_name()
+{
+    local index
+
+    index=$(printf "${F_FMT}" ${1})
+    echo "${dest}/${group}-f${index}.png"
+}
+
+# Arg1: start frame
+# Arg1: end frame
+function insert_empty_frames()
+{
+    local w
+    local start
+    local end
+    local wfname
+
+    start=${1}
+    end=${2}
+
+    for w in $(seq ${start} ${end}); do
+        log_dbg "New frame ID: ${w} (empty)"
+
+        wfname=$(get_frame_name ${w})
+
+        cp ${dest}/background.png ${wfname}
+    done
 }
 
 function generate_video()
 {
+    local png_files
+
     if which ffmpeg 1> /dev/null 2>&1; then
-        if [ -f ${dest}/${group}-f1.png ]; then
-            if [ ${dry_run} -eq 0 ]; then
-                # Conversion vidéo:
-                ffmpeg ${FFMPEG_OPTS} -r ${fps} -start_number 1 -i ${dest}/${group}-f%d.png ${dest}/${group}.mp4
+        set +e
+        png_files=$(ls ${dest}/${group}-f*.png 2> /dev/null)
+        set -e
+
+        if [ x"${png_files}" != x"" ]; then
+            log_dbg "generate_video start"
+            if [ ${dry_run} -eq 1 ]; then
+                touch ${dest}/${group}.mp4
+            else
+                ffmpeg ${FFMPEG_OPTS} -r ${fps} -pattern_type glob -i "${dest}/${group}-f*.png" ${dest}/${group}.mp4
             fi
+
+            log_dbg "generate_video: end"
         fi
     fi
 }
@@ -170,7 +237,7 @@ if [ $# -gt 1 ]; then
 fi
 
 if [ ! -f "${1}" ]; then
-    echo "Error: PSD source file not found"
+    log_err "Error: PSD source file not found"
     exit 1
 fi
 
@@ -203,7 +270,7 @@ if [ ${trames} -eq 1 ]; then
     log_dbg "Background size: ${size}"
 
     if [ x"${size}" = x"" ]; then
-        echo "Error: background layer not found"
+        log_err "Error: background layer not found"
         exit 1
     fi
 
@@ -216,16 +283,17 @@ if [ ${trames} -eq 1 ]; then
     nf=""
     oldnf=""
     files=""
-    group=""
+    group="default"
 
     while read l; do
         scene=$(get_scene_id "${l}")
         label=$(get_label "${l}")
         p=$(get_pos "${l}")
-        nf=$(get_frame_id "${l}")
-        ng=$(get_group_id "${l}")
-        dup_from=$(get_dup_from "${l}")
-        dup_to=$(get_dup_to "${l}")
+
+        nf=$(get_frame_id "${label}")
+        ng=$(get_group_id "${label}")
+        dup_from=$(get_dup_from "${label}")
+        dup_to=$(get_dup_to "${label}")
 
         # Also indicate a new frame, but to be copied from..to:
         if [ x"${dup_from}" != x"" ]; then
@@ -270,22 +338,23 @@ if [ ${trames} -eq 1 ]; then
                 expected_nf=$((${oldnf} + 1))
 
                 if [ ${expected_nf} -ne ${nf} ]; then
-                    echo "Error: invalid frame sequence: ${nf}"
-                    echo "  previous: ${oldnf}"
-                    echo "  expected: ${expected_nf}"
-                    files=""
-                    continue
+                    log_warn "Warning: non-sequential frame sequence: ${nf}"
+                    log_warn "  previous: ${oldnf}"
+                    log_warn "  expected: ${expected_nf}"
+                    insert_empty_frames ${expected_nf} $((${nf} -1))
                 fi
             fi
 
             log_dbg "New frame ID: ${nf}"
 
+            fname=$(get_frame_name ${nf})
+
             if [ ${dry_run} -eq 1 ]; then
-                touch ${dest}/${group}-f${nf}.png
+                touch ${fname}
             else
                 convert -colorspace sRGB -page +0+0 ${dest}/background.png \
                         ${files} -background none -layers merge \
-                        ${dest}/${group}-f${nf}.png
+                        ${fname}
             fi
 
             if [ x"${dup_from}" != x"" ]; then
@@ -296,8 +365,12 @@ if [ ${trames} -eq 1 ]; then
                 for w in $(seq ${dup_start} ${dup_to}); do
                     log_dbg "New frame ID: ${w} (duplicate)"
 
+                    wfname=$(get_frame_name ${w})
+
                     # Use symlink???
-                    cp ${dest}/${group}-f${nf}.png ${dest}/${group}-f${w}.png
+                    cp ${fname} ${wfname}
+
+                    nf=${w}
                 done
             fi