Third Party > Other Utilities

Shell script for converting progressive JPEGs to baseline

<< < (2/2)

bobba_mosfet:

--- Quote from: gevaerts on August 24, 2018, 04:17:08 PM ---Just for the record, newlines are perfectly valid in filenames, so that script is still breakable :)

--- End quote ---

See the canonical response for issues with iterating the output of find here:
https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29


--- Quote ---If you're using bash, then you have two additional options. One is to use GNU or BSD find's -print0 option, together with bash's read -d '' option and a ProcessSubstitution:

--- Code: ---while IFS= read -r -d '' file; do
  some command "$file"
done < <(find . -type f -name '*.mp3' -print0)
--- End code ---

--- End quote ---

Are you using bash as your shell? If so I would drop the backticks because of this: http://mywiki.wooledge.org/BashFAQ/082

I would rewrite the script to make it a little more solid like this:

--- Code: ---#!/usr/bin/env bash
 
function process_jpeg()
{
img="$1"
idout="$(identify -verbose "$img" | grep -i interlace | grep -i none)"

if [[ -z "$idout" ]]
then
echo "-------------------------"
echo "$img is progressive"
echo "....making copy of original with .prog extension"
/bin/cp -f "$img" "$img.prog"
echo "....converting to baseline"
convert "$img" -interlace none "$img"
echo "....done!"
fi
}

command -v identify >/dev/null 2>&1 || { echo >&2 "identify is required, but not installed.  Aborting."; exit 1; };

while IFS= read -r -d '' file; do process_jpeg "$file"; done < <(find "$PWD" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0)

--- End code ---

I also highly recommend using https://github.com/koalaman/shellcheck

Edit: Just realised this was from 2018... time to make coffee

cereal_killer:
Thanks for the rewrite and the additional information and tools bobba_mosfet. I'll update the wiki.

Navigation

[0] Message Index

[*] Previous page

Go to full version