ffmpeg batch script for video resizing - improved I posted a question here ffmpeg batch script for video resizing but I forgot the issue of the rotation and the video files were scaled with wrong dimensions.

Below is the improved version of the script

#!/bin/bash

set -eu


status=true
fail() {
    echo "$@" &gt;&amp;2
    status=false
}

# Resize a single file
resize() {
    exiftool -p 'Filename:$filename Megapixel:$megapixels Dimension:$imagesize Rotation:$rotation' $1
    if exiftool -if '$imageheight == 1920 &amp;&amp; $imageheight == 1080' $1 &gt;     /dev/null
    then
        fail "-&gt; No need to convert"
        return
    fi

    filename="$(date +%s)".mp4
    rotation=$(exiftool -p '$rotation' $1) 

    if [ "${rotation}" -eq 0 ] || [ "${rotation}" -eq 180 ]
    then
        scaling="-1:720"
    elif [ "${rotation}" -eq 90 ] || [ "${rotation}" -eq 270 ]
    then
        scaling="720:-1"
    else
        fail "-&gt; Unhandled rotation"
        return
    fi

    if 
        ffmpeg -v error -stats -i "$1" -map_metadata 0 \
               -vf scale="$scaling" -c:v libx264 -crf 23 \
               -c:a copy "$filename" &lt; /dev/null &amp;&amp;
            exiftool -TagsFromFile "$1" '-all:all&gt;all:all' \
                     -overwrite_original "$filename"
    then
        # success
        rename 's/.mp4/.success.mp4/' $1 
        mv "$filename" $1
        true
    else
        rename 's/.mp4/.error.mp4/' $1 
        # failed; destroy the evidence
        rm -f "$filename" 2&gt;/dev/null
        fail "-&gt; Failed to convert"
    fi
}

[ $# -gt 0 ] || fail "Usage: $0 FILE FILE..."

for arg
do
    if [ -f "$arg" ]
    then
        resize "$arg"
        echo
    elif [ -e "$arg" ]
    then
        fail "$arg: not a plain file or directory"
    else
        fail "$arg: file not found"
    fi
done

exec $status # true or false
Is it intended that date is executed separately for each input, or should all converted files have consistent names (for that use of the script)?  It's certainly worth a comment explaining your choice.

I'm not a fan of the big space in the redirection &gt;     /dev/null - that makes it harder to parse.

This if/else chain looks a little clumsy:


if [ "${rotation}" -eq 0 ] || [ "${rotation}" -eq 180 ]
then
    scaling="-1:720"
elif [ "${rotation}" -eq 90 ] || [ "${rotation}" -eq 270 ]
then
    scaling="720:-1"
else
    fail "-&gt; Unhandled rotation"
    return
fi



When testing a single value against various possibilities, it tends to be neater using case:

case "$rotation" in
    0|180) scaling="-1:720" ;;
    90|270) scaling="720:-1" ;;
    *) fail "-&gt; Unhandled rotation '$rotation'"; return ;;
esac


(I also added extra information in the failure case, that may help diagnose problem input files).