Video Quality Selection#
XHS-Downloader selects the highest-quality video file available from XiaoHongShu's API by sorting all available stream variants according to a user-defined preference metric. A cookie is required to unlock higher-quality tiers from the API; without one, only low-resolution files are accessible.
video_preference Setting#
The video_preference key in settings.json controls the sort dimension used to rank quality tiers. Three values are accepted :
| Value | Sort by |
|---|---|
resolution (default) | Height (vertical pixel count) |
bitrate | videoBitrate field |
size | File size |
Invalid values fall back to "resolution" .
How Selection Works#
The entry point is Video.deal_video_link(data, preference). It tries a fast path first:
generate_video_link— if the API response contains a directvideo.consumer.originVideoKey, it constructs a CDN URL athttps://sns-video-bd.xhscdn.com/{key}and returns it immediately, bypassing quality selection.get_video_link— if no direct key exists, it fetches all H.264 and H.265 stream objects fromvideo.media.stream.h264andvideo.media.stream.h265, sorts them by the requested metric, and returns either the firstbackupUrlsentry or themasterUrlof the highest-ranked variant.
The call site in app.py passes self.manager.video_preference directly :
container["下载地址"] = self.video.deal_video_link(data, self.manager.video_preference)
Cookie and Quality Tier Access#
The README explicitly warns: without a cookie, video downloads are limited to low-resolution files; configuring a cookie unlocks higher quality — no login required .
Cookies are integrated at the HTTP client level. Manager.__init__ converts the cookie string to a dict via cookie_str_to_dict and injects it into the AsyncClient used for all API requests . The cookie parameter is optional in settings.json and also overridable per-request via the API mode's cookie field .
To obtain a valid cookie, the README instructs filtering the browser DevTools Network tab for cookie-name:web_session while visiting xiaohongshu.com/explore, then copying the full Cookie header .
The Manager.clean_cookie utility strips webId and web_session tokens from a cookie string — used when displaying or logging cookies without sensitive values.
Configuration Reference#
| Setting | Default | Config path |
|---|---|---|
video_preference | "resolution" | settings.py line 30 / manager.py line 127 |
cookie | "" | settings.py line 20 / manager.py line 105 |
video_download | true | settings.py line 28 |
Key source files:
source/application/video.py— quality selection logicsource/module/manager.py— cookie injection and preference validationsource/module/settings.py— default configuration valuessource/application/app.py— call site wiringvideo_preferenceinto extraction