From 56fedd3beb43f7c2b95f326cbd38759b39e0c663 Mon Sep 17 00:00:00 2001 From: Zhao Zuohong Date: Mon, 20 Feb 2023 10:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Download=20audio=20and=20show=20pro?= =?UTF-8?q?gress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snippets/download.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/download.py 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)}%")