Can't convert audio to video with ffmpeg (it works with Terminal) [SOLVED]

I am able to use the same script (replacing the variables) in Terminal, but KM throws me an error.

This is the script that works in Terminal:

ffmpeg -f lavfi -i color=c=blue:s=1280x720 -i /Users/dannywyatt/My\ Files/Inbox\ Global/_Focus/Eu\ vou\ pra\ Costa/Eu\ vou\ pra\ Costa.mp3 -shortest -fflags +shortest /Users/dannywyatt/My\ Files/Inbox\ Global/_Focus/Eu\ vou\ pra\ Costa/output.mp4

+test.kmmacros (27 KB)

The error I get is this:

Almost assuredly, the issue is with the path and quoting. You can see it in the error output, too—about halfway down:

File Path: No such file or directory

As a simple test, try this Execute a Shell Script action in place of yours:

echo "audio file path:"
echo "$KMVAR_Local__Audio_File_Path"
echo "destination path:"
echo "$KMVAR_Local__Destination_Path"

Is the output what you'd expect?

EDIT: As this is v11, make sure the variables are included in the action by clicking the small triangle to the left.


1 Like


Yes, that is selected by default.

I think I know the issue: even though I use quotes outside the variables, the final script is not including them, so even though I'm using "$KMVAR_Local__AudioFilePath" the quotes are removed so a path that has spaces will throw an error, because it's not inside quotes. So that clarifies one question I had which is: "are those quotes just for the variable itself and are not used in the script?" to which the answer is yes, so that's good to know.

Now how can I add quotes to the script itself? I tried using quotes twice ""$KMVAR_Local__AudioFilePath"", but that seems to be removed as well.

I also saw that you used echo to show some information inside a shell script action, which is something I wanted to do so many times. So I guess that whenever I want to see how the final script is being "interpreted", I can just add echo "" and the whole script goes inside the quotes, right? At least I was trying it now and it seems that this is how it's done, but can you confirm it? This is a great tip :slight_smile:

@griffman for the quotes issue I can always use a Search and Replace and replace all spaces with \[space] , but I would still like to understand how to add the quotes to the script itself, if you know how it's done?

@griffman ok I was doing it wrong. For double quotes I was trying to escape the first one like this \"", but after checking StackExchange, I saw that it's done like this: "\"
So the path should be something like this:

I will try it now and see if it works

Ok so I was able to make the script look the way it's supposed to be and I even used it with Terminal and it converted it. So the final script is this:

ffmpeg -f lavfi -i color=c=black:s=1280x720 -i "/Users/dannywyatt/My Files/Inbox Global/Car starting sound effect.mp3" -shortest -fflags +shortest "/Users/dannywyatt/My Files/Inbox Global/Video Name Here.mp4"

Running this in Terminal created the video.
But when I use the same script in KM, I get an error

Here's the shell action script:

/opt/anaconda3/bin/ffmpeg -f lavfi -i color=c=black:s="$KMVAR_Local__Video_Size" -i "\"$KMVAR_Local__Audio_File_Path\"" -shortest -fflags +shortest "\"$KMVAR_Local__Destination_Path"/"$KMVAR_Local__Video_Name".mp4\"

I think the issue is with this section:

