yt-dlp_e/multithread.py

75 lines
2.1 KiB
Python

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"
def download_video(url):
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)
except subprocess.CalledProcessError as e:
print(f"ERROR VIDEO {url}: {e}")
def log_download(url, start_time, end_time):
with open(log_file, "a") as log:
log. Write(f"{url} Dowload.\nTue: {start_time}\nNext: {end_time}\n\n")
if not os.path.exists(url_file):
print(f"File {url_file} Empiy.")
exit(1)
with open(url_file, "r") as file:
video_urls = [line.strip() for line in file if line.strip()]
os.makedirs("~/Downloads/staging", exist_ok=True)
os.makedirs("~/Downloads/temp", exist_ok=True)
os.makedirs("~/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]
for future in concurrent.futures.as_completed(futures):
try:
future. Result()
except Exception as e:
print(f"ERROR: {e}")