# How do Bilibili BV and AV numbers relate, and which one should I store?

> BV ids and AV numbers name the same Bilibili video: a reversible encoding since March 2020. How the conversion works, a worked example, and which one to key your data on.

Canonical page: https://okdata.online/answers/bilibili-bv-vs-av-number/

**Short answer: **They are two spellings of the same video: the BV id is a reversible encoding of the numeric aid (the AV number), so you can convert either way locally for free. Store the BV id as your key, because links, share URLs and okdata Bilibili video detail ($0.002 a call) all use it, and keep the aid from ext.aid as a second column for older datasets.

Every Bilibili video has a numeric archive id, the `aid`, which used to appear in links as `av170001`. On 23 March 2020 Bilibili switched its links to BV ids such as `BV17x411w7KC`. The BV id is not a new key: it is the aid run through a fixed, reversible encoding. One video, one aid, one BV id, and you can turn either into the other with a few lines of code and no network call.

## What each one looks like

| | AV number (aid) | BV id (bvid) |
|---|---|---|
| Shape | A positive integer, written `av` + digits in old links | `BV1` + 9 characters, 12 in total |
| Characters | 0-9 | A 58-character alphabet without 0, I, O and l |
| Where you see it | Old links, comment-area mentions, many older datasets | Every current link, share link and app screen |
| In okdata responses | `ext.aid` (with `view=full`) | `id` of every Bilibili post |

Not to be confused with `cid`, which numbers each part of a multi-part video (`ext.cid` on video detail), or the `ep`/`ss` numbers of anime and drama episodes.

## How the conversion works

The current algorithm handles aids up to 2^51. To encode, set bit 51 on the aid, XOR it with 23442827791579, write the result in base 58 with the alphabet `FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf`, prefix `BV1`, then swap the characters at positions 3 and 9, and 4 and 7 (counting from 0). Decoding runs the same steps backwards and masks with 2^51 - 1.

Worked example for av170001: 2^51 OR 170001 is 2251799813855249; XOR 23442827791579 gives 2275242641642698; in base 58 that is `7141xw7KC`, so `BV17141xw7KC`; after the two swaps, `BV17x411w7KC`. We ran the same code on 20 videos from the Bilibili popular board, aids of 15 digits included, and every pair matched.

## Which one to store

Key your tables on the BV id. It is what users paste, what share links carry, and what [Bilibili video detail](/apis/bilibili/videos/get/) and [Bilibili video comments](/apis/bilibili/videos/comments/list/) accept as `id`. Keep the aid in a BIGINT column next to it when you need to join older data. Never treat BV ids as case-insensitive: `BV17x411w7KC` and `bv17X411W7kc` are different strings.

| What you have | What to do | Cost |
|---|---|---|
| An AV number | Convert to BV locally, then look it up | $0.002 |
| A BV id, want the aid | Convert locally | free |
| Search results | `id` is the BV id, `ext.aid` the aid | $0.002 a page |
| 1,000 old aids to refresh | Convert, then 1,000 lookups | $2.00 |

## Example

```python
XOR, MASK, TOP, BASE = 23442827791579, (1 << 51) - 1, 1 << 51, 58
ABC = "FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf"

def av_to_bv(aid: int) -> str:
    n, out = (TOP | aid) ^ XOR, []
    while n:
        n, r = divmod(n, BASE)
        out.append(ABC[r])
    s = list("BV1" + "".join(reversed(out)))
    s[3], s[9], s[4], s[7] = s[9], s[3], s[7], s[4]
    return "".join(s)

def bv_to_av(bvid: str) -> int:
    s = list(bvid)
    s[3], s[9], s[4], s[7] = s[9], s[3], s[7], s[4]
    n = 0
    for ch in s[3:]:
        n = n * BASE + ABC.index(ch)
    return (n & MASK) ^ XOR

assert av_to_bv(170001) == "BV17x411w7KC" and bv_to_av("BV17x411w7KC") == 170001
```

To check one against Bilibili itself, ask video detail for the aid it holds:

```bash
curl -G "https://api.okdata.online/v1/bilibili/videos/get" \
  -H "Authorization: Bearer $OKDATA_API_KEY" \
  --data-urlencode 'id=BV17x411w7KC' \
  --data-urlencode 'view=full' \
  --data-urlencode 'fields=id,title,ext.aid,ext.cid'
```

## Limits

- The endpoint takes a BV id or a `bilibili.com/video/BV...` link. An `av` link is refused as `invalid_params`, which is not charged; convert it first.
- The algorithm is Bilibili's, not a standard. If Bilibili changes it again, ids minted after the change may not decode, so keep the BV id the platform gave you rather than only the aid.
- bilibili-API-collect, long the usual reference for this algorithm, was taken down in January 2026 after a lawyer's letter from Bilibili. Small libraries such as abv still publish the code.

For the stats behind a BV id, see [getting views, coins and danmaku for a BV id](/answers/bilibili-bv-id-video-stats/).

## Endpoints used

- [Bilibili video detail](https://okdata.online/apis/bilibili/videos/get/) `bilibili.videos.get`: $0.002 per call
- [Bilibili video search](https://okdata.online/apis/bilibili/videos/search/) `bilibili.videos.search`: $0.002 per call · about 20 videos per page

## More about Bilibili

- [Bilibili API](https://okdata.online/apis/bilibili/)
- [Bilibili API in Python: videos, comments, popular board (2026)](https://okdata.online/guides/bilibili-api-python/)
- [How do I find Bilibili creators (UP主) in a niche for sponsorships?](https://okdata.online/answers/bilibili-find-up-creators-for-sponsorship/)
- [What is a Bilibili mid (UID), and where do I get it?](https://okdata.online/answers/bilibili-mid-uid/)
- [Bilibili's search API keeps returning -412. Is there a simpler way to search videos?](https://okdata.online/answers/bilibili-search-api-412-error/)
- [How do I scrape the comments on a Bilibili video?](https://okdata.online/answers/bilibili-video-comments-api/)
- [What is a b23.tv link, and how do I get the Bilibili video ID from it?](https://okdata.online/answers/bilibili-b23-short-link/)
- [Post detail API](https://okdata.online/roles/posts-get/)
- [Post search API](https://okdata.online/roles/posts-search/)
- [Bilibili hot searches: today’s list](https://okdata.online/tools/hot-search/bilibili/)

## Sources

- [哔哩哔哩 (Chinese Wikipedia): BV ids replaced AV numbers on 23 March 2020](https://zh.wikipedia.org/wiki/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9) (read 2026-11-12)
- [Colerar/abv: AV and BV convert functions for Bilibili videos](https://github.com/Colerar/abv) (read 2026-11-12)
- [yt-dlp Bilibili extractor: video URLs with an av or BV prefix](https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/extractor/bilibili.py) (read 2026-11-12)
- [bilibili-API-collect: repository closed in January 2026](https://github.com/SocialSisterYi/bilibili-API-collect) (read 2026-11-12)

Where people asked this (read 2026-11-12): 

- https://www.v2ex.com/t/655569
- https://www.v2ex.com/t/655711
- https://github.com/lifegpc/bili/issues/118

Updated 2026-11-12
