diff --git a/snippets/download.py b/snippets/download.py new file mode 100644 index 0000000..8e4f05f --- /dev/null +++ b/snippets/download.py @@ -0,0 +1,15 @@ +from bilibili_api import HEADERS +import httpx + + +def download_video(url: str): + with httpx.stream("GET", url, headers=HEADERS) as r: + length = int(r.headers["content-length"]) + received_bytes = 0 + with open("demo.m4a", "wb") as f: + for chunk in r.iter_bytes(1024): + if not chunk: + break + received_bytes += len(chunk) + f.write(chunk) + print(f"downloaded {int(received_bytes / length * 100)}%")