Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
215 lines (174 loc) · 5.66 KB
/
Copy pathutils.py
File metadata and controls
215 lines (174 loc) · 5.66 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File: utils.py
#
# Small helper functions: video property reader, human-readable ETA,
# and simple app data directory + settings management for future expansion.
#
import os
import json
import platform
import cv2
import math
APP_NAME = "DreamMachine"
def read_video_properties(path):
cap = cv2.VideoCapture(path)
if not cap.isOpened():
raise RuntimeError(f"Cannot open video: {path}")
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 640)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
try:
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
except Exception:
frame_count = 0
cap.release()
return {"fps": fps, "width": width, "height": height, "frame_count": frame_count}
def human_seconds(s):
# s integer seconds -> human readable
if s < 0:
s = 0
m, sec = divmod(int(s), 60)
h, m = divmod(m, 60)
if h > 0:
return f"{h}h {m}m {sec}s"
if m > 0:
return f"{m}m {sec}s"
return f"{sec}s"
# ---------------- App data directory + settings ----------------
def _default_app_data_root():
system = platform.system().lower()
if system.startswith("win"):
return os.environ.get("APPDATA", os.path.expanduser("~"))
elif system == "darwin":
return os.path.join(os.path.expanduser("~"), "Library", "Application Support")
else:
return os.path.join(os.path.expanduser("~"), ".local", "share")
def get_app_data_dir(app_name: str = APP_NAME):
root = _default_app_data_root()
path = os.path.join(root, app_name)
os.makedirs(path, exist_ok=True)
return path
def ensure_app_subdir(name: str, app_name: str = APP_NAME):
base = get_app_data_dir(app_name)
path = os.path.join(base, name)
os.makedirs(path, exist_ok=True)
return path
def _settings_path():
return os.path.join(get_app_data_dir(APP_NAME), "settings.json")
def load_settings():
try:
sp = _settings_path()
if os.path.exists(sp):
with open(sp, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
pass
return {}
def save_settings(data: dict):
try:
sp = _settings_path()
with open(sp, "w", encoding="utf-8") as f:
json.dump(data or {}, f, indent=2)
except Exception:
# Settings should never crash the app
pass
# ---------------- Recent files ----------------
def _recent_path():
return os.path.join(get_app_data_dir(APP_NAME), "recent.json")
def load_recent(max_items: int = 10):
try:
rp = _recent_path()
if os.path.exists(rp):
with open(rp, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
return data[:max_items]
except Exception:
pass
return []
def save_recent(items):
try:
rp = _recent_path()
with open(rp, "w", encoding="utf-8") as f:
json.dump(list(items) if items else [], f, indent=2)
except Exception:
pass
def add_recent(path: str, max_items: int = 10):
if not path:
return
try:
items = load_recent(max_items=max_items) or []
# move to front, keep unique
items = [p for p in items if p != path]
items.insert(0, path)
items = items[:max_items]
save_recent(items)
except Exception:
pass
# -------------- Recent by kind (videos, overlays, templates) --------------
def _recent_file_for(kind: str) -> str:
safe = (kind or "items").lower()
return os.path.join(get_app_data_dir(APP_NAME), f"recent_{safe}.json")
def load_recent_kind(kind: str, max_items: int = 20):
try:
rp = _recent_file_for(kind)
if os.path.exists(rp):
with open(rp, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
return data[:max_items]
except Exception:
pass
return []
def save_recent_kind(kind: str, items):
try:
rp = _recent_file_for(kind)
with open(rp, "w", encoding="utf-8") as f:
json.dump(list(items) if items else [], f, indent=2)
except Exception:
pass
def add_recent_kind(kind: str, path: str, max_items: int = 20):
if not path:
return
try:
items = load_recent_kind(kind, max_items=max_items) or []
items = [p for p in items if p != path]
items.insert(0, path)
items = items[:max_items]
save_recent_kind(kind, items)
except Exception:
pass
# ---------------- Open folder helper ----------------
def open_folder(path: str):
try:
if not path:
return False
if not os.path.exists(path):
return False
system = platform.system().lower()
if system.startswith("win"):
os.startfile(path) # type: ignore[attr-defined]
elif system == "darwin":
import subprocess
subprocess.Popen(["open", path])
else:
import subprocess
subprocess.Popen(["xdg-open", path])
return True
except Exception:
return False
def open_path(path: str):
try:
if not path or not os.path.exists(path):
return False
system = platform.system().lower()
if system.startswith("win"):
os.startfile(path) # type: ignore[attr-defined]
elif system == "darwin":
import subprocess
subprocess.Popen(["open", path])
else:
import subprocess
subprocess.Popen(["xdg-open", path])
return True
except Exception:
return False