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.
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
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')
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
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)
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
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_startisNoneandlinesis empty. A very loud instrumental can still produce a stray word, so checkvocal_startbefore 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/segmentationwith the file, thenPOST /api/functions/run/lyricswith the analysis's exeId asanalysisExeId. 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,
segmentandlyricsare 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.
`
---
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.