I get this error:
2024-03-27 23:42:45 Action 15776312 failed: Execute a Shell Script failed with script error: text-script: line 1: unexpected EOF while looking for matching `"'
text-script: line 2: syntax error: unexpected end of file

This is still confusing me, the escaping of the quotes...
If the quotes close to the variable are used for the variable itself such as

then when I want to include the quotes in the script, wouldn't it make sense to use this?
instead of

So my brain is really at that point of melting...
I can't seem to understand what's going on
a is for audio
d is for destination path
v is for video name
(I'm literally too tired to type at this point, with all this frustration...

So when I use echo it seems to show me the right script, but when I remove echo, the first quote and the last quote, like this:

Then I get errors like this one:
"/Users/dannywyatt/My Files/Inbox Global/Car starting sound effect.mp3": No such file or directory

Sorry, I was AFK for a while. I think you're making this overly complicated; what you had should "just work." As a demo, I made a simpler version of you macro that just reverses a video:

It works perfectly in Keyboard Maestro. I think you may have some sort of permissions issue in Keyboard Maestro relative to your home folder. As a test, replace your shell script with this:

ls "$KMVAR_Local__Audio_File_Path"

Does that simple command work?


I'm just trying to understand how it actually works, because for example, in Terminal if a path contains spaces, we need to either put the path inside double quotes or we have to escape the spaces, right?

Now, with a semi-good night sleep, I was able to sit down and perform a few more tests and some things are a bit clearer, for example, I thought that when we use $KMVAR we always need the double quotes, no matter what, even if the path does not have any spaces. From my tests I noticed that this is not the case. I tried 4 different versions:
1 - Path WITH spaces, your ls script WITH quotes → no error
2 - Path WITH spaces, your ls script WITHOUT quotes → ERROR
1 - Path WITHOUT spaces, your ls script WITH quotes → no error
2 - Path WITHOUT spaces, your ls script WITHOUT quotes → no error

So this showed me that the double quotes around $KMVAR is just for paths with spaces. I think using quotes as a habit is more of a way to prevent potential errors, so if we always use double quotes, we are always including both options, no matter what.

What I noticed last night was that when I tried the echo option and I included the quotes, the displayed result would show me without quotes. That made me assume that the script would read it without quotes, throwing an error, because of what I just said at the top about using quotes OR escaping the spaces.

Another thing I noticed / learned from my tests today is that $KMVAR always needs the underscores when there's spaces in the variable. I wrongly thought that what I had in the script should be exactly the same as the variable itself, which is not the case. What I mean is that I can still have variables with spaces such as Local__Video Path, but then the script needs to be $KMVAR_Local__Video_Path. I thought I needed to actually rename the variables to Local__Video_Path as well in order for it to work, but no need to. Only the $KMVAR needs the underscores. I got that info from here

So I just performed a final test and it works! :slight_smile:
I guess the issue here was really what echo was showing me. It removes the quotes around the paths, making me think that the script would fail. Let me show you what I got:


You can see that I have the quotes around all KMVAR instances.
Then my prompt:


And now the window where you can see that the quotes are gone:


This made me think that it would throw an error, that's why I was adding the "\" and all that. If I copied this result window script and pasted it in Terminal, it would fail, hence the escaped quotes. Hope it makes sense what I'm trying to explain?

Thank you for your help!

One question: is there a way to use echo but where I can see the actual script including the quotes around paths? I mean, now I know that in this particular case the paths are good to go if I use the quotes around $KMVAR, but for other scripts I would like to see the script as is, to avoid confusion.

So just to make it clear, because now I think I see why my first script was not working: it seems that the issue was just that I didn't replace the spaces present in the variables with underscores in the $KMVAR instances. I think it was just that, now that I looked at my initial post.

You should consider it a rule, actually—if it's double-quoted, it will be interpreted correctly, and you don't have to worry about it.

It does, though I can't really tell you why you don't see them. I think the reason why is that they did their job: They simply passed on the value of the variable in the quotes. With an echo, you just see the output of that variable. But used in a command, the quotes are seen and the path is correctly handled, even with spaces. (This is basically what the wiki says about variables in shell scripts and how to handle paths.)

This would work:

echo \"$KMVAR_local_theVariable\"


Yes, that's what I thought. No need to always think about it :wink:

I see what you mean.
That's what was making me so confused and frustrated last night... I couldn't figure it out no matter what.

Thank you. I was looking for a solution that would allow me not to alter the original code to avoid (even more) the possibility of confusion. I think I will just have to have in mind that quotes are removed in that window. But thank you for sharing that. Now that I know I have a way of seeing the final script, that makes it so much easier! :+1:

One step at a time! Truly appreciate it, Rob!