Skip to content

Instantly share code, notes, and snippets.

@pasdam
Created January 1, 2023 17:54
Show Gist options
  • Save pasdam/8907a1bd98efdb11285335248d3946e2 to your computer and use it in GitHub Desktop.
Save pasdam/8907a1bd98efdb11285335248d3946e2 to your computer and use it in GitHub Desktop.
#!/bin/sh
DIR=$1
EXTENSIONS_LIST="(MOV|MP4|AVI)"
HANDBRAKE_CLI=HandBrakeCLI
find_files() {
find "$DIR" -type f 2> /dev/null | egrep -i "\.$EXTENSIONS_LIST" | egrep -i --invert-match ".*_s\.$EXTENSIONS_LIST" | egrep --invert-match "/\."
}
get_file_size() {
stat -f%z "$1"
}
gracefully_exit() {
echo ""
if [ $finish -eq 0 ]; then
echo "[INFO] Gracefully exiting the script once the current video has been completed"
echo "[INFO] Press ctrl+c again to stop it immediately, and interrupt the current process"
finish=1
else
echo "[INFO] Forced interruption: terminating the current processing"
echo "[INFO] Cleaning up $new_file"
# TODO: interrupt processing and delete temp file
exit 1
fi
}
finish=0
trap 'gracefully_exit' SIGUSR1 SIGHUP SIGINT SIGTERM INT
find_files | while IFS= read -r old_file; do
old_file_size=$(get_file_size "$old_file")
new_file=$(echo $old_file | sed -E "s/\.$EXTENSIONS_LIST/_s.mp4/i")
if [ ! -f "$new_file" ]; then
echo "[INFO] converting $old_file"
(
$HANDBRAKE_CLI \
--input "$old_file" --output "$new_file" \
--format av_mp4 --encoder x265 --encoder-preset medium --encoder-profile main --encoder-level 4.0 --quality 22 --pfr --rate 30 --two-pass --turbo \
--maxWidth 1280 --maxHeight 720 \
--comb-detect --deinterlace \
--aencoder ca_aac --mixdown stereo --ab 112 \
< /dev/null
) <&0 &
until wait; do :; done
echo "[INFO] converted $old_file"
else
echo "[WARN] skipping $new_file (already exists)"
fi
if [ $finish -eq 1 ]; then
echo "[INFO] Interrupting processing"
exit 0
fi
# new_file_size=$(get_file_size "$new_file")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment