badl/snippets/download.py

15 lines
506 B
Python

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)}%")