From 3ba23834f67b279dfc2d983f110a70ec2c0cb8b0 Mon Sep 17 00:00:00 2001 From: evgeniy_t Date: Mon, 3 Feb 2025 12:58:08 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20test.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/test.py b/test.py index e69de29..a2433f0 100644 --- a/test.py +++ b/test.py @@ -0,0 +1,93 @@ +import subprocess +import os +import json +from datetime import datetime +import concurrent.futures + +formats = "18" +cookies_file = "cookies.txt" +output_template = "%(id)s/%(id)s.f%(format_id)s.%(ext)s" +output_infojson = "infojson:%(id)s/%(id)s.t%(duration_string)s.%(ext)s" +paths = "~/Downloads/staging" +paths_temp = "temp:~/Downloads/temp" +cache_dir = "~/Downloads/cache" +ffmpeg_location = "~/" +num_retries = 10 +fragment_retries = 10 +concur_fragments = 1 +num_threads = 16 + +url_file = "video_urls.txt" +log_file = "download_log.txt" +metadata_file = "metadata.json" +successful_downloads_file = "successful_downloads.txt" +failed_downloads_file = "failed_downloads.txt" + +def load_downloaded_urls(file): + if os.path.exists(file): + with open(file, "r") as f: + return set(line.strip() for line in f if line.strip()) + return set() + +def save_downloaded_url(file, url): + with open(file, "a") as f: + f.write(f"{url}\n") + +def download_video(url): + if url in successful_downloads: + print(f"Video {url} already downloaded. Skipping.") + return + if url in failed_downloads: + print(f"Video {url} previously failed. Retrying.") + + start_time = datetime.now() + try: + command = [ + "yt-dlp", + "--paths", paths, + "--paths", paths_temp, + "--cache-dir", cache_dir, + "--ffmpeg-location", ffmpeg_location, + "--format", formats, + "--cookies", cookies_file, + "--output", output_template, + "--output", output_infojson, + "--ignore-config", + "--no-progress", + "--write-info-json", + url + ] + print(f"Start: {' '.join(command)}") + subprocess.run(command, check=True) + end_time = datetime.now() + log_download(url, start_time, end_time) + save_downloaded_url(successful_downloads_file, url) + except subprocess.CalledProcessError as e: + print(f"ERROR VIDEO {url}: {e}") + save_downloaded_url(failed_downloads_file, url) + +def log_download(url, start_time, end_time): + with open(log_file, "a") as log: + log.write(f"{url} Download.\nStart: {start_time}\nEnd: {end_time}\n\n") + +if not os.path.exists(url_file): + print(f"File {url_file} Empty.") + exit(1) + +successful_downloads = load_downloaded_urls(successful_downloads_file) +failed_downloads = load_downloaded_urls(failed_downloads_file) + +with open(url_file, "r") as file: + video_urls = [line.strip() for line in file if line.strip()] + +os.makedirs(os.path.expanduser("~/Downloads/staging"), exist_ok=True) +os.makedirs(os.path.expanduser("~/Downloads/temp"), exist_ok=True) +os.makedirs(os.path.expanduser("~/Downloads/cache"), exist_ok=True) + +with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor: + futures = [executor.submit(download_video, url) for url in video_urls if url not in successful_downloads] + for future in concurrent.futures.as_completed(futures): + try: + future.result() + except Exception as e: + print(f"ERROR: {e}") \ No newline at end of file