Any Bash Nerds Out There?

ben

Registered
Hi, from recent threads it's become clear we have some experts in Mac hardware out there - let's see if someone can give a hand with software.

I need some help with text variable handling. Specifically, I am looking to put together a bash script that will allow me to drag and drop a file onto the script icon, which will invoke ffmpeg, convert the file to a different sound format, and leave the outputted file in the same location as the original, and with the same name - but a different extension.

I now have a DOS Windows bat file that does this, which means that to get this particular task done, I need to switch into Windows. I normally have Parallels running, which means I can download the file, move over to Windows, access the file I've just downloaded on the macOS side, and once the job is done, it's on the Mac in the same folder as before - but I'd still much rather have something I could run on the Mac itself.

Here's the relevant code from my batch file:

Code:
set Input="%1"
set Output="%Input:m4a=wav%"

ffmpeg -i "%Input%" -acodec pcm_s16le -ac 1 -ar 8000 %Output%
This takes the name of the input file (which I've dragged onto the program shortcut), makes a new variable with the same name but replacing the extension "m4a" with "wav", and directs ffmpeg to convert the m4a file to wav (PCM mono, 8000, 16) and assign thereto the new variable as a filename.

Anybody knows how to do this in bash? Checked Stack Overflow et al with no success. Maybe some resident nerds here?

Thanks all!
 

BAJeffpat

Registered
I have no idea, but I recommend employing Cunningham's law:

"The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer."
 

camel

Registered
I don't know how to make it where you can drag a file, but here's a script that manipulates the filename from .m4a to .wav:

Code:
!/bin/sh
input=$1
output=`echo $input | sed -e "s/.m4a/.wav/"`
echo Input = $input
echo Output = $output
#add your ffmpeg command line here
Be sure to get the backticks (`) right, those aren't apostrophes. You can remove the two "echo" statements, they're just for debugging.
Also be sure to give this file the execute bit. If you call the script foo.sh, then you want to do this from the command line:

chmod a+x foo.sh

Then you can run ./foo.sh from the command line.

For drag-drop, this may help - I was able to make the App, but I don't know how to drag a file onto the app:
http://stackoverflow...-onto-a-sh-file
 
  • Like
Reactions: ben

ben

Registered
OK thanks a lot - half the problem solved.

In DOS/Windows, you can refer to any dropped argument as %1, %2 and so on - haven't yet figured out how to do that on the Mac.

Wish I were growing up around Macs/Linux when I had the time and interest to learn these things...
 

camel

Registered
OK thanks a lot - half the problem solved.

In DOS/Windows, you can refer to any dropped argument as %1, %2 and so on - haven't yet figured out how to do that on the Mac.

Wish I were growing up around Macs/Linux when I had the time and interest to learn these things...
In bash, it's $1, $2, etc, for command-line parameters. You're only missing the drag & drop part, and that stackoverflow link makes me think it's possible.
 
  • Like
Reactions: ben
Top