Skip to content

Instantly share code, notes, and snippets.

@christofluethi
Last active December 1, 2024 18:22
Show Gist options
  • Save christofluethi/646ae60d797a46a706a5 to your computer and use it in GitHub Desktop.
Save christofluethi/646ae60d797a46a706a5 to your computer and use it in GitHub Desktop.
Convert m4a to mp3 on OS X command line using ffmpeg
brew update
brew link yasm
brew link x264
brew link lame
brew link xvid
brew install ffmpeg
ffmpeg wiki:
https://trac.ffmpeg.org/wiki/Encode/MP3
lame option Average kbit/s Bitrate range kbit/s ffmpeg option
-b 320 320 320 CBR (non VBR) example -b:a 320k (NB this is 32KB/s, or its max)
-V 0 245 220-260 -q:a 0 (NB this is VBR from 22 to 26 KB/s)
-V 1 225 190-250 -q:a 1
-V 2 190 170-210 -q:a 2
-V 3 175 150-195 -q:a 3
-V 4 165 140-185 -q:a 4
-V 5 130 120-150 -q:a 5
-V 6 115 100-130 -q:a 6
-V 7 100 80-120 -q:a 7
-V 8 85 70-105 -q:a 8
-V 9 65 45-85 -q:a 9
ffmpeg -i in.m4a -codec:a libmp3lame -qscale:a 1 out.mp3
@mavogel
Copy link

mavogel commented Aug 18, 2017

thx man :) there is a little typo: should be -codec:a
-> ffmpeg out.mp3 -i in.m4a -codec:a libmp3lame -qscale:a 1

@christofluethi
Copy link
Author

thanks for pointing out. fixed.

@atchoo78
Copy link

Thank you for this. I created a macOS "Quick Action" out of this in Applescript/Automator (with "audio file in Finder" as input)

on run {input, parameters}
	tell application "Finder"
		set fileAlias to input as alias
		set fileName to name of fileAlias
		set fileExtension to name extension of fileAlias
		set AppleScript's text item delimiters to "."
		set title to text 1 thru text item -2 of fileName
		set theInputFilePath to POSIX path of fileAlias
		set theOutputFilePath to text item 1 of theInputFilePath
		set convert to do shell script "/usr/local/bin/ffmpeg '" & theOutputFilePath & ".mp3" & "' -i '" & theInputFilePath & "' -codec:a libmp3lame -qscale:a 1"
		set theOutput to theOutputFilePath & ".mp3"
	end tell
        return theOutput
end run

@dajare
Copy link

dajare commented Apr 29, 2021

Great to see this — a real help! One tweak that further refined this for me was needed, since I was getting an error, Trailing options were found on the commandline. My "quality" setting was being ignored as a consequence.

I followed the guidlelines about ordering elements on the ffmpeg syntax structure from this StackOverflow Q&A, and all was good. It only took putting the output file after its options, so tweaking the example given by OP:

ffmpeg -i in.m4a -codec:a libmp3lame -qscale:a 1 out.mp3

@christofluethi
Copy link
Author

Great to see this — a real help! One tweak that further refined this for me was needed, since I was getting an error, Trailing options were found on the commandline. My "quality" setting was being ignored as a consequence.

I followed the guidlelines about ordering elements on the ffmep syntax structure from this StackOverflow Q&A, and all was good. It only took putting the output file after its options, so tweaking the example given by OP:

ffmpeg -i in.m4a -codec:a libmp3lame -qscale:a 1 out.mp3

thanks for heads up. changed it.

@Hyvi
Copy link

Hyvi commented Jul 24, 2022

Thank you for this. I created a macOS "Quick Action" out of this in Applescript/Automator (with "audio file in Finder" as input)

on run {input, parameters}
	tell application "Finder"
		set fileAlias to input as alias
		set fileName to name of fileAlias
		set fileExtension to name extension of fileAlias
		set AppleScript's text item delimiters to "."
		set title to text 1 thru text item -2 of fileName
		set theInputFilePath to POSIX path of fileAlias
		set theOutputFilePath to text item 1 of theInputFilePath
		set convert to do shell script "/usr/local/bin/ffmpeg '" & theOutputFilePath & ".mp3" & "' -i '" & theInputFilePath & "' -codec:a libmp3lame -qscale:a 1"
		set theOutput to theOutputFilePath & ".mp3"
	end tell
        return theOutput
end run
on run {input, parameters}
	tell application "Finder"
		repeat with fileitem in input
			set fileAlias to fileitem as alias
			set fileName to name of fileAlias
			set fileExtension to name extension of fileAlias
			set AppleScript's text item delimiters to "."
			set title to text 1 thru text item -2 of fileName
			set theInputFilePath to POSIX path of fileAlias
			set theOutputFilePath to text item 1 of theInputFilePath
			set convert to do shell script "/usr/local/bin/ffmpeg '" & theOutputFilePath & ".mp3" & "' -i '" & theInputFilePath & "' -codec:a libmp3lame -qscale:a 1"
			set theOutput to theOutputFilePath & ".mp3"
		end repeat
	end tell
	return theOutput
end run

thanks , I change the code with "Get Selected Files in Finder" as input to support multi files (on my laptop , MacOS Monterey version 12.5 )

however, I not know how to work with "audio file in Finder" as input, thanks so much if have your reply @atchoo78

@atchoo78
Copy link

Thank you for this. I created a macOS "Quick Action" out of this in Applescript/Automator (with "audio file in Finder" as input)

on run {input, parameters}
	tell application "Finder"
		set fileAlias to input as alias
		set fileName to name of fileAlias
		set fileExtension to name extension of fileAlias
		set AppleScript's text item delimiters to "."
		set title to text 1 thru text item -2 of fileName
		set theInputFilePath to POSIX path of fileAlias
		set theOutputFilePath to text item 1 of theInputFilePath
		set convert to do shell script "/usr/local/bin/ffmpeg '" & theOutputFilePath & ".mp3" & "' -i '" & theInputFilePath & "' -codec:a libmp3lame -qscale:a 1"
		set theOutput to theOutputFilePath & ".mp3"
	end tell
        return theOutput
end run
on run {input, parameters}
	tell application "Finder"
		repeat with fileitem in input
			set fileAlias to fileitem as alias
			set fileName to name of fileAlias
			set fileExtension to name extension of fileAlias
			set AppleScript's text item delimiters to "."
			set title to text 1 thru text item -2 of fileName
			set theInputFilePath to POSIX path of fileAlias
			set theOutputFilePath to text item 1 of theInputFilePath
			set convert to do shell script "/usr/local/bin/ffmpeg '" & theOutputFilePath & ".mp3" & "' -i '" & theInputFilePath & "' -codec:a libmp3lame -qscale:a 1"
			set theOutput to theOutputFilePath & ".mp3"
		end repeat
	end tell
	return theOutput
end run

thanks , I change the code with "Get Selected Files in Finder" as input to support multi files (on my laptop , MacOS Monterey version 12.5 )

however, I not know how to work with "audio file in Finder" as input, thanks so much if have your reply @atchoo78

Hi! Thanks for the modified code. Sorry I wasn't clear on this. What I meant by that is this:
I created a new document in Automator, chose the "Quick Action" document type and the "AppleScript" action with the actual script. I configured the "Quick Action" like this in Automator:

Screenshot 2022-07-25 at 05 17 14

That's what I meant by

"...with "audio file in Finder" as input"

Andreas 🙂

@seamusdemora
Copy link

I wonder why the size increases so much?

I had an m4a of size: '124943', and the output of the command given above produced an mp3 of size: '518403'
Seems odd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment