DEV Community

loserrrrrrrrrrrc
loserrrrrrrrrrrc

Posted on

Bilibili vs YouTube: Getting Video Metadata Programmatically

play and view are the same number. I spent three months believing they were different fields on different endpoints.

Here is how that went. Bilibili's search endpoint returns play for the view count. Its detail endpoint returns stat.view. I wrote a normalizer that looked for view, ran it against search rows, got None eighteen times in a row, and concluded that search mode does not carry view counts at all.

So I pulled detail for every video I cared about. Eighteen extra requests per keyword, each one a chance to eat a 412, all of it to fetch a number that had been sitting in the search payload the entire time — 22,118,726 for BV1rpWjevEip, right there next to the title.

The lesson is not "read the docs". There were no docs. The lesson is that a None in a normalized field tells you nothing about the source payload until you have printed the raw one.

The drift you have to design around

The same Rick Astley video, on Bilibili, gained 6,199 views in 124 minutes while I was doing something else. I know because I pulled it twice: 109,072,992 views at 01:17 UTC, 109,079,191 at 03:21 UTC. Roughly 3,000 views an hour on a video uploaded in December 2019.

That is the first thing to design for in any cross-platform video table. The second is that Bilibili's schema has three engagement columns YouTube does not have, and flattening both platforms into one table quietly deletes the most interesting signal on the Chinese side.

I only measured the Bilibili half myself. I am not going to quote YouTube view counts I never fetched.

The access models are not comparable

YouTube's Data API wants a key and meters you in quota units. Bilibili's web API wants a User-Agent and a Referer and meters you by getting annoyed at you with a 412. One is a contract. One is a tolerance.

For a batch job reading a few thousand records a day, the second is cheaper and the first is more predictable. Pick on whether you can live with an occasional retry-sleep, not on which is "better".

What one Bilibili record actually contains

Detail mode, trimmed to the fields I care about. Values are real:

{
  "bvid": "BV1GJ411x7h7",
  "aid": 80433022,
  "cid": 137649199,
  "title": "【官方 MV】Never Gonna Give You Up - Rick Astley",
  "author": {"mid": 486906719, "name": "索尼音乐中国"},
  "category": null,
  "duration": "00:03:33",
  "duration_seconds": 213,
  "published_at": "2019-12-31T23:43:23+00:00",
  "stats": {
    "views": 109079191,
    "danmaku": 152684,
    "likes": 2991807,
    "coins": 1299992,
    "favorites": 1574649,
    "shares": 510825,
    "comments": 242415
  }
}
Enter fullscreen mode Exit fullscreen mode

Three of those have no YouTube equivalent:

  • danmaku — scrolling comment overlays drawn on top of the video. A separate comment system with its own culture, 152,684 of them here.
  • coins — a currency users earn through daily activity and spend to "support" a video. You cannot buy them with money at any useful scale, which makes 1,299,992 coins far harder to fake than 2,991,807 likes.
  • favorites — saves to a personal collection. On tutorial content this outruns likes, which is the part most people miss.

If your cross-platform schema is {views, likes, comments}, all three are gone at ingest and cannot be recovered later. The number only exists in the payload at the moment you call it.

Flattening it without losing the shape

import requests

UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
      "(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")

def detail(bvid):
    r = requests.get("https://api.bilibili.com/x/web-interface/view",
                     params={"bvid": bvid},
                     headers={"User-Agent": UA, "Referer": "https://www.bilibili.com/"},
                     timeout=20)
    d = r.json()["data"]
    st = d.get("stat") or {}
    views = st.get("view") or 0
    return {
        "platform": "bilibili",
        "id": d["bvid"],
        "title": d["title"],
        "creator": d["owner"]["name"],
        "published_at": d["pubdate"],
        "duration_s": d["duration"],
        "views": views,
        # keep the platform-specific columns, do not fold them into "likes"
        "danmaku": st.get("danmaku"),
        "coins": st.get("coin"),
        "favorites": st.get("favorite"),
        "likes": st.get("like"),
        "shares": st.get("share"),
        "comments": st.get("reply"),
        # ratios you can compare across platforms
        "like_rate": round((st.get("like") or 0) / views * 100, 3) if views else None,
        "fav_rate": round((st.get("favorite") or 0) / views * 100, 3) if views else None,
        "coin_rate": round((st.get("coin") or 0) / views * 100, 3) if views else None,
    }

print(detail("BV1GJ411x7h7"))
Enter fullscreen mode Exit fullscreen mode

For the MV that gives like_rate 2.74, coin_rate 1.19, fav_rate 1.44 — all three in percent of views.

Note the "category": null up there. The detail endpoint returned no category for that video, while the search endpoint returns tname for the same bvid. Enrich from search when you need it, or leave the column nullable. Do not "fix" it by copying the category of whatever video happened to sit next to it in the batch.

The ratios are where the platform shows through

Run the same arithmetic over the 18 videos my python keyword search returned and a pattern appears that raw view counts hide completely.

The 22,118,726-view course (BV1rpWjevEip) has like_rate 2.76 — basically identical to the music video. But its fav_rate is 4.23, nearly three times the MV's 1.44. Nobody favourites a Rick Astley video. People favourite a 39-hour course they intend to come back to, and favourite-to-view is that "I will need this later" signal.

Meanwhile the 26,268,293-view 黑马程序员 course has like_rate 1.47 and fav_rate 3.34 — fewer likes, plenty of saves, which is exactly what a utilitarian course looks like next to an entertainment upload.

None of that survives a pipeline that stores views and nothing else.

Design for snapshots, not for state

The +6,199-view delta is the whole argument for append-only storage. Two records, same bvid, different scraped_at, both true. If your table is videos(bvid PRIMARY KEY, views) you overwrite history and lose velocity — and velocity is the thing a trend client actually pays for.

Store (bvid, scraped_at, views, likes, coins, favorites) and let the current value be a max(scraped_at) query. At 1,460 bytes per record — 26,282 bytes for those 18 rows — you will not regret the storage.

Two smaller traps in the same area.

duration arrives in two formats. Search returns "39:58:14" as a string, detail returns 213 as an integer. One normalizer, one column, seconds only, format on output.

published_at is UTC with an offset, the website shows Beijing time. The MV stamped 2019-12-31T23:43:23+00:00, which reads as 2020-01-01 07:43 in China. Sort on the timestamp, never the string, or a New Year's Eve upload lands in the wrong year and your "uploaded in 2020" cohort silently gains a member.

I keep this one in a hosted actor so I do not babysit it: Bilibili Scraper on Apify. Search, detail, popular and ranking modes, same 17-field record shown above.

Next step: pick any bvid you care about, call the detail endpoint twice an hour apart, and subtract. If the delta is zero the video is dead and it is a bad candidate for a time series. If it moves, you have just measured the only metric that predicts anything.

Top comments (0)