Question about adapting a batch script to automator

I am a recent convert to the Mac ecosystem, I have a windows batch script from when I was using windows that i would like to adapt to an automator script. It is pretty simple. The goal of is about using ffmpeg to convert media files to the proper WAV format for a sampler I have. Essentially what it does is allow you to right click on a file in a folder, and the script looks through all media files in that folder, then creates a new folder in an exports folder, and converts all the files to 44.1 khz wav files and places them in the few folder. An additional wrinkle of my original script that isn't completely necessary is that it queries you about whether you want to use the original folder name or create a new one.

I wonder if y'all can provide me some guidance for adapting this to a bash script for automator. I am pretty new to bash scripts and not exactly an A+ programmer. I already have simple bash scripts working, doing things like creating a directory or other simple terminal like commands, but I haven't quite figured out things like setting variables or getting user input from a pop up terminal. Thanks for any help you could provide!

@ECHO OFF


for %%I in (.) do set CurrDirName=%%~nxI

set CurrDir="%CD%"

cd C:\Users\warpi\Desktop\Samples\Octatrack Exports

set /p name= Use Parent Folder Name? [Y/N]:
if %name%==y (set input=%CurrDirName%) else (set /p input= Folder Name: )

mkdir "%input%"
echo Directory Created: "%input%"



cd %CurrDir%
for %%i in (*.*) do ffmpeg -i "%%i" -ar 44100 "C:\Users\warpi\Desktop\Samples\Octatrack Exports\%input%\%%~ni.wav"
::for %%i in (*.wav) do ffmpeg -i "%%i" -ar 44100 "C:\Users\warpi\Desktop\Samples\Octatrack Exports\%input%\%%~ni.wav"




PAUSE

Replies

So a bit of an update, I have gotten most of the way to where I want. I still haven't quite been able to get automator to run ffmpeg though. So I have made a command that I can run in terminal if I am in the correct directory which will make a new folder in my export directory, and export all the media files in the current folder to that new created folder. Here is my command for that:

mkdir [Export Folder path]/${PWD##*/};for i in *.*; do ffmpeg -i "$i" -ar 44100 "[Export Folder path]/${PWD##*/}/${i%.*}.wav"; done

This does essentially everything I need for this functionality. But it is a pain to have to navigate in terminal can copy and paste that command in, so I would still ideally like to do it as a script where I can right click the location I want to run it from.

So to start with I am just trying to run a simple ffmpeg command from automator but I keep getting errors. Initially I was having problems getting the automator script to recognize ffmpeg, so I had to give it the path directly. But now I am getting a different error.

Here is the entirety of my automator script:

for i in *.*; do /opt/homebrew/bin/ffmpeg -i "$i" -ar 44100 "${i%.*}.wav"; done

And I get the following error:

"The action “Run Shell Script” encountered an error: “ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers built with Apple clang version 14.0.3 (clang-1403.0.22.14.1) configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon libavutil 58. 2.100 / 58. 2.100 libavcodec 60. 3.100 / 60. 3.100 libavformat 60. 3.100 / 60. 3.100 libavdevice 60. 1.100 / 60. 1.100 libavfilter 9. 3.100 / 9. 3.100 libswscale 7. 1.100 / 7. 1.100 libswresample 4. 10.100 / 4. 10.100 libpostproc 57. 1.100 / 57. 1.100 .: No such file or directory”

For what it's worth this is what my automator window looks like.

Okay, I have gotten it honed down to where I can make it work in one specific situation, but no others.

Basically the goal is to right click in a directory, save that directory to a variable, then cd over to that directory and run ffmpeg there, having the files go to a new folder in an export directory, named the same as the folder you right clicked to run the quick action. The weird thing is that this works perfectly if run on my user folder, named "carsonwalls". It will create a folder in my desired export directory named "carsonwalls" and convert any media files in my user folder to the fight format. But if I try to run it anywhere else I get the error I posted above saying "*.* No such file or directory" Although the folders are created correctly, just the ffmpeg conversion doesn't happen. This seems very strange to me. Here is my automator script:

do
 	parent="$(dirname "$f")"
	#echo $parent  # or whatever
done

cd "$parent"
mkdir "/Users/carsonwalls/Desktop/Samples/Octatrack Exports/${PWD##*/}";for i in *.*; do /opt/homebrew/bin/ffmpeg -i "$i" -ar 44100 "/Users/carsonwalls/Desktop/Samples/Octatrack Exports/${PWD##*/}/${i%.*}.wav"; done

Thanks for any insight you could provide.