Skip to content

Instantly share code, notes, and snippets.

@Moonbase59
Last active March 26, 2024 21:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moonbase59/5e70279740a5b227c2106cff45abd706 to your computer and use it in GitHub Desktop.
Save Moonbase59/5e70279740a5b227c2106cff45abd706 to your computer and use it in GitHub Desktop.
spec - Quick-n-dirty spectrogram display for audio files
#!/bin/bash
# spec - Copyright (c) 2024 Matthias C. Hormann
# 2024-03-26
# Show spectrogram for an audio file, using ffmpeg's showspectrumpic
# Add this to the "Open with…" context menu of your file manager!
# define me
me=`basename "$0"`
version="0.2"
if [ -z "$1" ] ; then
exit 1;
fi
# Create temporary file names to use.
# kludge for MacOS: Mac variant first, then Linux:
TEMP=`mktemp -u -t ${me} 2>/dev/null || mktemp -u -t ${me}-XXXXXXXXXX`
TEMPIMG="${TEMP}.png"
TEMPTXT="${TEMP}.txt"
# Shell + ffmpeg quoting rules are a mess, and inconsistent,
# so better use a temporary text file for the info.
# Save the original file name to show in the spectrogram.
basename "$1" > "${TEMPTXT}"
# Note: showspectrumpic height MUST be a power of 2!
ffmpeg -v quiet -y -i "$1" -filter_complex showspectrumpic=s=1024x512:stop=22000,drawtext="expansion=none:textfile='${TEMPTXT}':x=(w-tw)/2:y=16:fontcolor='white':fontsize=20" "$TEMPIMG"
exitcode=$?
if [ $exitcode -ne 0 ] ; then
rm "$TEMPTXT"
exit $exitcode
fi
# Open in your default PNG image file viewer.
# Using a subshell here so we can wait until closed, before removing the temp file
# Macs don’t have `xdg-open`, so use `open` instead. Requires correct file extension.
dummy=$(open "$TEMPIMG")
rm "$TEMPIMG" "$TEMPTXT"
@Moonbase59
Copy link
Author

Moonbase59 commented Mar 20, 2024

spec

Quick-n-dirty spectrogram display for audio files, using ffmpeg.

I did this because I missed good old spek so much… It’s been on a long hiatus and not available in Ubuntu & Linux Mint for a while, but it’ll come back (already in Debian Bookworm, I reckon).

Copy this to a location in your path (~/bin, ~/.local/bin, /usr/local/bin come to mind), chmod +x it and you can quickly watch an audiofile’s spectrogram from the commandline:

spec audiofile.ext

It will open in your default PNG file viewer:

spec-Ejug6wfSv2 png_001

You can also add this command as an Open With… context menu entry in your favourite file manager, for more ease of use.

@Moonbase59
Copy link
Author

I rewrote the quoting and filename part, so spec can now correctly display the craziest filenames and won’t break:

spec-7N0OTacMak png_001

A spectrogram is normally used to diagnose audio properties and quality problems, but sometimes, just sometimes, you can find hidden treasures… ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment