147

Now, I know I can use iTunes to convert music. But it's quite a pain.. All I'm asking is: Is there anything built into OS X or UNIX for converting files? Right now I simply want to convert .mp4 to .mp3..

4
  • 2
    Do you consider open source packages as third party applications ? Or do you want to stick with Apple Products that ships with the OS only . Can you make that clear in your question ?
    – Sairam
    Sep 27, 2011 at 8:37
  • I would like to do it with pre-loaded software. Sep 27, 2011 at 9:00
  • 3
    A comment on the FFmpeg solution (lacking reputation). Output quality control for MP3 can be done via codec:a libmp3lame and qscale:a [0-9] options (see FFmpeg wiki). For instance, to get VBR in a 140-185 kbit/s range use: ffmpeg out.mp3 -i in.mp4 -codec:a libmp3lame -qscale:a 4 This can also be used to reduce MP3 files size.
    – trybik
    Nov 12, 2014 at 13:11
  • @JamesLitewski Please consider choosing the ffmpeg answer as the accepted answer. This would save people time from trying the less useful and lesser-voted answer first (Doug's AppleScripts). Oct 19, 2021 at 12:25

9 Answers 9

21

Doug's AppleScripts for iTunes

including

Convert and Export 2.0

is a collection of useful scripts that you can run from the Finder or the command line. Of course they all use iTunes to process data, but you might find it more convenient than having to use iTunes' GUI and mouse commands to convert files.

4
  • How do you run this script? I see no instructions on the site.
    – coolaj86
    Aug 15, 2012 at 14:57
  • Installation and configuration instructions from the guy providing the scripts are here: dougscripts.com/itunes/itinfo/downloadfaq.php
    – user9290
    Aug 15, 2012 at 18:01
  • 8
    Note to those that don't know: this is not a commandline solution. It puts an icon up in the iTunes toolbar. You must select a collection of songs, then select the script from the dropdown menu. It will default to 256Kbps for AAC. It's not mentioned anywhere in the documentation (that I could find), but you can see it in the screencast in the very last 2 or 3 seconds.
    – coolaj86
    Aug 15, 2012 at 20:40
  • 1
    Going to the Convert and Export 2.0 link, it says "This script will not work on macOS 10.15+". 10.15 Catalina released in 2019 and two years since then we've had Big Sur and Monterey, so this answer no longer applies to >80% of Mac users. Not sure how it should be edited, as I'm not sure there is (with the removal of iTunes) a built-in CLI way of converting audio files in MacOS.
    – johnfernow
    Nov 18, 2021 at 18:02
241

I installed ffmpeg via MacPorts, although it also available via Homebrew (brew install ffmpeg) or download the binary.

To convert something like that, (without worrying about audio quality, which I know nothing about), I just use:

ffmpeg -i input.mp4 output.mp3

Here is an example of how you would convert a .wav file to .mp3 from their website:

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3 

Here is an example of how to find all .wav files larger than 50M, convert them to mp3 and then delete the original wav (aka, batch mode -- alter the find command to create your 'batch')

find . -size +50M -iname *.wav -type f -exec ffmpeg -i {} -codec:a libmp3lame -qscale:a 2 {}.mp3 -y \; -exec /bin/rm {} \;
8
  • Thanks for the easy answer. How to convert list of files? Also does it have serial or parallel options? May 30, 2018 at 12:26
  • 1
    ffmpeg also works to convert ogg to mp3: ffmpeg -i beep.ogg beep.mp3
    – rgkobashi
    Mar 6, 2019 at 8:52
  • @rgkobashi Is this method lossless? Mar 22, 2021 at 15:22
  • @MartinBraun no idea, this is not my expertise area, ffmpeg since pretty solid and well maintained, perhaps you can ask them
    – rgkobashi
    Mar 23, 2021 at 4:35
  • I'm having trouble using the "Edit" feature, so I'll share here: -iname *.wav should be -iname '*.wav', with quotes. Quotes required, else you are relying on bash shell expansion, and files inside folders won't be picked up. Mar 28, 2021 at 2:13
41

OS X does not ship with any MP3 encoder apart from the one in iTunes. For converting to mpeg4 audio you can use the CLI command afconvert (afconvert -h for available options). For example:

afconvert track.aiff -o track.m4a -q 127 -b 128000 -f m4af -d aac

Help for this tool can be found by running "afconvert --help" as "man afconvert" doesn't point to a useful manual page.

3
  • 1
    +1 but I really wish somebody would add a pointer to exhaustive documentation. I have gotten it to work through trial and error and guesswork, but many of the options I should perhaps have been using are very very briefly documented.
    – tripleee
    Mar 7, 2013 at 9:55
  • 3
    The man page is next to useless... :-( May 31, 2015 at 8:06
  • 1
    I found this tutorial to be informative. Valid arguments for data and file formats can be seen using afconvert -hf
    – chb
    Oct 9, 2017 at 2:02
7

afconvert will not write .mp3 files. Even though it is listed in the help as a format:

MacBook-Pro:local user$ afconvert -hf
Audio file and data formats:
    '3gpp' = 3GP Audio (.3gp)
               data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aach' 'aacl' 
                             'aacp' 'samr' 
    '3gp2' = 3GPP-2 Audio (.3g2)
               data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aach' 'aacl' 
                             'aacp' 'samr' 
    'adts' = AAC ADTS (.aac, .adts)
               data_formats: 'aac ' 'aach' 'aacp' 
    'ac-3' = AC3 (.ac3)
               data_formats: 'ac-3' 
    'AIFC' = AIFC (.aifc, .aiff, .aif)
               data_formats: I8 BEI16 BEI24 BEI32 BEF32 BEF64 UI8 'ulaw' 
                             'alaw' 'MAC3' 'MAC6' 'ima4' 'QDMC' 'QDM2' 
                             'Qclp' 'agsm' 
    'AIFF' = AIFF (.aiff, .aif)
               data_formats: I8 BEI16 BEI24 BEI32 
    'amrf' = AMR (.amr)
               data_formats: 'samr' 
    'm4af' = Apple MPEG-4 Audio (.m4a, .m4r)
               data_formats: 'aac ' 'aace' 'aacf' 'aach' 'aacl' 'aacp' 
                             'alac' 'paac' 
    'm4bf' = Apple MPEG-4 AudioBooks (.m4b)
               data_formats: 'aac ' 'aace' 'aacf' 'aach' 'aacl' 'aacp' 
                             'paac' 
    'caff' = CAF (.caf)
               data_formats: '.mp1' '.mp2' '.mp3' 'QDM2' 'QDMC' 'Qclp' 
                             'Qclq' 'aac ' 'aace' 'aacf' 'aach' 'aacl' 
                             'aacp' 'alac' 'alaw' 'dvi8' 'ilbc' 'ima4' 
                             I8 BEI16 BEI24 BEI32 BEF32 BEF64 LEI16 LEI24 
                             LEI32 LEF32 LEF64 'ms\x00\x02' 'ms\x00\x11' 
                             'ms\x001' 'paac' 'samr' 'ulaw' 
    'MPG1' = MPEG Layer 1 (.mp1, .mpeg, .mpa)
               data_formats: '.mp1' 
    'MPG2' = MPEG Layer 2 (.mp2, .mpeg, .mpa)
               data_formats: '.mp2' 
    'MPG3' = MPEG Layer 3 (.mp3, .mpeg, .mpa)
               data_formats: '.mp3'

If you attempt to use it:

/usr/bin/afconvert -d '.mp3' -f MPG3 mysong.MP4 -o mysong.mp3

then an error occurs:

Error: ExtAudioFileSetProperty ('cfmt') failed ('fmt?')

As explained on afconvert MPG3?

'Core Audio can read, but not write, MP3 files...'

I ended up using zpletan's answer and installed ffmpeg

brew install ffmpeg

and the following script, placed in the same directory as the .MP4 files that I want to convert. It also works for files with spaces:

#!/bin/sh -x
for f in *.MP4; do
        /usr/local/Cellar/ffmpeg/2.5.4/bin/ffmpeg -i "$f" "$f.mp3"
#        /usr/bin/afconvert -d '.mp3' -f MPG3 "$f" -o "$f.mp3"
        echo "$f converted"
done
2
  • 1
    Slight variation on the above for example to fix the extensions: <code> for i in *.m4a; do echo "${i%.*}.mp3" ; ffmpeg -i "$i" "${i%.*}.mp3"; done Sep 2, 2019 at 18:10
  • 1
    Another version to convert all mp3 files in a folder (from inside said folder): for f in *.mp3 ; do b=`basename -s .mp3 $f` ; afconvert $f -o $b.m4a -f m4af -d aac ; done
    – saschpe
    Nov 12, 2020 at 11:37
4

There's also the X Lossless Decoder (XLD) - a lossless audio decoder for Mac OS X available as a command-line tool and a GUI app. The GUI version supports MP3 as well as other output formats.

1
  • Link doesn't work.
    – orschiro
    Dec 30, 2020 at 16:22
3

I compiled libav from source to serve this purpose. This is the same package installed by default on recent versions of Ubuntu Linux so tutorials for Ubuntu works almost as-is on OS X with libav.

Here is something I do to convert MP4 (AAC) to MP3 (using LAME) using command line and a makefile (for the sake of parallelism):

for each in *.mp4; do mv "$each" "$(echo "$each" | sed "s/ /./g")"; done
cat << EOF > Makefile
MP4_FILES := $(wildcard *.mp4)
MP3_FILES := ${MP4_FILES:.mp4=.mp3}

all: $(MP3_FILES)

%.mp3: %.mp4
    avconv -i $< -c:a libmp3lame $@
EOF
make -j20

Adjust the number after -j of the make command to about 1.5x your processor core number. My server had 24 cores but -j24 gave me serious IO bottleneck, throttled it back to -j20 and the performance is pretty well balanced.

1
  • Clever hack - making it a Makefile so you can run in parallel!
    – Pryftan
    Nov 29, 2022 at 20:49
2

I hadn't used it for a decade but I love ecasound for converting sound files.

If you have brew set up:

brew install ecasound

ecasound -i input.mp4 -o output.mp3

I haven't tried it with .mp4s myself but it should work.

I just bumped into this question when looking to convert wav into flac.

1

With automator, define a service (Thanks DIGITALHOBBIT'S MUSIC BLOG for the idea): enter image description here

And shell script is the following:

    # -----------------------------
    # DESCRIPTION
    #   Export iTunes Music to another destination to get only MP3 files
    #       - Copy MP3 to Destination
    #       - Convert M4A in MP3 in Destination
    #
    # -----------------------------
    # DATE      : 06-Apr-2017
    # AUTHOR    : JF LE MADEC
    # VERSION   : 1.0
    # -----------------------------

    # Local iTunes Directory
    ITUNDIR="$HOME/Music/iTunes/iTunes Media/Music"

    # binary to convert audio files
    AFCONVERT="afconvert"
    # conversion paramaters
    AFCPARAM="-f mp4f -d aac -b 128000"

    # Audio files suffixes
    # Source
    M4ASUF=".m4a"
    # Destination
    MP3SUF=".mp3"

    # for all directories given in arguments copy music
    for destination in "$@"
    do

        # Copy MP3 files
        find "$ITUNDIR" -name "*$MP3SUF" | while read myMp3File; do
            mp3dir=`dirname "$myMp3File"`
            targetDir="$destination${mp3dir#$ITUNDIR}"
            fileName=`basename "$mp3File"`
            [[ -d "$targetDir" ]] || mkdir -p "$targetDir"
            cp -R "$myMp3File" "$targetDir"
        done

        # Convert and copy M4A files (iTunes native)
        find "$ITUNDIR" -name "*$M4ASUF" | while read m4aFile; do
            #afconvert -f mp4f -d aac -b 128000 "./Vianney/Vianney/11 Le Galopin.m4a"  /Users/jflm/Desktop/output2.mp3
            # Get file info
            m4aDir=`dirname "$m4aFile"`
            targetDir="$destination${m4aDir#$ITUNDIR}"
            fileName=`basename -s $M4ASUF "$m4aFile"`
            # Create parent directories if not exist
            [[ -d "$targetDir" ]] || mkdir -p "$targetDir"

            # Convert audio file directly in destination directory
            $AFCONVERT $AFCPARAM "$m4aFile" "$targetDir/$fileName$MP3SUF"

        done
    done
1
  • Did you mean aac or mp3 in AFCPARAM="-f mp4f -d aac -b 128000"?
    – John N
    Apr 6, 2017 at 16:04
-1

Under linux, I find python-audio-tools does exactly what I want. Unfortunately, I haven't been able to get it to encode to mp3 under OSX. It might work for you, though. It's not at all built into OSX, but it's very lightweight and can convert between pretty much any pair of formats (or just convert to a more compressed file with the same format).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .