Update 组播定期检测.py

This commit is contained in:
frxz751113
2024-08-20 23:38:25 +08:00
committed by GitHub
parent fdf8ee03be
commit d9cea48295
+17 -3
View File
@@ -1,9 +1,12 @@
import os
import os
import cv2
import time
from tqdm import tqdm
import sys # 导入 sys 模块
# 初始化字典
detected_ips = {}
def get_ip_key(url):
"""从URL中提取IP地址,并构造一个唯一的键"""
start = url.find('://') + 3
@@ -15,12 +18,15 @@ def get_ip_key(url):
break
dot_count += 1
return url[start:end] if dot_count == 3 else None
# 设置固定的文件夹路径
folder_path = 'playlist'
# 确保文件夹路径存在
if not os.path.isdir(folder_path):
print("指定的文件夹不存在。")
sys.exit()
sys.exit() # 使用 sys.exit() 退出程序
# 遍历文件夹中的所有.txt文件
for filename in os.listdir(folder_path):
if filename.endswith('.txt'):
@@ -28,6 +34,7 @@ for filename in os.listdir(folder_path):
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
# 准备写回文件
with open(file_path, 'w', encoding='utf-8') as output_file:
# 使用tqdm显示进度条
@@ -35,15 +42,19 @@ for filename in os.listdir(folder_path):
if 'genre' in line:
output_file.write(line)
continue
parts = line.split(',', 1)
if len(parts) == 2:
channel_name, url = parts
channel_name = channel_name.strip()
url = url.strip()
ip_key = get_ip_key(url)
# 检查是否已经检测过该IP地址,并且状态为 'ok'
if ip_key in detected_ips and detected_ips[ip_key]['status'] == 'ok':
output_file.write(line)
elif ip_key:
continue
# 尝试打开视频流
cap = cv2.VideoCapture(url)
start_time = time.time()
@@ -54,11 +65,14 @@ for filename in os.listdir(folder_path):
break
frame_count += 1
cap.release()
# 根据检测结果更新字典,并写入文件
if frame_count >= 200:
detected_ips[ip_key] = {'status': 'ok'}
output_file.write(line)
else:
detected_ips[ip_key] = {'status': 'fail'}
# 打印检测结果
for ip_key, result in detected_ips.items():
print(f"IP Key: {ip_key}, Status: {result['status']}")