3
\$\begingroup\$

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 "$@" >&2
 status=false
}
# Resize a single file
resize() {
 exiftool -p 'Filename:$filename Megapixel:$megapixels Dimension:$imagesize Rotation:$rotation' 1ドル
 if exiftool -if '$imageheight == 1920 && $imageheight == 1080' 1ドル > /dev/null
 then
 fail "-> 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 "-> 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" < /dev/null &&
 exiftool -TagsFromFile "1ドル" '-all:all>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>/dev/null
 fail "-> 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
asked Jan 27, 2020 at 11:50
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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 > /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 "-> 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 "-> Unhandled rotation '$rotation'"; return ;;
esac

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

answered Jan 28, 2020 at 17:02
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.