-1

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

awvickers
  • 39
  • 7
  • It's a good idea when the source files are 48k and 320Kbps and its just a talking voice/podcast. I can save roughly 60% in file size while not having a real noticeable difference in quality to an average listener. – awvickers Aug 04 '22 at 18:30
  • Ah, that makes sense. I might use Speex if you want something even more optimized for voice. – Charles Duffy Aug 04 '22 at 18:38

1 Answers1

0

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
' {} +
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Using the above in Automator isn't producing any results/files. Maybe I'm approaching it incorrectly. – awvickers Aug 04 '22 at 18:35
  • Hmm. Unfortunately, I don't have Automator at hand... but one approach you might try is writing the above to a file (with a shebang line like `#!/usr/bin/env bash` at the top), setting the file to have +x permissions, and telling Automator to run it. That way we can clearly distinguish between an Automator problem and a problem with the script itself, since you can try running the script at the command line. – Charles Duffy Aug 04 '22 at 18:39
  • I created a file and set it to executable. Getting an error within Automator, "The action “Run Shell Script” encountered an error: “find: : No such file or directory”" – awvickers Aug 04 '22 at 20:17
  • Ahh, so that means `$1` isn't set. Generally, that would be a problem for your original code in the question too. – Charles Duffy Aug 04 '22 at 23:47
  • ...if you start your program from the command line, `/path/to/yourscript /path/to/folder` is basically what usage should look like -- `/path/to/folder` is what becomes `$1`. I can't tell you what the Automator equivalent to that is, though. – Charles Duffy Aug 04 '22 at 23:49
  • Within the Automator quick action, the folder that I select and apply the action to is the argument that is passed to the script. – awvickers Aug 05 '22 at 01:01
  • ...the folder clearly wasn't populated as $1 (within the script's execution context), if you got that error. – Charles Duffy Aug 05 '22 at 01:03
  • To add some logging, consider adding the following line to your script, below the shebang and above the `find` invocation: `exec 2>"$HOME/myscript.log"; set -x; : "$0" "$@"` -- then read `~/myscript.log` to see what it contains. It _should_ have a line like `+ : /path/to/yourscript /path/to/your-directory`, but we'd want to see what it actually has instead. – Charles Duffy Aug 05 '22 at 01:04
  • Well there is no error on my original script. It works just fine. I'm also not a good programmer, so though I appreciate the help I'm not sure what I'm really looking at :/ – awvickers Aug 05 '22 at 01:10
  • Adding the logging, this is what is what is in myscript.log ``` + : recodemp3.sh /Users/Andrew/Desktop/05 + find /Users/Andrew/Desktop/05 -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 ' '{}' + ``` – awvickers Aug 05 '22 at 01:17
  • Also adding your above script as the Automator script (not executing a file) the log file is basically the same... `+ : - /Users/Andrew/Desktop/05 + find /Users/Andrew/Desktop/05 -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 ' '{}' + ` – awvickers Aug 05 '22 at 01:23
  • Okay, so there _really is_ a `$1` set then. Can you show me where the error message shows up? (You can also change `sh -c` to `sh -xc` to get more logs after the child process starts). Also, think about using something like https://gist.github.com/ so we don't need to worry about newlines being eaten by how SO formats comments. – Charles Duffy Aug 05 '22 at 01:47
  • So even hard coding my test dir that contains mp3 files into the script, nothing really happens. There is no error, and automator states that it has successfully run, but the folder doesn't contain a new mp3 file, nor is the old one deleted. Contents of log file https://gist.github.com/mocodev/93ad7e71e1c984e018464ee268854594 – awvickers Aug 05 '22 at 02:37