Script to convert .wav to .mp3

Is there a way to have a script convert a .wav file to .mp3 automatically?
I would like to add this to my menu when I right click a file.
I’ve found this other thread, but because I’m not familiar with Applescript, I have no idea how to adapt that to my needs:
https://macscripter.net/viewtopic.php?id=43095

I need the script to convert the selected file and then the mp3 would be saved next to the original .wav file, so the script would have to be written in a way that doesn’t specify a file or output. It’s dependent on the selected file. Hope it makes sense :slight_smile:

Thanks

AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.15

I tested Nigel’s script from the linked thread and it worked fine:

set oldFile to quoted form of "/Users/Robert/Desktop/Fields of Gold.mp3"
set newFile to quoted form of "/Users/Robert/Desktop/Fields of Gold.wav"
do shell script ("/usr/bin/afconvert " & oldFile & " -o " & newFile & " -f 'WAVE' -d 'ulaw' -c 1")

If you don’t want to specify the output file then this also worked:

set oldFile to quoted form of "/Users/Robert/Desktop/Fields of Gold.mp3"
do shell script ("/usr/bin/afconvert " & oldFile & " -f 'WAVE' -d 'ulaw' -c 1")

If you want to convert an mp3 to a wav file and then back to an mp3, I guess you could simply repeat the above do shell script with the desired parameters. I don’t quite understand what you want to do here, though.

As far as implementing this with the right-click menu, I answered this question in one of your other threads.

@tiagorocha1979

I just wrote a script, quick-and-dirty. It allows you to choose between the 4 most common audio formats and converts using the presets depending on the file type of the selected destination format. The script runs on multiple files and formats. The new files are placed in the same folder as the originals. The script is subject to the further embroidering if you deem necessary, ready to be used as the starting point.

The script makes use of a built-in system binary named avconvert. The utility also lets plugging additional parameters in such as sample rate and bit-rate but I omitted it for now.



# created and tested in macOS 10.14 "Mojave" 01/29/2022 by scrutinizer82, first published 01/29/2022 on macscripter.net

property MusicFolder : path to music folder

property AIFF : {_description:"Audio Interchangeable File Format (.aiff)", fourcc:"aiff"}
property ALAC : {_description:"Apple Lossless Audio Codec (.m4a)", fourcc:"m4a"}
property AAC : {_description:"Advanced Audio Coding (.aac)", fourcc:"aac"}
property MP3 : {_description:"MPEG-1/2 Audio Layer III (.mp3)", fourcc:"mp3"}
property FormatsData : {AIFF, ALAC, AAC, MP3}

set EncodeFormats to choose from list {AIFF's _description, ALAC's _description, AAC's _description, MP3's _description} with prompt "Choose Formats To Encode To:" & return with multiple selections allowed

if the result is false then error number -128

set ChosenEncodeFormats to {}
repeat with i from 1 to number of items in EncodeFormats
	set this_item to item i of EncodeFormats
	repeat with aDescRecord in FormatsData
		if this_item is aDescRecord's contents's _description then set end of ChosenEncodeFormats to aDescRecord's contents's fourcc
	end repeat
end repeat


set MyPrompt to ""

repeat with aFormat in EncodeFormats
	if (count EncodeFormats) is 1 then
		set MyPrompt to EncodeFormats's item 1 & "."
	else
		if aFormat's contents is the last item of EncodeFormats then
			set MyPrompt to MyPrompt & ", " & aFormat's contents & "."
		else
			if aFormat's contents is the first item of EncodeFormats then
				set MyPrompt to MyPrompt & aFormat's contents
			else
				set MyPrompt to MyPrompt & ", " & aFormat's contents
			end if
		end if
	end if
end repeat

set SourceAudio to choose file of type {"public.audio"} default location MusicFolder with prompt "Choose audio to convert to " & MyPrompt with multiple selections allowed

try
	repeat with aSource in SourceAudio
		tell application "Finder" to set OrigExtension to "." & (alias (aSource's contents as text))'s name extension
		set SrcPosix to quoted form of aSource's contents's POSIX path
		set Basename to (do shell script "basename -s " & quoted form of OrigExtension & space & SrcPosix)
		set DirName to (do shell script "dirname " & SrcPosix)
		set NewPathNoExt to DirName & "/" & Basename
		
		repeat with FourCCFormat in ChosenEncodeFormats
			
			if FourCCFormat's contents is "aiff" then
				set EncodePreset to "PresetAppleProRes422LPCM"
			else if FourCCFormat's contents is "m4a" then
				set EncodePreset to "PresetAppleM4A"
			else
				set EncodePreset to "PresetAppleM4ViPod"
			end if
			
			do shell script "avconvert  -q -p " & EncodePreset & " -s " & SrcPosix & " -o " & quoted form of (NewPathNoExt & "." & (FourCCFormat's contents)) & " -af " & (FourCCFormat's contents)
		end repeat
		
	end repeat
on error errMess number errNum
	tell application "System Events" to set the clipboard to errMess & return & errNum
	display alert "Error " & errNum message errMess & return & "_______________" & return & "The error message is in the clipboard." as critical
end try

Model: MacBook Pro 9,1 (mid-2012 15") Core i7 2.3 GHz, 16 GB RAM, 1TB SSD
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14