Timestamps and offsets: what each time field means
What every time value in an AudD response measures: timecode on the standard endpoint; chunk offset, start_offset and end_offset on the enterprise endpoint; timestamp, play_length and time in stream callbacks. With a worked example for each.
There are various fields referring to time in our API responses. Some are positions in the recognized song, others are positions in the file you sent, and a few are dates and times. Knowing which is which is important if you’re creating tracklists, cue sheets, airplay logs, or anything else with a timeline. Below, you’ll find what each of them means, with a worked example for every endpoint.
| Field | Endpoint | What it is |
|---|---|---|
timecode | Standard and enterprise | The time in the song when the fragment you sent is played |
offset | Enterprise | The start of the 12-second fragment in your file |
start_offset, end_offset | Enterprise | The start and end of the match in the fragment, in milliseconds |
start_seconds, end_seconds | Enterprise, in the SDKs | The start and end of the match in your file, in seconds |
timestamp | Streams | The time the song started |
play_length | Streams | The total streamed time of the song, in seconds |
time | Stream notifications | The time of sending, in Unix seconds |
since_time | Longpoll | The cursor for the next request |
Standard endpoint: timecode
And just in case: timecode is the time in the recognized song when the fragment you sent is played. So 00:56 means that the fragment you sent matched the part of the song that starts 56 seconds in. It’s a position in the song, not in your clip.
from audd import AudD
audd = AudD("your-api-token") # get your own token at dashboard.audd.io
result = audd.recognize("https://audd.tech/example.mp3")
if result is None:
print("no match")
else:
print(result.artist, "-", result.title, "at", result.timecode, "in the song")
For this file, it prints Tears For Fears - Everybody Wants To Rule The World at 00:56 in the song. The standard endpoint analyzes up to the first 12 seconds of the audio, and the result doesn’t say where your clip is in the recording. If the clip is part of a longer recording, you have to store where it starts yourself. For example, if you cut the clip at 03:00 of a recording and it matches at 00:56, the song was playing at 03:00 of the recording, 56 seconds into the song.
Enterprise endpoint: offset, start_offset and end_offset
When you upload a file, our servers consider it to be a group of 12-second-long audio chunks. The response is an array of these chunks, and each one has an offset and the songs found in it:
offsetis the position, in the audio file you submitted, of the start of the 12-second-long fragment that contains matches.start_offsetandend_offsetare the positions (in milliseconds) in the 12-second-long fragment of the start and end of the fragment we matched to a song.timecodeis the position in the original song where the matched fragment is playing.
Set accurate_offsets to true to get start_offset and end_offset. To get the position of a match in your file, add the song’s start_offset, divided by 1000, to the chunk’s offset in seconds:
position in the file = offset (in seconds) + start_offset / 1000
Worked example
Here is the second chunk of the example response in the enterprise docs, shortened:
{
"songs": [
{
"artist": "Barely Alive",
"title": "Keyboard Killer",
"timecode": "00:41",
"start_offset": 1,
"end_offset": 9660
}
],
"offset": "00:48"
}
- The chunk starts 48 seconds into the file (
"00:48"). - The match starts at 48 + 1 / 1000 = 48.001 seconds and ends at 48 + 9660 / 1000 = 57.66 seconds.
- During those 9.66 seconds, the file plays “Keyboard Killer” from 00:41 of the song.
Don’t use timecode to find where a song starts in the file. You might be tempted to subtract 41 seconds from 48, but in a mix, the DJ can start a song from the middle, so the result would be wrong.
When offsets skip ahead
The example request used skip=3 and every=1, so the server recognized one chunk, skipped the next three, and so on. That’s why the offsets go 00:00, 00:48, 01:36: the 36 seconds after each chunk weren’t scanned. Also, a chunk with no matches doesn’t appear in the response at all. In the full 63-chunk response of that example, the offsets jump from 13:36 to 16:00. So read the position from offset, and don’t count chunks to work it out.
skip_first_seconds tells the server how many seconds of the file to skip before it starts the music recognition, so it’s the start time of the first chunk. The offset is still counted from the start of the file you submitted.
Read the offsets from the HTTP response
If you call the HTTP API yourself, go through the chunks, and then through the songs of each chunk:
import httpx
def to_seconds(offset):
# "00:48" -> 48; also works for "1:02:24"
seconds = 0
for part in offset.split(":"):
seconds = seconds * 60 + int(part)
return seconds
body = httpx.post(
"https://enterprise.audd.io/",
data={
"api_token": "your-api-token",
"url": "https://audd.tech/example.mp3",
"accurate_offsets": "true",
"limit": "5",
},
timeout=600,
).json()
if body.get("status") != "success":
raise SystemExit(f"AudD error: {body.get('error')}")
for chunk in body.get("result") or []:
base = to_seconds(chunk["offset"])
for song in chunk.get("songs") or []:
# without start_offset/end_offset, all you know is the 12-second chunk
start = base + song.get("start_offset", 0) / 1000
end = base + song.get("end_offset", 12000) / 1000
print(f"{start:7.2f}s to {end:7.2f}s in the file "
f"{song.get('artist')} - {song.get('title')}, "
f"at {song.get('timecode')} in the song")
limit is the upper bound for the number of chunks the server will recognize. The requests to the enterprise endpoint are counted as 1 request per 12 seconds of audio, so always set it while you’re testing. For the chunk above, the script prints:
48.00s to 57.66s in the file Barely Alive - Keyboard Killer, at 00:41 in the song
With an SDK
The SDKs return one list of matches instead of the chunks, so there’s no chunk offset. Instead, each match has start_seconds and end_seconds (startSeconds and endSeconds in Node.js), the positions of the match in your file, in seconds. The SDK calculates them from offset and start_offset for you. The SDKs send accurate_offsets=true by default.
from audd import AudD
audd = AudD("your-api-token")
for m in audd.recognize_enterprise("https://audd.tech/example.mp3", limit=5):
if m.start_seconds is None:
continue
print(f"{m.start_seconds:7.2f}s to {m.end_seconds:7.2f}s in the file "
f"{m.artist} - {m.title}, at {m.timecode} in the song")
If you need the chunks themselves, for example to show which parts of the file were scanned with skip, read the raw HTTP response instead. For the fields of the chunks and the songs, see the enterprise response and match fields reference.
Streams: timestamp, play_length and time
The results will contain the timestamp of the song’s start, the played duration, and the metadata (shortened here):
{
"status": "success",
"result": {
"radio_id": 7,
"timestamp": "2020-04-13 10:31:43",
"play_length": 111,
"results": [{ "artist": "Alan Walker, A$AP Rocky", "title": "Live Fast (PUBGM)" }]
}
}
By default, callbacks are sent after the song ended playing and contain the total streamed time of the song, in seconds, in play_length. Add timestamp and play_length to find when the song ended:
from datetime import datetime, timedelta
started = datetime.strptime("2020-04-13 10:31:43", "%Y-%m-%d %H:%M:%S")
ended = started + timedelta(seconds=111) # play_length
print(ended.strftime("%Y-%m-%d %H:%M:%S"))
It prints 2020-04-13 10:33:34. If you add the stream with callbacks=before, you’ll receive the callbacks as soon as a song starts playing, and you won’t receive the total played time in the callbacks. The timestamp is still the start of the song.
Notifications don’t have a timestamp. They contain the time of sending in time, as Unix seconds: 1587939136 is 2020-04-26 22:12:16 UTC.
The time zone of timestamp
The time in timestamp is UTC. The string doesn’t contain the time zone. If you parse it without setting the time zone, your code can read it as the local time of your server. It’s also useful to store the time your server received the callback. If your server doesn’t respond, we’ll put the callbacks in a queue, so you can receive a callback much later than the song played.
from datetime import datetime, timezone
from flask import Flask, request
from audd import AudD
from audd.errors import AudDSerializationError
audd = AudD("your-api-token")
app = Flask(__name__)
plays = [] # keep these in a database in production
@app.post("/audd-callback")
def audd_callback():
try:
match, notification = audd.streams.handle_callback(request)
except AudDSerializationError:
return "", 200
if match:
plays.append({
"radio_id": match.radio_id,
"timestamp": match.timestamp, # UTC, as received: "2020-04-13 10:31:43"
"play_length": match.play_length, # seconds; don't use it with callbacks=before
"received_at": datetime.now(timezone.utc).isoformat(),
"artist": match.song.artist,
"title": match.song.title,
})
return "", 200
The longpoll timestamp is a cursor
In longpoll, timestamp has a different job. When you receive a response containing the timestamp field, use it as the updated since_time for the new request. For how longpoll works, see Callbacks vs. longpoll.
Related
Reading this as an AI agent? The raw Markdown is at concepts/timestamps-and-offsets.md, and the full index is /resources/llms.txt.
