The old school way would be to buy an external USB device that provides 2 3.5mm jack, line in and line out. Buy a 3.5mm male-to-male cable and plug into both ports. There is an easier alternative and it is free.
The easier and free alternative is to install https://github.com/ExistentialAudio/BlackHole . I prefer to install it using Homebrew with just a simple command line. If you do not have Homebrew, visit https://brew.sh for installation command, should be a one liner, copy and paste it into your terminal.
# how to install BlackHole
brew install blackhole-2ch
# or
brew install blackhole-16ch
Which to use? It depends on your recording software, whether is supports more than 2 channels? Quicktime audio recording supports both 2 channels and 16 channels. I prefer to use command line recording software called “sox” but it support 2 channels only.
# how to install SoX
brew install sox
Once BlackHole installation completed you will see an additional sound input
And also an additional sound output
To simulate the same configuration as the hardware method. Set both sound input and sound output to “BlackHole 2ch”.
Start your recording. SoX supports multiple codec output, whereas Quicktime only allows m4a.
# start recording with SoX
rec filename.mp3
Play the audio you would like to record. To end the SoX recording, press Ctrl + C. Since we started recording first and later play the audio, there will be a quiet few seconds at the beginning. Commands below to trim with SoX.
# trim 1 seconds away at the beginning
sox input.mp3 output.mp3 trim 0:01# trim the mp3 length to 1 minute 23 seconds
sox input.mp3 output.mp3 trim 0:00 1:23
The disadvantages of this method, is that you need to mute all your other applications that will produce sound while you are recording like notifications. You can do this by enabling do not disturb.
Secondly, is the waiting. If you are recording an audio clip of 30 minutes, you will have to wait 30 minutes for it to complete.
Updates (16 Feb 2022)
I found a better alternative to BlackHole for 2ch. It is called Background Music. To install run the command below
brew install --cask background-music
Upon installation completion, make sure in macOS System Preferences the sound output is set to Background Music
And if you are using external earphones at the 3.5mm jack. Choose your output device within Background Music app.
Before recording Spotify music, make sure you set your sound input in system preferences to Background Music.
Now start your mp3 recording in SoX, then play the music on Spotify. When it is done you will need to trim the beginning and the end. I recommend using Audacity.