Use OS X 10.5 to work with audio in the Terminal

Rob Griffiths
2 February, 2009
View more articles fromthe author

With the release of OS X 10.5, command line aficianados gained a few new tools for working with their music files: afplay will play back (most) audio files, afinfo will return information about a specified audio file, and afconvert will convert audio files from one format to another. In previous versions of OS X, you could play audio via the Terminal, but you had to do it by using AppleScript calls (using osascript) to iTunes. Now you can work with most audio files natively, without launching iTunes.

I say “most” audio files because there’s one large exception to the rule—protected iTunes Store music will not play back with afplay. With the advent of iTunes Plus, however, this shouldn’t be much of a limitation going forward. If you’re interested in playing protected music tracks in Terminal, check out the freeware play, which will play all QuickTime-compatible audio files, including protected music (assuming the Mac doing the playback is authorised to play those tracks).

But enough with the digression; back to the new OS X 10.5 audio commands. There’s not much in the way of help available for these new commands; their man pages are pretty much empty. You can get more help for afplay and afconvert by running them with the -h option; here’s a bit of the help for afconvert, for instance:

$ afconvert -hUsage:afconvert [option...] input_file [output_file]Options: (may appear before or after arguments)    { -f |—file } file_format:        '3gpp' = 3GP Audio (.3gp)                   data_formats: 'aac ' 'samr'         '3gp2' = 3GPP2 Audio (.3g2)        ...

Thankfully, usage of all three commands is relatively straightforward. To play an audio file, type afplay "/full/path/to/file". Once a song starts, you can stop it by pressing Control-C to terminate the afplay command. (If you’d like to close the Terminal window after starting a song, you can do so by appending & exit after the path to the audio file. Note that this will make it tougher to stop playback; you’ll have to open a new Terminal window and then type killall afplay to stop the music.

afinfo works much like afplay; just give it the path to an audio file, and you’ll get a slew of information about that file:

$ afinfo Jungleland.mp3File:           08 Jungleland.mp3File type ID:   MPG3Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame                no channel layout.estimated duration: 573.000 secaudio bytes: 14025334audio packets: 21961bit rate: 195000 bits per secondpacket size upper bound: 1052maximum packet size: 1045audio data file offset: 2441optimized

The last command, afconvert is the most powerful, and therefore, the most complicated of the bunch. I won’t even attempt to go into all the details on how it works—because I’m honest enough to admit that I don’t know them all! You can specify all sorts of options to control the input and output formats. A simple example:

$ afconvert -v -f "mp4f" -d "aac@44100" /System/Library/Sounds/Basso.aiff ~/Desktop/basso_converted.mp4

That will take the Basso system sound, and convert it into an MP4 file (encoded with the AAC encoder at a 44.1kHz sample rate, which is stated in straight hz (44100) in the command). The converted file is saved to the Desktop with the name basso_converted.mp4.

What you do with these commands is really only limited by your imagination. If you’d rather not fire up iTunes for some simple audio playback, you can use them that way. If you have remote login (ssh) abilities on another machine in your house or business, you can play a song for a housemate or coworker, perhaps as an alarm clock if they’re oversleeping. If you’ve got a lot of audio files to convert, you can use afconvert to automate the process, though this will take a bit more work with shell scripting (as you need a simple loop to loop through the files).

While not useful to everyone, perhaps, having tools such as these available for those who need them is one of the reasons OS X appeals to such a diverse audience.

One Comment

One person was compelled to have their say. We encourage you to do the same..

  1. subcorpus says:

    this has been very helpful …
    going to try this in leopard now …
    thank you …

Leave a Comment

Please keep your comments friendly on the topic.

Contact us