1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| def download_video(video_url: str, filename: str): """Download the video given the video URL.""" print(f" 🔍 Downloading {filename}...", end="\r") tmp_file_path = filename + ".tmp" if not path.exists(filename) or path.exists(tmp_file_path): try: r = x.get(video_url, headers=VIDEO_HEADERS, stream=True) r.raise_for_status() # Raise an exception if the response is not 200 OK total_size = int(r.headers["Content-Length"]) if path.exists(tmp_file_path): tmp_size = path.getsize(tmp_file_path) print(f" Already downloaded {tmp_size} Bytes out of {total_size} Bytes ({100 * tmp_size / total_size:.2f}%)") if tmp_size == total_size: move(tmp_file_path, filename) print(" ✅ Downloaded {filename} successfully.") return True elif tmp_size > total_size: print(" ❌ The downloaded .tmp file is larger than the remote file. It is likely corrupted.") return False else: tmp_size = 0 print(f" File is {total_size} Bytes, downloading...")
with open(tmp_file_path, "ab") as f: retries = 0 while retries < RETRIES: try: res = x.get(video_url, headers={**VIDEO_HEADERS, "Range": f"bytes={tmp_size}-"}, stream=True) for chunk in res.iter_content(chunk_size=CHUNK_SIZE): tmp_size += len(chunk) f.write(chunk) f.flush()
done = int(50 * tmp_size / total_size) print(f"\r [{'█' * done}{' ' * (50 - done)}] {100 * tmp_size / total_size:.0f}%", end="") break except RequestException as e: retries += 1 print(f"\n ⚠️ Retrying... ({retries}/{RETRIES})") sleep(INTERVAL) else: print(f"\n ❌ Failed to download {filename} after {RETRIES} retries.") return False
if tmp_size == total_size: move(tmp_file_path, filename) print(f"\n ✅ Downloaded {filename} successfully.")
except RequestException as e: # Log the error print(e) with open(filename + '_log.txt', 'a+', encoding = 'UTF-8') as f: f.write('%s, %s\n' % (video_url, e)) print(f" ❌ Failed to download {filename}.") else: print(f" ✅ Downloaded {filename} successfully.")
|