Обновить multithread.py
This commit is contained in:
parent
12372a18c0
commit
88ac7b11d5
@ -4,80 +4,72 @@ import json
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
|
|
||||||
# Конфигурация
|
formats = "18"
|
||||||
formats = "18,599,140,133,134,135,136,137,298,299"
|
|
||||||
cookies_file = "cookies.txt"
|
cookies_file = "cookies.txt"
|
||||||
output_template = "video/%(id)s.f%(format_id)s.%(ext)s"
|
output_template = "%(id)s/%(id)s.f%(format_id)s.%(ext)s"
|
||||||
num_threads = 4 # Количество потоков
|
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
|
|
||||||
url_file = "video_urls.txt"
|
url_file = "video_urls.txt"
|
||||||
log_file = "download_log.txt" # Файл журнала скачанных видео
|
log_file = "download_log.txt"
|
||||||
metadata_file = "metadata.json" # Файл метаданных
|
metadata_file = "metadata.json"
|
||||||
|
|
||||||
|
|
||||||
# Функция для загрузки одного видео
|
|
||||||
def download_video(url):
|
def download_video(url):
|
||||||
start_time = datetime.now()
|
start_time = datetime.now()
|
||||||
try:
|
try:
|
||||||
command = [
|
command = [
|
||||||
"yt-dlp",
|
"yt-dlp",
|
||||||
"-f", formats,
|
"--paths", paths,
|
||||||
|
"--paths", paths_temp,
|
||||||
|
"--cache-dir", cache_dir,
|
||||||
|
"--ffmpeg-location", ffmpeg_location,
|
||||||
|
"--format", formats,
|
||||||
"--cookies", cookies_file,
|
"--cookies", cookies_file,
|
||||||
"--output", output_template,
|
"--output", output_template,
|
||||||
|
"--output", output_infojson,
|
||||||
|
"--ignore-config",
|
||||||
|
"--no-progress",
|
||||||
"--write-info-json",
|
"--write-info-json",
|
||||||
url
|
url
|
||||||
]
|
]
|
||||||
print(f"Запуск команды: {' '.join(command)}")
|
print(f"Start: {' '.join(command)}")
|
||||||
subprocess.run(command, check=True)
|
subprocess.run(command, check=True)
|
||||||
end_time = datetime.now()
|
end_time = datetime.now()
|
||||||
log_download(url, start_time, end_time)
|
log_download(url, start_time, end_time)
|
||||||
update_metadata(url)
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Ошибка при загрузке видео {url}: {e}")
|
print(f"ERROR VIDEO {url}: {e}")
|
||||||
|
|
||||||
|
|
||||||
# Функция для записи информации о загруженных видео в лог
|
|
||||||
def log_download(url, start_time, end_time):
|
def log_download(url, start_time, end_time):
|
||||||
with open(log_file, "a") as log:
|
with open(log_file, "a") as log:
|
||||||
log.write(f"{url} скачано.\nНачало: {start_time}\nКонец: {end_time}\n\n")
|
log. Write(f"{url} Dowload.\nTue: {start_time}\nNext: {end_time}\n\n")
|
||||||
|
|
||||||
# Функция для обновления метаданных
|
|
||||||
def update_metadata(url):
|
|
||||||
video_id = url.split('=')[-1]
|
|
||||||
metadata_path = f"video/{video_id}.info.json"
|
|
||||||
if os.path.exists(metadata_path):
|
|
||||||
with open(metadata_path, "r") as meta_file:
|
|
||||||
metadata = json.load(meta_file)
|
|
||||||
|
|
||||||
# Загрузка существующих метаданных
|
|
||||||
if os.path.exists(metadata_file):
|
|
||||||
with open(metadata_file, "r") as file:
|
|
||||||
all_metadata = json.load(file)
|
|
||||||
else:
|
|
||||||
all_metadata = {}
|
|
||||||
|
|
||||||
# Добавление новых метаданных
|
|
||||||
all_metadata[url] = metadata
|
|
||||||
|
|
||||||
# Сохранение метаданных
|
|
||||||
with open(metadata_file, "w") as file:
|
|
||||||
json.dump(all_metadata, file, ensure_ascii=False, indent=4)
|
|
||||||
|
|
||||||
# Чтение URL из файла
|
|
||||||
if not os.path.exists(url_file):
|
if not os.path.exists(url_file):
|
||||||
print(f"Файл {url_file} не найден.")
|
print(f"File {url_file} Empiy.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
with open(url_file, "r") as file:
|
with open(url_file, "r") as file:
|
||||||
video_urls = [line.strip() for line in file if line.strip()]
|
video_urls = [line.strip() for line in file if line.strip()]
|
||||||
|
|
||||||
# Проверка наличия папки для вывода
|
|
||||||
os.makedirs("video", exist_ok=True)
|
|
||||||
|
|
||||||
# Многопоточная загрузка
|
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:
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
|
||||||
futures = [executor.submit(download_video, url) for url in video_urls]
|
futures = [executor.submit(download_video, url) for url in video_urls]
|
||||||
for future in concurrent.futures.as_completed(futures):
|
for future in concurrent.futures.as_completed(futures):
|
||||||
try:
|
try:
|
||||||
future.result()
|
future. Result()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Ошибка при обработке: {e}")
|
print(f"ERROR: {e}")
|
||||||
Loading…
x
Reference in New Issue
Block a user