Let's say you're a Linux user. And let's say that you have valid, legitimate, one might even say ethical, reasons for capturing the audio that you're hearing through your speakers. Most modern Linux system are using Pipewire these days, so let'se discuss how you, on a Pipewire system, you can capture the audio being produced by some application you're currently running.

The first step is to figure out what sink is producing the audio. Pipewire has the concept of a "sink," which is something that either fills from, or drains to, another component, ultimately ending at some hardware attached to speakers. Most people running Pipewire still have Pulseaudio as an interface layer, just because so many applications have been written to the Pulseaudio API. Running the Pulseaudio command pactl list sink-inputs will show you a list of all the applications that are currently registered to produce audio. All you want, really, is the sink number, which is at the top of each sink, reading Sink Input #4111, where the number will be different for each application.

If you want to extract just the number, here's a gnarly command for doing just that:

pactl list sink-inputs | \
    grep -E 'Input #|application\.name' | \   # Only the sink number and application name
    tr -d '[:space:]' | \                    # Remove ALL whitespace (including newlines!)
    tr -d '"' | \                           # Remove the quotes around the application name
    grep -Eoi '#[0-9]*application\.name=Audacious' | \  # EDIT THIS: Set the application to the name you want!
    grep -oi '[0-9]*'                        # Spit out only the number left: the sink ID.

Some uncommon (maybe?) grep flags here:

$ grep -i # Case insensitive grep.
$ grep -E # extended grep: allows for pipe and grouping
$ grep -o # Only print what the expression matched, not the whole line

Once you've got the sink, Pipewire's pw-record command has all the power you need. Do whatever substitutions you need for SINK_NUMBER and RECORDED_FILE.

pw-record --latency=20ms --volume=1.0 --format=f32 \
          --channel-map stereo --latency=20ms \
          --rate 44100 \
          --target="$SINK_NUMBER" "$RECORDED_FILE.rec"

When your recording is done, just hit Ctrl-C. If it's going to take awhile, and you're sure this is the only pw-record session currently running, you can automate its shutdown. Let's say it'll take two hours. Set up sleep for that long, then kill the pw-record application.

sleep 2h ; kill $(ps fx | grep pw-record | grep -v grep | awk '{print $1}')

I'd run ps fx | grep pw-record | grep -v grep to make sure it's the only one running before I set up my sleep command. (The grep -v grep line is there because, since "pw-record" is part of the grep command, it might show up in the ps record, so you want to remove it in any case, leaving only the real "pw-record" to kill.) (Yes, I know, killall exists, but I'm not sure I trust it. I'm old-skool, as they say.)

Because you're recording from the input-sink, any other bleeps and boops your operating system might make in that time shouldn't be part of the stream. But do try to keep everything else on your system down and quiet; running some other high-demand applications can add crackling and unwanted pauses to your recording.

You can then convert the output to MP3 with the following:

ffmpeg -y -i "RECORDED_FILE.rec" -acodec mp3 -b:a 320k "OUTPUT_FILE.mp3"

In the event that your recording comes out funny, either too slow or too fast, start the recording and then run pw-top; find your source in the list of running applications, write down the FORMAT and RATE, and restart pw-record with the format and rate flags set to the values you wrote down. You may need to run man pw-record to see what the format inputs are, but they're fairly easy to understand.


Now, it was a 5-4 vote in the 1984 case Universal Studios vs. Sony Corporation of America that made it legal for Americans to save TV shows to VHS tape, and to save music they'd heard over the air to their tape records. On the other hand, I would never, ever suggest that one should use this power to save one's audiobooks, podcasts, and music from such honorable folks as Spotify or Audible, and I simply must discourage any thoughts you might have in that direction.

Leave stealing our intellectual property to the big boys, mmmmkay?