Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -189,7 +189,7 @@ def upload_to_dataset(file_path, project_id, filename, subfolder="videos"):
|
|
| 189 |
return None
|
| 190 |
|
| 191 |
def get_font_path(font_family):
|
| 192 |
-
"""Get font path from font family name -
|
| 193 |
font_family_lower = font_family.lower().replace(' ', '_')
|
| 194 |
print(f"π Looking for font: {font_family}")
|
| 195 |
print(f"π Available fonts: {list(FONTS.keys())}")
|
|
@@ -198,14 +198,20 @@ def get_font_path(font_family):
|
|
| 198 |
if font_family_lower in FONTS:
|
| 199 |
path = FONTS[font_family_lower]["path"]
|
| 200 |
print(f"β
Found exact match: {path}")
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
# Try partial match
|
| 204 |
-
for key, font_info in FONTS.items():
|
| 205 |
-
if font_family_lower in key or key in font_family_lower:
|
| 206 |
-
path = font_info["path"]
|
| 207 |
-
print(f"β
Found partial match: {key} -> {path}")
|
| 208 |
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
print(f"β Font not found: {font_family}")
|
| 211 |
return None
|
|
@@ -213,15 +219,15 @@ def get_font_path(font_family):
|
|
| 213 |
# =============================================
|
| 214 |
# ASS SUBTITLE METHOD - WORKS FOR CHINESE
|
| 215 |
# =============================================
|
|
|
|
| 216 |
def create_text_overlay(input_video, output_video, text_style):
|
| 217 |
-
"""Add text overlay using ASS subtitles -
|
| 218 |
font_path = get_font_path(text_style.font_family)
|
| 219 |
if not font_path:
|
| 220 |
print(f"β οΈ Font not found: {text_style.font_family}")
|
| 221 |
return False
|
| 222 |
|
| 223 |
print(f"β
Using font: {font_path}")
|
| 224 |
-
print(f"π Text to render: {text_style.text}")
|
| 225 |
|
| 226 |
# Create working directory for ASS file
|
| 227 |
work_dir = os.path.dirname(output_video)
|
|
@@ -231,8 +237,7 @@ def create_text_overlay(input_video, output_video, text_style):
|
|
| 231 |
color_map = {
|
| 232 |
"white": "FFFFFF", "black": "000000", "red": "FF0000",
|
| 233 |
"green": "00FF00", "blue": "0000FF", "yellow": "FFFF00",
|
| 234 |
-
"gold": "FFD700", "purple": "800080", "cyan": "00FFFF"
|
| 235 |
-
"pink": "FFC0CB", "orange": "FFA500", "brown": "A52A2A"
|
| 236 |
}
|
| 237 |
|
| 238 |
# Get font color
|
|
@@ -243,19 +248,13 @@ def create_text_overlay(input_video, output_video, text_style):
|
|
| 243 |
bg_color_name = bg_parts[0]
|
| 244 |
bg_opacity = float(bg_parts[1]) if len(bg_parts) > 1 else 0.5
|
| 245 |
bg_color_hex = color_map.get(bg_color_name.lower(), "000000")
|
| 246 |
-
bg_alpha = int((1 - bg_opacity) * 255)
|
| 247 |
|
| 248 |
-
# Map position to ASS alignment
|
| 249 |
pos_map = {
|
| 250 |
-
"bottom-left": 1,
|
| 251 |
-
"
|
| 252 |
-
"
|
| 253 |
-
"left": 4,
|
| 254 |
-
"center": 5,
|
| 255 |
-
"right": 6,
|
| 256 |
-
"top-left": 7,
|
| 257 |
-
"top-center": 8,
|
| 258 |
-
"top-right": 9
|
| 259 |
}
|
| 260 |
alignment = pos_map.get(text_style.position, 5)
|
| 261 |
|
|
@@ -264,10 +263,10 @@ def create_text_overlay(input_video, output_video, text_style):
|
|
| 264 |
margin_r = text_style.margin if alignment in [3,6,9] else 0
|
| 265 |
margin_v = text_style.margin
|
| 266 |
|
| 267 |
-
# Get
|
| 268 |
font_name = os.path.basename(font_path).split('.')[0]
|
| 269 |
|
| 270 |
-
# Create ASS file content with proper
|
| 271 |
ass_content = f"""[Script Info]
|
| 272 |
; Script generated by Video Styling Space
|
| 273 |
ScriptType: v4.00+
|
|
@@ -283,7 +282,7 @@ Style: Default,{font_name},{text_style.font_size},&H00{font_color_hex},&H000000F
|
|
| 283 |
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
| 284 |
Dialogue: 0,0:00:00.00,0:00:10.00,Default,,0,0,0,,{text_style.text}"""
|
| 285 |
|
| 286 |
-
# Write ASS file with UTF-8 encoding
|
| 287 |
with open(ass_file, 'w', encoding='utf-8') as f:
|
| 288 |
f.write(ass_content)
|
| 289 |
|
|
|
|
| 189 |
return None
|
| 190 |
|
| 191 |
def get_font_path(font_family):
|
| 192 |
+
"""Get font path from font family name - FIXED for subfolders"""
|
| 193 |
font_family_lower = font_family.lower().replace(' ', '_')
|
| 194 |
print(f"π Looking for font: {font_family}")
|
| 195 |
print(f"π Available fonts: {list(FONTS.keys())}")
|
|
|
|
| 198 |
if font_family_lower in FONTS:
|
| 199 |
path = FONTS[font_family_lower]["path"]
|
| 200 |
print(f"β
Found exact match: {path}")
|
| 201 |
+
# Verify file exists
|
| 202 |
+
if os.path.exists(path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
return path
|
| 204 |
+
else:
|
| 205 |
+
print(f"β οΈ File exists in FONTS but not on disk: {path}")
|
| 206 |
+
|
| 207 |
+
# If not found, try to find by scanning directories
|
| 208 |
+
print("π Scanning directories for font files...")
|
| 209 |
+
for root, dirs, files in os.walk(FONTS_DIR):
|
| 210 |
+
for file in files:
|
| 211 |
+
if file.lower().startswith(font_family_lower) or font_family_lower in file.lower():
|
| 212 |
+
full_path = os.path.join(root, file)
|
| 213 |
+
print(f"β
Found font file: {full_path}")
|
| 214 |
+
return full_path
|
| 215 |
|
| 216 |
print(f"β Font not found: {font_family}")
|
| 217 |
return None
|
|
|
|
| 219 |
# =============================================
|
| 220 |
# ASS SUBTITLE METHOD - WORKS FOR CHINESE
|
| 221 |
# =============================================
|
| 222 |
+
|
| 223 |
def create_text_overlay(input_video, output_video, text_style):
|
| 224 |
+
"""Add text overlay using ASS subtitles - FIXED for subfolders"""
|
| 225 |
font_path = get_font_path(text_style.font_family)
|
| 226 |
if not font_path:
|
| 227 |
print(f"β οΈ Font not found: {text_style.font_family}")
|
| 228 |
return False
|
| 229 |
|
| 230 |
print(f"β
Using font: {font_path}")
|
|
|
|
| 231 |
|
| 232 |
# Create working directory for ASS file
|
| 233 |
work_dir = os.path.dirname(output_video)
|
|
|
|
| 237 |
color_map = {
|
| 238 |
"white": "FFFFFF", "black": "000000", "red": "FF0000",
|
| 239 |
"green": "00FF00", "blue": "0000FF", "yellow": "FFFF00",
|
| 240 |
+
"gold": "FFD700", "purple": "800080", "cyan": "00FFFF"
|
|
|
|
| 241 |
}
|
| 242 |
|
| 243 |
# Get font color
|
|
|
|
| 248 |
bg_color_name = bg_parts[0]
|
| 249 |
bg_opacity = float(bg_parts[1]) if len(bg_parts) > 1 else 0.5
|
| 250 |
bg_color_hex = color_map.get(bg_color_name.lower(), "000000")
|
| 251 |
+
bg_alpha = int((1 - bg_opacity) * 255)
|
| 252 |
|
| 253 |
+
# Map position to ASS alignment
|
| 254 |
pos_map = {
|
| 255 |
+
"bottom-left": 1, "bottom-center": 2, "bottom-right": 3,
|
| 256 |
+
"left": 4, "center": 5, "right": 6,
|
| 257 |
+
"top-left": 7, "top-center": 8, "top-right": 9
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
}
|
| 259 |
alignment = pos_map.get(text_style.position, 5)
|
| 260 |
|
|
|
|
| 263 |
margin_r = text_style.margin if alignment in [3,6,9] else 0
|
| 264 |
margin_v = text_style.margin
|
| 265 |
|
| 266 |
+
# Get just the filename without extension and path
|
| 267 |
font_name = os.path.basename(font_path).split('.')[0]
|
| 268 |
|
| 269 |
+
# Create ASS file content with proper font path
|
| 270 |
ass_content = f"""[Script Info]
|
| 271 |
; Script generated by Video Styling Space
|
| 272 |
ScriptType: v4.00+
|
|
|
|
| 282 |
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
| 283 |
Dialogue: 0,0:00:00.00,0:00:10.00,Default,,0,0,0,,{text_style.text}"""
|
| 284 |
|
| 285 |
+
# Write ASS file with UTF-8 encoding
|
| 286 |
with open(ass_file, 'w', encoding='utf-8') as f:
|
| 287 |
f.write(ass_content)
|
| 288 |
|