DEV Community

The MP3 Header Said 7:15. The Audio Ran 3:03, and I Bill by the Minute

I run a visa agency in Bali, and in the evenings I build data tools on Apify. One of them is MP3 to Text: you give it MP3 links and it returns a transcript with timed passages.

Under the hood it is Whisper behind Groq's API. Most of my code is the plumbing around the model: downloading, converting, splitting long files and billing. Billing is per started minute of audio, so the most important number in my code isn't the transcript. It's the duration.

This post is about one MP3 that reports a duration 2.4 times longer than its real length, and why a transcriber should never trust what a file says about itself.

Building a file that lies

I didn't want to guess, so I made the file myself. I took the one-minute test MP3 I use for daily checks (a passage from Pride and Prejudice read aloud), put three copies in a row, and added three seconds of silence at the front. I encoded it as variable bitrate with LAME, then copied the frames into a new file without the Xing header:

ffmpeg -f lavfi -t 3 -i anullsrc=r=44100:cl=mono \
  -i sample.mp3 -i sample.mp3 -i sample.mp3 \
  -filter_complex "...concat=n=4:v=0:a=1" \
  -c:a libmp3lame -q:a 4 vbr_xing.mp3

ffmpeg -i vbr_xing.mp3 -c copy -write_xing 0 vbr_noxing.mp3

The audio in both files is byte-for-byte the same, and both are 1.74 MB. The only difference is a small frame at the start of the first file that says "this stream has N frames".

Four durations for one file

Here's what I got for vbr_noxing.mp3:

Method Duration
ffmpeg -i (header, no Xing) 7:15.37, with the warning "Estimating duration from bitrate, this may be inaccurate"
File size รท 128 kbps (the usual guess) 1:48.8
ffmpeg -i on the copy with Xing 3:03.00
Decoding the whole stream (-f null -) 3:03.04

The 7:15 has a simple cause. An MP3 has no length field. A VBR file can carry a Xing (or VBRI) frame that gives the frame count. Without one, the demuxer reads the bitrate of the first frame and divides the file size by it.

My file starts with three seconds of silence, and LAME encodes silence at the lowest rate it can, which here is 32 kbps. So: 1,741,533 bytes ร— 8 / 32,000 bit/s = 435.4 s = 7:15

The guess from file size is wrong in the other direction because the speech in this file averages about 76 kbps, not 128.

You might say I built this case on purpose. I did, but the parts are common. Podcasts usually open with a jingle or a pause. Most phone recorders and podcast tools encode VBR. And there are plenty of ways to lose the header, or end up with a stale one: joining MP3s with cat, cutting a file with a tool that copies frames, or a recorder that streams to disk and never goes back to finish the header. A joined file can be even worse, because it keeps the first part's Xing frame and reports the length of that part only.

Why this matters for a transcriber

My billing function is three lines:

BILLING_GRACE_SECONDS = 1.0

def billable_minutes(duration):
    return max(1, math.ceil(((duration or 60) - BILLING_GRACE_SECONDS) / 60))

With 435 seconds as input, that's 8 minutes. With 183 seconds, it's 4.

The same problem hits any other step that uses duration: your cost estimate before sending a batch, the "Max cost per run" check, timeout planning and queue ordering.

So the Actor never asks the original file how long it is. The first thing it does is decode the file to 16 kHz mono Opus, the format the recognizer gets anyway. Then it measures that file:

command = [
  'ffmpeg', '-hide_banner', '-loglevel', 'error',
  '-i', path, '-vn', '-ac', '1', '-ar', '16000',
  '-c:a', 'libopus', '-b:a', '24k', '-y', output,
]
...
return output, probe_duration(output)

Ogg Opus stores the real sample position in every page, so its duration is counted, not estimated. The billable minutes come from that number.

The real run

I uploaded vbr_noxing.mp3 to a test account and sent it through the Actor with default settings:

{
  "urls": ["https://.../episode.mp3"],
  "includeSegments": true
}

The row, trimmed:

{
  "fileName": "episode.mp3",
  "durationSeconds": 183.05,
  "durationMinutes": 3.05,
  "language": "English",
  "model": "fast",
  "wordCount": 643,
  "charCount": 3478,
  "segmentCount": 45,
  "status": "ok"
}

The run log says 4 min billed, 183.0s of audio recognised. The charged events were audio-minute ร— 4 and file-processed ร— 1, $0.038 in total, and the run took 6.3 seconds. If I had trusted the header, it would have been 8 minutes and $0.074 for the same 643 words.

The last segment ends at 182.5 s, and the decoded file is 183.05 s long. That half second at the end is the trailing pause after "It is a truth universally acknowledged." So the duration and the transcript agree with each other. That's the cross-check I use when I don't trust a number.

A side benefit: the file gets smaller

The same conversion turned the 1.74 MB MP3 into a 525 KB Ogg file, about 0.17 MB per minute of speech. That matters because Groq refuses uploads over 25 MB. A 128 kbps MP3 reaches that at about 26 minutes, so a normal podcast episode is too big to send as it is. After conversion, more than 100 minutes of speech fit in one upload.

Longer files get cut into 100-minute parts, and each part's segment times are shifted by its offset. Then a three-hour episode comes back as one transcript with continuous timings.

There's one ffmpeg detail I learned the hard way. When you split with -f segment -c copy, you need -reset_timestamps 1. Without it, every part after the first keeps its original timestamps and reports a wrong duration, and the recognizer rejected those parts outright.

The other thing the silence did

Compare the first segment of the plain one-minute file with the first segment of this one:

{"start": 0, "duration": 4.6, "text": "It is a truth universally acknowledged, that a single man in possession of a good fortune,"}
{"start": 0, "duration": 7.6, "text": "It is a truth universally acknowledged, that a single man in possession of a good fortune,"}

The text is the same, and the second segment is exactly three seconds longer. The model doesn't output an empty segment for the silence. It adds the silence to the first passage, so the passage "starts" at 0 even though the reader starts at 3 s.

If you use segment times to cut clips or place a first subtitle, the first timestamp can be early by the length of the intro. Later segments are fine. In this file the next one starts at 7.96, which is right.

Calling it, and checking duration yourself

curl -X POST "https://api.apify.com/v2/acts/lergassy~mp3-to-text/run-sync-get-dataset-items?token=YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"urls":["https://example.com/episode-12.mp3"],"includeSegments":true}'

If you estimate cost before sending a batch, decode the file instead of reading its header. It's slower, but it's the real number:

import re, subprocess

def real_seconds(path):
    out = subprocess.run(
      ["ffmpeg", "-hide_banner", "-i", path, "-f", "null", "-"],
      capture_output=True, text=True
    ).stderr
    h, m, s = re.findall(r"time=(\d+):(\d+):([\d.]+)", out)[-1]
    return int(h) * 3600 + int(m) * 60 + float(s)

On my file this returns 183.04. After the run, compare it with durationSeconds on each row. If they differ by more than a second, look at the file before you look at the bill.

When this is the wrong tool

  • Private files. A link that needs a login won't download. Pre-signed links work, but the full URL appears in the run log, query string included. Use short-lived links, not ones with a permanent key in them.
  • Word-level timing. You get phrase-level segments. As shown above, the first one can include a silent intro.
  • YouTube videos that already have captions. Read the captions instead of paying to recognize the audio again.
  • Live streams. This works on finished files only.

What I'd take away from this: an MP3's reported duration comes from metadata the file may not have. If you price, bill or time out by duration, measure it from the decoded audio.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.