shell Automator script to re-encode MP3 using LAME and overwriting original files

151 Views Asked by At

I'm looking to run through a parent folder that has many sub folders of MP3 files and re-encode at a lower sample and bit rate. I'm doing this in Automator on my Mac using a Run Shell Script workflow and making it a Finder action.

Currenlty I can pass a folder and the script works but I'm getting new MP3 files with a double *.mp3.mp3 extension.

I know LAME cannot overwrite existing files, but I'm having trouble creating the command that would delete the original files when complete, then remove the double .mp3 extension off of the new files.

find "$1" -name "*.mp3" -execdir /usr/local/bin/lame -a  --resample 44.1 -b 48 {} \;

I came across this lame - Overwrite existing files but cannot get it to work within Automator.

Automator Screenshot

Quick Action through Finder

1

There are 1 best solutions below

13
Charles Duffy On

If you want to do extra logic, like deleting files, make find launch a shell to do it in.

find "$1" -name "*.mp3" -execdir /bin/sh -c '
  for arg in "$@"; do
    if /usr/local/bin/lame -a  --resample 44.1 -b 48 "$arg"; then
      mv -- "$arg.mp3" "$arg" # success
    else
      rm -- "$arg.mp3" # failure
    fi
  done
' {} +