DEV Community

Zach Farrell
Zach Farrell

Posted on

Find a song's structure and word-timed lyrics in Python published: false

Get a song's sections (intro, verse, chorus) with start times and bars, its BPM and key, and its lyrics with a start and end time for every word, from an audio file.

Disclosure: I build Audial, the API this post uses.

Getting a song's BPM and key from Python is a solved problem. Two things are harder: where each section starts (intro, verse, chorus, the build before a drop), and when each word of the vocal is sung. The research code for song structure takes some assembling and a GPU, and speech-to-text tools are made for speech, not a voice over a full band.

Here's a short way to get both from an audio file: what you need for DJ cue points, karaoke-style lyrics, cutting a video on the chorus, or a practice app that loops one section.

Install

pip install audial-sdk
audial login        # opens your browser once; or: export AUDIAL_API_KEY=your_key
Enter fullscreen mode Exit fullscreen mode

Sections, tempo and key

import audial

song = audial.segment("song.mp3")   # sections, tempo map, bars, key
a = song["analysis"]

print(a["bpm"], a["key"], a["camelot"], a["time_signature"])
for s in a["sections"]:
    print(f'{s["start"]:6.1f}-{s["end"]:6.1f}  {s["label"]:<7} {s["bars"]} bars')
Enter fullscreen mode Exit fullscreen mode

On a 65-second song made with Audial's song generator, this took 17 seconds and printed (after the SDK's own progress lines):

89.99 Dmin 7A 4/4
   0.0-  21.9  INTRO   8 bars
  21.9-  43.2  VERSE   8 bars
  43.2-  48.5  UP      2 bars
  48.5-  65.6  VERSE   7 bars
Enter fullscreen mode Exit fullscreen mode

The labels are INTRO, VERSE, UP (a build), CHORUS, DOWN (a breakdown), OUTRO, and LOOP for a clip of 30 seconds or less. There's no DROP label: a drop comes back as CHORUS, with the build before it as UP. Each section also carries start_bar and fills. If a track's tempo drifts, a["tempo_mode"] is "variable" and a["grid"] holds the tempo map; audial.grid.beats_of(song) expands it into beat and bar times.

Lyrics with word timestamps

lyrics = audial.lyrics(song["analysis_id"])   # uses the analysis above

print("vocals start at", lyrics["vocal_start"])
for line in lyrics["lines"][:4]:
    text = " ".join(word for word, start, end in line["words"])
    print(f'{line["start"]:6.2f}-{line["end"]:6.2f}  {text}')

word, start, end = lyrics["lines"][1]["words"][0]
print(word, start, end)
Enter fullscreen mode Exit fullscreen mode
vocals start at 5.207
  4.95-  5.47  Ah
 10.95- 14.35  Porch light humming on an empty street
 16.10- 19.80  Summer's fading underneath my feet
 20.75- 28.47  Stay wild, stay wild Stay wild, stay wild
Porch 10.95 11.57
Enter fullscreen mode Exit fullscreen mode

Every word is [word, start, end] in seconds on the track's own timeline, so it lines up with the sections above. It reads the full mix, so there's no need to isolate the vocal first. This call took 78 seconds, most of it the "Preparing the track…" step the SDK prints before the lyrics.

Things to know

  • It works on an audio file you have, not a streaming link or a track ID.
  • The language is detected and the words are transcribed as sung, never translated.
  • With no vocals, vocal_start is None and lines is empty. A very loud instrumental can still produce a stray word, so check vocal_start before trusting a line.
  • The first call after a quiet spell can take a few minutes while a machine starts. For a library, run several tracks at once and cache the JSON.
  • Over REST it's two calls: POST /api/functions/run/segmentation with the file, then POST /api/functions/run/lyrics with the analysis's exeId as analysisExeId. The API reference is at audialmusic.ai/resources/api-reference.

Cost

  • Free: 2 runs. An analysis counts as one run and everything added to it (lyrics, tags, audio features) is included, so that's two whole songs.
  • After that: $10 a month for 100 credits. On a plan, segment and lyrics are a credit each.
  • audial.analyze("song.mp3") gives the BPM and key alone and is always free.

If you build something with this, I'd like to hear which label or field you were missing.

Previously: Spotify's audio features are gone for new apps. Here's how to get energy and danceability from your own audio


`

---
Enter fullscreen mode Exit fullscreen mode

Top comments (0)