How to play audio files in the Terminal
[ad_1]
If you’re a command line aficionado and you want to play an audio file, there’s no need to leave the Terminal. There are Terminal commands you can use to not just play audio, but to convert files as well.
We’ll go over the afplay
, afinfo
, and afconvert
audio commands in this article. 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 (type afplay -h
or afconvert -h
into Terminal). Thankfully, usage of all three commands is relatively straightforward.
Play an audio file
To play an audio file, type the afplay
command followed by the path of file. For example:
afplay /Users/macworld/Desktop/Opening.aif
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 & disown
after the path to the audio file. For example:
afplay /Users/macworld/Desktop/Opening.aif & disown
However, this makes it tougher to stop playback. You have to open a new Terminal window and then type killall afplay
to stop the audio.
Get info on an audio file
The command 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. For example, in Terminal, I entered:
afinfo /Users/macworld/Desktop/Opening.aif
And this is what appears in Terminal:
File: /Users/macworld/Desktop/Opening.aif File type ID: AIFF Num Tracks: 1 ---- Data format: 2 ch, 44100 Hz, 'lpcm' (0x0000000E) 16-bit big-endian signed integer no channel layout. estimated duration: 32.693424 sec audio bytes: 5767120 audio packets: 1441780 bit rate: 1411200 bits per second packet size upper bound: 4 maximum packet size: 4 audio data file offset: 54 not optimized source bit depth: I16 ----
Covert audio files
The afconvert
command is a powerful and complicated one. 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! Read the help file (enter afconvert -h
in the Terminal); it provides good detail on how to use this command.
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.
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).
What you do with these commands is really only limited by your imagination. If you’d rather not fire up the Music app 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.
While not useful to everyone, perhaps, having tools such as these available for those who need them is one of the reasons macOS appeals to such a diverse audience.
[ad_2]
Source link