Repository navigation
Description
DASH manifest: get_urls miscounts SegmentTimeline segments
Background
Downloading a playlist, one track failed every time with 400 Bad Request on its last segment. The track is Queen - "Yeah" (534096006), which is only 4 seconds long. It plays fine in the TIDAL apps, and playbackinfopostpaywall answers 200 at every quality.
Its HI_RES_LOSSLESS manifest (tokens removed):
<SegmentTemplate timescale="44100"
initialization="https://sp-ad-fa.audio.tidal.com/mediatracks/…/0.mp4?token=…"
media="https://sp-ad-fa.audio.tidal.com/mediatracks/…/$Number$.mp4?token=…"
startNumber="1">
<SegmentTimeline><S d="176128"/><S d="1730"/></SegmentTimeline>
</SegmentTemplate>That is two media segments (1.mp4, 2.mp4) plus the initialization segment, but StreamManifest.urls has four entries:
| URL | Response |
|---|---|
0.mp4 |
200 (initialization) |
1.mp4 |
200 |
2.mp4 |
200 |
3.mp4 |
400 Bad Request |
Issue
DashInfo.get_urls (media.py) counts the segments as 2 + Σ(r if r else 1). In DASH, each <S> element is one segment followed by r repeats (r defaults to 0), so the timeline holds Σ(r + 1) media segments, plus one initialization segment.
The two formulas agree only when exactly one <S> element has r > 0. That is the usual timeline (<S d="…" r="N"/><S d="…"/>), which is why almost every track works:
| Timeline | Media segments | tidalapi lists | Result |
|---|---|---|---|
<S/><S/> (short track) |
2 | 3 | extra segment, 400 |
<S r="2"/><S r="1"/><S/> |
6 | 5 | last segment missing, audio cut short |
get_hls() builds from the same list, so the HLS playlist for such a track has the same phantom or missing entry.
Reproduce (offline)
from tidalapi.media import DashInfo
mpd = (
"<?xml version='1.0' encoding='UTF-8'?>"
'<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" '
'mediaPresentationDuration="PT4.033S"><Period>'
'<AdaptationSet contentType="audio" mimeType="audio/mp4">'
'<Representation id="a" codecs="flac" bandwidth="1" audioSamplingRate="44100">'
'<SegmentTemplate timescale="44100" initialization="https://cdn/0.mp4" '
'media="https://cdn/$Number$.mp4" startNumber="1">'
'<SegmentTimeline><S d="176128"/><S d="1730"/></SegmentTimeline>'
"</SegmentTemplate></Representation></AdaptationSet></Period></MPD>"
)
print(DashInfo.from_mpd(mpd).urls)
# ['https://cdn/0.mp4', 'https://cdn/1.mp4', 'https://cdn/2.mp4', 'https://cdn/3.mp4']
# expected ['https://cdn/0.mp4', 'https://cdn/1.mp4', 'https://cdn/2.mp4']Seen with tidalapi 0.8.11; main (e13888a) has the same code.
Proposed fix
Count Σ(r + 1), take the first URL from the template's initialization, and number the media segments from startNumber (default 1). For the timelines TIDAL usually serves, this gives exactly the same URLs as today. I checked 8 regular tracks against the live API and the lists were identical, and track 534096006 now downloads in full. I have a branch with the fix and offline tests in tests/test_parsing.py, and will open a PR referencing this issue.
One thing the fix doesn't cover: DASH allows r="-1", meaning "repeat until the end", and neither the old code nor mine handles it. Tidal hasn't served it on any track I checked, so I left it out.
Activity