[SOLVED] Shell script not working with KM, but works with Terminal

If I run this with Terminal, the MP3 is normalized as expected:

ffmpeg-normalize "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies.mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies - normalized.mp3"

But if I use this with KM:

/opt/anaconda3/bin/ffmpeg-normalize "$KMVAR_Local__mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "$KMVAR_Local__mp3 - normalized"

I get a bunch of errors:

2023-11-03 21:53:11 Action 15299640 failed: Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/opt/anaconda3/bin/ffmpeg-normalize", line 8, in
sys.exit(main())
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/main.py", line 496, in main
ffmpeg_normalize = FFmpegNormalize(
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_ffmpeg_normalize.py", line 116, in init
self.ffmpeg_exe = get_ffmpeg_exe()
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_cmd_utils.py", line 171, in get_ffmpeg_exe
raise FFmpegNormalizeError(
ffmpeg_normalize._errors.FFmpegNormalizeError: Could not find ffmpeg in your $PATH or $FFMPEG_PATH. Please install ffmpeg from http://ffmpeg.org
2023-11-03 21:53:11 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/opt/anaconda3/bin/ffmpeg-normalize", line 8, in
sys.exit(main())
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/main.py", line 496, in main
ffmpeg_normalize = FFmpegNormalize(
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_ffmpeg_normalize.py", line 116, in init
self.ffmpeg_exe = get_ffmpeg_exe()
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_cmd_utils.py", line 171, in get_ffmpeg_exe
raise FFmpegNormalizeError(
ffmpeg_normalize._errors.FFmpegNormalizeError: Could not find ffmpeg in your $PATH or $FFMPEG_PATH. Please install ffmpeg from http://ffmpeg.org. Macro “Normalize MP3 -1dB (available via Raycast) FINAL” cancelled (while executing Execute Shell Script).

image

Any idea what's happening?
This is the action and that's all I have for that macro

image

For starters, fix the unmatched double quotes in your KM action. Umm, sorry, there was a pop up symbol that was hiding your second double quote. Let me try again. Here's what I saw. You can see why I didn't notice the second quote...

Screenshot 2023-11-03 at 6.05.32 PM

Yeah that is confusing sometimes...

I'm still studying it.

Why did you specify a full path name in the KM action for the program name? Can you try it without the pathname? I'm worried that you may be choosing a different program.

I'm also worried that the environment doesn't match. You may have specified some environment variables in your open Terminal that you didn't specify in your fie that sets up your environment variables for your default Terminal. I'd say that's the most likely culprit.

This might work:

#!/bin/bash

/opt/anaconda3/bin/ffmpeg-normalize "$KMVAR_Local__mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "${KMVAR_Local__mp3} - normalized"

The way you specified the output file "$KMVAR_Local__mp3 - normalized" might be interpreted as two separate arguments because of the space. You may need to enclose the entire output filename in quotes or use an escape character, hence the suggestion above.

The original script (which was for another macro and I was just using ffmpeg, without the -normalize) was without the path and wasn't working then someone here on the forum suggested using it and it worked.

If I try it without, I get this:
2023-11-03 22:13:22 Action 15299669 failed: Execute a Shell Script failed with script error: text-script: line 1: ffmpeg-normalize: command not found
2023-11-03 22:13:22 Execute a Shell Script failed with script error: text-script: line 1: ffmpeg-normalize: command not found. Macro “Normalize MP3 -1dB (available via Raycast)” cancelled (while executing Execute Shell Script).

This is the script I used:

ffmpeg-normalize "$KMVAR_Local__mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "$KMVAR_Local__mp3 - normalized"

@noisneil, that ideas was illuminating and may be one of the solutions to all the problems I have with quotes. Good idea.

1 Like

...if it works!

Then my theory about a path problem (one of your environment variables) is probably correct. You need to look at your path in your open Terminal and see what it is for your spawned terminals.

Do you know how to check your path for both cases?

Step 1. In your open window enter the command: env, and make a close note of your long PATH variable.

Step 2: Run this:

image

Step 3: compare the two results, character by character. I suspect the difference is your problem.

But isn't it what I had already?
"$KMVAR_Local__mp3 - normalized"

I tried your script and I get the same errors...

Should he try replacing the double quotes by single quotes? I sometimes find that works.

Sorry, I don't know what you mean... can you clarify?
What do you mean by "open window"? And "close note" and "long PATH variable"?

Just tried it, no luck...

In the very first sentence of your post, you wrote "if I try this in Terminal." That's the window that I want you to type "env" in, or maybe better, "env | grep PATH".

And "close" is a homonym and may be the reason it was misunderstood. I'll figure out how to reword it.

Can you create and run the Execute Shell Script action above, and consider posting the result here?

The reason I'm pursuing this line of thought is that there's a chance you set up a variable in your open Terminal without knowing that that's required for your program to run, and it hasn't been set up correctly in your environment set file which KM uses to run the shell script.

Just in case, I tested this and it also didn't work

/opt/anaconda3/bin/ffmpeg-normalize "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies copy.mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies copy normalized.mp3"

So it seems that the issue is not with variable portion of the script

@Airy I will try that in Terminal then

This is what I get when I type env | grep PATH

SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS=/Users/dannywyatt/Library/WebKit/Databases
PATH=/opt/anaconda3/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

Your last result also suggests that my concerns about your PATH are valid! Please continue as you are planning...

Please post the result of the KM Execute Shell Script action now, as I illustrated above.

I will do it now.

When I search for that file, I only get one result and that seems to be the path:

There's far more to making a program work than getting its path correct! There are often library paths that need to be in the path too. That's why I'm having you check. Sometimes there are other ENV variables also, which we haven't checked yet.

This is what I get from KM

PATH=/usr/bin:/bin:/usr/sbin:/sbin