badl/snippets/download.py

16 lines
506 B
Python
Raw Normal View History

2023-02-20 10:50:22 +08:00
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)}%")