Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Rockbox Ports are now being developed for various digital audio players!

+  Rockbox Technical Forums
|-+  Third Party
| |-+  Other Utilities
| | |-+  Shell script for converting progressive JPEGs to baseline
« previous next »
  • Print
Pages: [1]

Author Topic: Shell script for converting progressive JPEGs to baseline  (Read 7127 times)

Offline cereal_killer

  • Member
  • *
  • Posts: 430
Shell script for converting progressive JPEGs to baseline
« on: August 24, 2018, 09:31:08 AM »
I found this shell script to convert progressive JPEG files to baseline: https://www.codeproject.com/Articles/438021/Shell-script-for-converting-progressive-JPEGs-to-b. This is much quicker than batch converting with irfanview. But unfortunately it won't work with file/folder names with spaces.

Code: [Select]
for img in `find . -name "*" | egrep *\.jpe?g$`
do
  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!"
  #else
    #echo "$img is non-progressive"
  fi
done

I already tried to insert " in line 3, but it still doesn't work. My scripting knowledge is close to none. The Author hasn't responded to my inquiry yet. Does anyone have a fix for this? I think the part with copying the file with a new file extension is unnecessary. This surely would be a helpful tool and is worth to be added to https://www.rockbox.org/wiki/AlbumArt.
I appreciate any help.
Logged

Offline Bilgus

  • Developer
  • Member
  • *
  • Posts: 876
Re: Shell script for converting progressive JPEGs to baseline
« Reply #1 on: August 24, 2018, 11:10:12 AM »
I'm not the most versed with shell scripts but I believe the problem is with what the shell considers a valid field separator
by default I think it is SPACE|TAB|NEWLINE so it needs to be set to just newline


Code: [Select]
IFS_ORIG=$IFS #save original IFS will restore at end
set -f #Disable filename expansion (globbing).
IFS=$'\n'
for img in `find . -name "*" | egrep *\.jpe?g$`
do
  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!"
  #else
    #echo "$img is non-progressive"
  fi
done
set +f
IFS=$IFS_ORIG
Logged

Offline cereal_killer

  • Member
  • *
  • Posts: 430
Re: Shell script for converting progressive JPEGs to baseline
« Reply #2 on: August 24, 2018, 01:35:53 PM »
Thank you so much Bilgus, now it works perfectly. I'll add this to the wiki, maybe someone else will find this usefull.
Logged

Offline gevaerts

  • Developer
  • Member
  • *
  • Posts: 1066
Re: Shell script for converting progressive JPEGs to baseline
« Reply #3 on: August 24, 2018, 04:17:08 PM »
Just for the record, newlines are perfectly valid in filenames, so that script is still breakable :)
Logged

Offline cereal_killer

  • Member
  • *
  • Posts: 430
Re: Shell script for converting progressive JPEGs to baseline
« Reply #4 on: September 03, 2018, 11:37:27 AM »
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 :)

I hope that this isn't the case very often. If there are newlines in filenames, I hope that the user has the knowledge to adjust this script. It is working fine for me and hopefully for many others, so I'll put it on the wiki.

Thank you Bilgus for the help.
Logged

Offline rockbox_dev123

  • Member
  • *
  • Posts: 74
Re: Shell script for converting progressive JPEGs to baseline
« Reply #5 on: April 13, 2019, 11:48:27 AM »
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 :)

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: [Select]
while IFS= read -r -d '' file; do
  some command "$file"
done < <(find . -type f -name '*.mp3' -print0)

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: [Select]
#!/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)

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

Edit: Just realised this was from 2018... time to make coffee
« Last Edit: April 13, 2019, 11:58:15 AM by bobba_mosfet »
Logged

Offline cereal_killer

  • Member
  • *
  • Posts: 430
Re: Shell script for converting progressive JPEGs to baseline
« Reply #6 on: April 22, 2019, 02:48:46 PM »
Thanks for the rewrite and the additional information and tools bobba_mosfet. I'll update the wiki.
Logged

  • Print
Pages: [1]
« previous next »
+  Rockbox Technical Forums
|-+  Third Party
| |-+  Other Utilities
| | |-+  Shell script for converting progressive JPEGs to baseline
 

  • SMF 2.0.19 | SMF © 2021, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.035 seconds with 22 queries.