Download audio and show progress

This commit is contained in:
zhbaor 2023-02-20 10:50:22 +08:00
parent dffedeeb4e
commit 56fedd3beb

15
snippets/download.py Normal file
View file

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