-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
134 lines (115 loc) · 4.24 KB
/
base.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
@FileName:base.py
@Author:Huterox
@Description:Go For It
@Time:2024/5/21 8:31
@Copyright:©2018-2024 awesome!
"""
import os
import shutil
import uuid
from datetime import datetime
from colorlog import ColoredFormatter
current_dir_root = os.path.dirname(os.path.abspath(__file__))
cache_dir_root = os.path.join(current_dir_root, "cache")
whisper_tiny_dict = os.path.join(current_dir_root, "pluings/whisper/model/tiny")
docs_dir_root = os.path.join(current_dir_root,"docs")
run_app = os.path.join(current_dir_root, "run_app")
"""
********************************
* 对应组件的一些公共变量设置 *
********************************
"""
def create_signal_source(file_name, content):
# 获取当前的年月日
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
target_folder_path = os.path.join(cache_dir_root, str(year), str(month).zfill(2), str(day).zfill(2),
f"{uuid.uuid4()}")
# 检查目标文件夹是否存在,如果不存在则创建
os.makedirs(target_folder_path, exist_ok=True)
file_name = f"{file_name}.mp4"
# 构建完整的文件路径
file_path = os.path.join(target_folder_path, file_name)
# 创建文件
with open(file_path, 'wb') as file:
file.write(content)
return file_path
def hex_to_rgb(hex_color):
"""
将十六进制颜色字符串转换为RGB格式
:param hex_color: 十六进制颜色字符串,例如 "#fdfbfb"
:return: (R, G, B) 元组
"""
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
"""
只根据名字,创建临时存储文件路径,不创建文件,交给真正生成视频的函去创建保存文件
"""
def create_signal_temp_source(file_type="mp4"):
# 获取当前的年月日
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
target_folder_path = os.path.join(cache_dir_root, str(year), str(month).zfill(2), str(day).zfill(2),
f"{uuid.uuid4()}")
os.makedirs(target_folder_path, exist_ok=True)
file_name = f"{uuid.uuid4()}."+file_type
file_path = os.path.join(target_folder_path, file_name)
return file_path
def create_signal_temp_source_floder():
# 获取当前的年月日
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
target_folder_path = os.path.join(cache_dir_root, str(year), str(month).zfill(2), str(day).zfill(2),
f"{uuid.uuid4()}")
os.makedirs(target_folder_path, exist_ok=True)
return target_folder_path
import logging
def setup_logger():
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
if not logger.handlers:
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = ColoredFormatter(
"%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt='%Y-%m-%d %H:%M:%S',
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'bold_red',
}
)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
mylogger = setup_logger()
def delete_file(file_path):
try:
if os.path.exists(file_path):
os.remove(file_path)
print(f"File {file_path} has been deleted successfully.")
else:
print(f"File {file_path} does not exist.")
except PermissionError:
print(f"Permission denied: unable to delete {file_path}.")
except FileNotFoundError:
print(f"File not found: {file_path}.")
except Exception as e:
print(f"An error occurred while trying to delete the file: {e}")
def delete_folder(folder_path):
"""
删除指定的文件夹及其内容。
参数:
folder_path (str): 要删除的文件夹路径
"""
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
print(f"Folder '{folder_path}' has been deleted.")
else:
print(f"Folder '{folder_path}' does not exist.")